Append date to existing file name?
I want to append the date to existing file name.
I was looking for and found that. It works, but it doesnot keep the existing name. I just want to put the date after the existing name when copy. Code:
cp test.back "test.backup-"`date +"%d-%m-%Y"` Script I have is: cp /test/ /backup In folder /test I have several files with random names. I want to copy them in /backup and keep random names they have but append the date after their names. |
If you don't know the source file name, then what is your process to copy the files.
Is your files path in any text file so you are going to use cat command with cp to copy these files? In this case you can use a shell script which adds the date after the file name? |
Hi,
some time ago I wrote a script for a similar task, and I used ls -1 to create a plain list of the files contained in the source directory. Then sed in a while loop reads the lines from the list until it's over, and the outputs are used to make a copy of each file, appending a date or whatever to its original name. Code:
#!/bin/bash Kind regards, Philip |
You shouldn't rely on parsing 'ls' output in scripts. Please see http://mywiki.wooledge.org/BashPitfalls.
This should copy the source file preserving the relative path and append an ISO-style date: Code:
#!/bin/bash -- |
@unSpawn: Thank you! :hattip:
|
You're welcome. If you like shell scripting then you may like
Code:
function howto() { echo "Bash scripting guides: |
Hi unSpawn.
You are a guru man. I never will create such a hard script I think. I tested it and it works like a charm. But let me make it more difficult? May I? Instead of cp I used Code:
rsync --ignore-existing -vr |
Quote:
|
Well, I really like your script and I prefer to do it by cronjob instead of install new software. Rsync will copy everyday the same file and just will change the name with current day. That's because of --ignore-existing, right?
I use --ignore-existing because only that protect my backup of virus. A time ago, a virus encrypt my backup changeing the name from backup.back to backup.encrypt and delete backup.back. Using --ignore-existing it will not affect to original name, but if I use option only to update the file, rsync will update the original name and encrypt part ot it. So my backups will be encrypted too. I dont know if you understand me. |
All times are GMT -5. The time now is 09:50 PM. |