Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 in to a another folder by renaming the files with current date while copying.

ex : i have a folder named folder1 which contains 4 files. path for this folder is home/install/project1/folder1


now i want to copy all these four files in to a different folder named folder2. path for this folder is home/install/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..


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

share|improve this question

migrated from Mar 4 '13 at 9:36

This question came from our site for professional and enthusiast programmers.

@JimGarrison, i tried this command...$ mv /directory_one/* /directory – G M Ramesh Mar 4 '13 at 5:47
@JimGarrison, for unix shell , i know the script how to copy files but for linux i dont know... if the unix commands works for linux also then its ok. i can make it out... – G M Ramesh Mar 4 '13 at 5:49
98% of Unix commands have identical or very similar counterparts in Linux. – Ex Umbris Mar 4 '13 at 5:52
@GMRamesh: which linux flavor are you using? Ubuntu? – mvp Mar 4 '13 at 5:54
I am using Red Hat – G M Ramesh Mar 4 '13 at 5:57
up vote 2 down vote accepted
d=$(date +%m%d%y)

for srcfile in ${srcdir}/*
    dstfile=$(basename $srcfile)
    cp $srcfile $dstdir/$dstfile
share|improve this answer
There are a few problems with that script; most notably, $srcfile will actually just be equal to $srcdir. – ruakh Mar 4 '13 at 6:19
Good catch @ruakh, editing now – maverick Mar 4 '13 at 6:20
Just add a note that this script would not copy any files present in sub-directories of srcdir to ensure it is what OP expects. – Tuxdude Mar 4 '13 at 6:21
Now $dstfile will be e.g. home/install/project1/folder1/aaa091012.dat, so $dstdir/$dstfile will be e.g. home/install/project2/folder2/home/install/project1/folder1/aaa091012.dat. – ruakh Mar 4 '13 at 6:23
Nice, @ruakh - edited again – maverick Mar 4 '13 at 6:26

You can do it in 2 steps, first cp:

cp -rp source/ target/

Then use rename. But you should use Ubuntu flavor of it, which is actually Perl script. For Redhat, you might be able to install or download it from Unfortunately, native Redhat/Fedora rename does not support Perl regular expressions and will not work for this.

At any rate, use Perl-ish rename like this:

cd target
rename 's/\.dat$/091012.dat/' *

You can even use it recursively by combining it with find and xargs, something like:

find | xargs rename 's/\.dat$/091012.dat/'
share|improve this answer

this worked for me:

    cd home/install/project1/folder1
    for f in *.dat
    cp -v $f /home/install/project1/folder2/${f%.dat}$(date +%m%d%y).dat
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.