LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2009, 03:26 PM   #1
Joe_P
LQ Newbie
 
Registered: Apr 2009
Posts: 1

Rep: Reputation: 0
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.
 
Old 04-30-2009, 04:12 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 04-30-2009, 04:13 PM   #3
janhe
Member
 
Registered: Jul 2007
Location: Belgium
Distribution: slackware64 13.1, slackware 13.1
Posts: 369

Rep: Reputation: 49
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

Last edited by janhe; 04-30-2009 at 04:16 PM.
 
Old 04-30-2009, 04:33 PM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by Joe_P View Post
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.

Last edited by rweaver; 04-30-2009 at 04:51 PM. Reason: disclaimer, note, builtin
 
Old 04-30-2009, 04:51 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 11:52 AM
Gnome/Nautilus issues Beagle-Tracker, file transfer info, replace file info, popup. Mysticle31 Linux - Software 0 01-08-2008 06:30 PM
how to rename a file and copy a file in a shell zach014 Linux - Newbie 6 11-23-2006 10:23 AM
How can i Rename file? Khmer Linux - Newbie 4 10-21-2005 09:09 AM
How to rename a file? juanb Linux - General 4 03-19-2003 12:20 PM


All times are GMT -5. The time now is 06:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration