ran
ran

Reputation: 737

Shell script to copy files from one location to another location and add the current date to every file name

I have a folder in my server which contains some files. These are automated that means everyday we get new files automatically which will overwrite the old ones. So want to take a back up for this data. How can I copy all these files into another folder by renaming the files with current date while copying.

ex : I have home/webapps/project1/folder1 folder which contains 4 files:

  1. aaa.csv
  2. bbb.csv
  3. ccc.csv
  4. ddd.csv

Now, I want to copy all these four files in to a different location, like home/webapps/project1/folder2.
While copying these files I want to rename each file and add the current date to the file. So my file names in folder2 should be:

  1. aaa091012.csv
  2. bbb091012.csv
  3. ccc091012.csv
  4. ddd091012.csv

I want to write a shell script for this. Please give me some idea or some sample scripts related to this.

Upvotes: 32

Views: 314253

Answers (6)

kanakaraj. p
kanakaraj. p

Reputation: 1

You can be used this step is very useful:

for i in `ls -l folder1 | grep -v total | awk '{print $ ( ? )}'`
do
   cd folder1
   cp $i folder2/$i.`date +%m%d%Y`
done

Upvotes: 0

Stephane Rouberol
Stephane Rouberol

Reputation: 4384

In bash, provided you files names have no spaces:

cd /home/webapps/project1/folder1
for f in *.csv
do 
   cp -v "$f" /home/webapps/project1/folder2/"${f%.csv}"$(date +%m%d%y).csv
done

Upvotes: 50

Lipongo
Lipongo

Reputation: 1261

You could use a script like the below. You would just need to change the date options to match the format you wanted.

#!/bin/bash

for i in `ls -l /directroy`
do
cp $i /newDirectory/$i.`date +%m%d%Y`
done

Upvotes: 6

l0b0
l0b0

Reputation: 58828

cp --archive home/webapps/project1/folder1/{aaa,bbb,ccc,ddd}.csv home/webapps/project1/folder2
rename 's/\.csv$/'$(date +%m%d%Y).csv'/' home/webapps/project1/folder2/{aaa,bbb,ccc,ddd}.csv

Explanation:

  • --archive ensures that the files are copied with the same ownership and permissions.
  • foo{bar,baz} is expanded into foobar foobaz.
  • rename is a commonly available program to do exactly this kind of substitution.

PS: don't use ls for this.

Upvotes: 1

Dr. Jan-Philip Gehrcke
Dr. Jan-Philip Gehrcke

Reputation: 35771

There is a proper way to split the filename and the extension: Extract filename and extension in Bash

You can apply it like this:

date=$(date +"%m%d%y")
for FILE in folder1/*.csv
do
    bname=$(basename "$FILE")
    extension="${bname##*.}"
    filenamewoext="${bname%.*}"
    newfilename="${filenamewoext}${date}.${extension}
    cp folder1/${FILE} folder2/${newfilename}
done

Upvotes: 2

perreal
perreal

Reputation: 97948

path_src=./folder1
path_dst=./folder2
date=$(date +"%m%d%y")
for file_src in $path_src/*; do
  file_dst="$path_dst/$(basename $file_src | \
    sed "s/^\(.*\)\.\(.*\)/\1$date.\2/")"
  echo mv "$file_src" "$file_dst"
done

Upvotes: 2

Related Questions