27

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.

paleonix
  • 2,293
  • 1
  • 13
  • 29
ran
  • 667
  • 4
  • 9
  • 16
  • You probably want to "move" the files into your new directory rather than "copy". Otherwise, the next time you run the script, you will copy files even if they have not changed. With the answers provided, use the `mv` command instead of `cp`. If the two directories are on different file systems (meaning you cannot use `mv` ), add a `rm` command to remove the source file after the copy. And if you **do** need to use `cp`, I'd add the `-p` option to preserve the original timestamp. – BellevueBob Sep 11 '12 at 14:49
  • @ran, for linux server also , the below answer will work???? – G M Ramesh Mar 04 '13 at 05:33

6 Answers6

46

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Stephane Rouberol
  • 4,286
  • 19
  • 18
  • hi..i want to add these files to list and loop through that list...how can i do it.... – ran Sep 11 '12 at 15:25
  • 1
    Also for f in `/home/webapps/project1/folder1/*.csv` would suffice. No need to `cd` to the directory. – fedorqui Sep 28 '16 at 10:40
4

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
hardillb
  • 54,545
  • 11
  • 67
  • 105
Lipongo
  • 1,221
  • 8
  • 14
  • $i.`date +%m%d%Y` does this create file as aaa.091012.csv format...i don't want any 'dot's in my file name...the name should be just namedate.csv (aaa091012.csv)..??? – ran Sep 10 '12 at 16:26
  • 1
    I have a feeling I was down voted because some one mistakenly read my ls command as have a lowercase L, it's a number one. This outputs each file on a single line. – Lipongo Sep 11 '12 at 15:39
  • On windows I had to use `-1` (one) and pass whole path to current file before `$i` in first argument of `cp`, like `/directory/$i`. – Igor Nowosad Aug 24 '21 at 08:08
2
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
perreal
  • 94,503
  • 21
  • 155
  • 181
2

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
Community
  • 1
  • 1
Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
1
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.

l0b0
  • 55,365
  • 30
  • 138
  • 223
0

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
cSteusloff
  • 2,487
  • 7
  • 30
  • 51