aswathy
aswathy

Reputation: 831

Unix Shell scripting for copying file from one folder to another

Hi this is my shell script to copy files from one directory to another directory with timestamp.But my script shows too many arguments.I want to copy files from one directory to another.What error in my code.

Date_Val="$(date +%Y%m%d%H%M%S)";

cd /etl_mbl/SrcFiles/

if [  -f /etl_mbl/SrcFiles/SrcFiles_TEMP*.csv ]
then
cp /etl_mbl/SrcFiles/SrcFiles_TEMP/*.csv /etl_mbl/SrcFiles/Archive/*_$Date_Val.csv
fi

Upvotes: 0

Views: 121

Answers (1)

Henrik Sundström
Henrik Sundström

Reputation: 56

The reason you got "too many arguments" error is that the wildcard in the "if" statement expands to a multitude of files. Please also note that you cannot have wildcards in the destination of a "cp". You probably want something like this:

#!/bin/bash

Date_Val="$(date +%Y%m%d%H%M%S)";

for file in ./src/*.csv; do
    filename=${file##*/}
    basename=${filename%.*}
    cp $file ./archive/$basename\_$Date_Val.csv
done

Upvotes: 2

Related Questions