LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need to replace a file with a newer version but keep the old file via a rename (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-replace-a-file-with-a-newer-version-but-keep-the-old-file-via-a-rename-722782/)

Joe_P 04-30-2009 02:26 PM

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.

Tinkster 04-30-2009 03:12 PM

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
datestamp=$( date "+%Y%m%d" )
cp $2 $2"_$datestamp"
cp $1 $2

Save that e.g. as cpd and make it executable, then run it like
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

janhe 04-30-2009 03:13 PM

If you execute these commands, would it do what you want?

Code:

cd prod
mv cupt001.sqr cupt001.sqr_`date +%m%d%Y`
cp ../stage/cupt001.sqr .

note that the second line contains 2 backtics (`) not apostrophes (') or some other kind of quotation mark.

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

rweaver 04-30-2009 03:33 PM

Quote:

Originally Posted by Joe_P (Post 3526089)
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.

add this to .bashrc

Code:

function bucp () { cp $2 $2_`date +%m%d%Y` && cp $1 $2; }
Then use: bucp src dst

Code:

core:~/test$ function bucp () { cp $2 $2_`date +%m%d%Y` && cp $1 $2; }
core:~/test$ echo "1" > file1
core:~/test$ echo "2" > file2
core:~/test$ echo "3" > file3
core:~/test$ bucp file1 file2
core:~/test$ ls -l
total 16
-rw-r--r-- 1 root root    2 2009-04-30 16:26 file1
-rw-r--r-- 1 root root    2 2009-04-30 16:27 file2
-rw-r--r-- 1 root root    2 2009-04-30 16:27 file2_04302009
-rw-r--r-- 1 root root    2 2009-04-30 16:27 file3
core:~/test$ cat file2
1
core:~/test$ cat file2_04302009
2
core:~/test$ bucp file3 file2
core:~/test$ cat file2_04302009
1
core:~/test$ cat file2
3

So realistic command would be something like...

Code:

bucp /path/to/stage/filename.ext /path/to/prod/filename.txt
Edit:

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'
then you could use copy normally.

colucix 04-30-2009 03:51 PM

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.