Need to replace a file with a newer version but keep the old file via a rename
I have two directories, one is called "stage" and the other "prod". I need to copy a file from "stage" to "prod" in such a way that it doesn't overwrite the file in the target directory, I need it to append the original file with the system date. Is there a way to do this with a single command or script?
Example: file cupt001.sqr would be copied to "prod" and the original file would be renamed cupt001.sqr_04302009. A look up would show cupt001.sqr and cupt001.sqr_04302009 in the target directory. Any help would be appreciated. |
Hi,
Welcome to LQ! You'll have to use a script for that (should be easy enough). A quick hack (no error checking): Code:
#!/bin/bash any normal cp. Note that I've changed the date to ISO format so it sorts sensibly. Month, day, year makes no sense whatsoever in sorting. Cheers, Tink |
If you execute these commands, would it do what you want?
Code:
cd prod The first line says you change the working directory to prod. The second line says you rename cupt001.sqr. The `date +%m%d%Y` part is replaced by the date of today (MMDDYYYY). This is called command substitution, more info: http://tldp.org/LDP/Bash-Beginners-G...#sect_03_04_04 The third line says you copy cupt001.sqr to this directory. This only works if prod and stage are in the same directory. If they aren't you have to adapt the command. I assume you know how to use the command line for day to day tasks, and thus that you already knew most of the things I said above. If you want to make a script of it, you maybe can read the Bash Guide for Beginners: http://tldp.org/LDP/Bash-Beginners-G...tml/index.html |
Quote:
Code:
function bucp () { cp $2 $2_`date +%m%d%Y` && cp $1 $2; } Code:
core:~/test$ function bucp () { cp $2 $2_`date +%m%d%Y` && cp $1 $2; } Code:
bucp /path/to/stage/filename.ext /path/to/prod/filename.txt Disclaimer: I didn't do much testing, just slapped it together. I suggest you test and additionally, I'd add at least h&m to the time stamp in case you move a file over more than once a day and Tink's suggestion on sane sorting is very sound. Edit2: This does have the advantage that if for some reason the backup fails, it won't execute the second portion of it and overwrite a file you've not backed up. Edit3: There's also a built in way of doing numbered backups (but not time and date stamped, although... that information is already available.) you could put this alias in your .bashrc Code:
alias cp='cp --backup=numbered' |
You don't really need a script if you use the available cp options:
Code:
cp -bS $(date +_%Y%m%d) stage/cupt001.sqr prod/ |
All times are GMT -5. The time now is 07:12 AM. |