Issue
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:
- aaa.csv
- bbb.csv
- ccc.csv
- 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:
- aaa091012.csv
- bbb091012.csv
- ccc091012.csv
- ddd091012.csv
I want to write a shell script for this. Please give me some idea or some sample scripts related to this.
Solution
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
Answered By - Stephane Rouberol Answer Checked By - David Marino (WPSolving Volunteer)