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/webapps/project1/folder1
- aaa.csv
- bbb.csv
- ccc.csv
- ddd.csv
now i want to copy all these four files in to a different folder named folder2. path for this folder is 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.
Explanation:
--archive
ensures that the files are copied with the same ownership and permissions.foo{bar,baz}
is expanded intofoobar foobaz
.rename
is a commonly available program to do exactly this kind of substitution.PS: don't use
ls
for this.You could use a script like the below. You would just need to change the date options to match the format you wanted.
There is a proper way to split the filename and the extension: Extract filename and extension in Bash
You can apply it like this:
You can be used this step is very useful:
In
bash
, provided you files names have no spaces: