LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 06-21-2016, 12:52 PM   #1
biosboy4
Member
 
Registered: Aug 2015
Distribution: Devuan/LFS
Posts: 239

Rep: Reputation: 38
cp auto rename


Hello,

I'm writing a little script that will just cp files from one place to another. Since they will always be the exact same filename and I don't want to ever overwrite, I've been looking for a good way to autorename the files but I've been reading conflicting info.

How do you guys do it?
 
Old 06-21-2016, 01:24 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Write a script that checks if the destination file name already exists, and if so modify the destination by appending a number, date, etc. onto the name.
 
1 members found this post helpful.
Old 06-21-2016, 01:29 PM   #3
BCarey
Senior Member
 
Registered: Oct 2005
Location: New Mexico
Distribution: Slackware
Posts: 1,619

Rep: Reputation: Disabled
Have you tried the --backup=t option?

Brian
 
2 members found this post helpful.
Old 06-21-2016, 02:03 PM   #4
biosboy4
Member
 
Registered: Aug 2015
Distribution: Devuan/LFS
Posts: 239

Original Poster
Rep: Reputation: 38
something like:

Quote:
cp --backup=t /dir1/filename /dir2/whateverfolder
right?

From what I just read, this should number the files as they drop in. right?
 
Old 06-21-2016, 03:54 PM   #5
biosboy4
Member
 
Registered: Aug 2015
Distribution: Devuan/LFS
Posts: 239

Original Poster
Rep: Reputation: 38
Quote:
Write a script that checks if the destination file name already exists, and if so modify the destination by appending a number, date, etc. onto the name.
Could you give me an example?
 
Old 06-21-2016, 04:00 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Quote:
Originally Posted by biosboy4 View Post
Could you give me an example?
eg:
Code:
dst=$src
adder=0
odst=$dst
while [[ -f "$dstdir/$dst" ]]; do
  dst=${odst}_$adder
  ((adder++))
done

cp $srcdir/$src $dstdir/$dst
Or similar, in a loop over input files.

That one just uses a counter, but you could use whatever convention you like for preventing naming conflicts.


There's also the other way of approaching the problem...ALWAYS copy to a unique name, but first check to see if the file has changed since the previous copy was run, and if not, hard-link the old copy to the new name instead of copying it over fresh. That way you have a completely independent and separate copy of all of your files from each time the backup was run, but it only uses the disk space required for the files that changed since the last time it was run. This is what rsync's --link-dest argument does. That is assuming your objective with this is some kind of versioned backup system. It all depends on your goals.

Last edited by suicidaleggroll; 06-21-2016 at 04:07 PM.
 
Old 06-21-2016, 05:38 PM   #7
BCarey
Senior Member
 
Registered: Oct 2005
Location: New Mexico
Distribution: Slackware
Posts: 1,619

Rep: Reputation: Disabled
Quote:
Originally Posted by biosboy4 View Post
something like:



right?

From what I just read, this should number the files as they drop in. right?
Yes, you'll of course need the -r option as well (assuming you are copying directories).
 
Old 06-21-2016, 11:23 PM   #8
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,345

Rep: Reputation: 226Reputation: 226Reputation: 226
How about this command below, try it first with a dummy data.

Quote:
cp -ra /home/production/Test1 "/mnt/backup_prod/Test2-$(date +"%m-%d-%y-%r")"
 
Old 06-22-2016, 10:10 AM   #9
biosboy4
Member
 
Registered: Aug 2015
Distribution: Devuan/LFS
Posts: 239

Original Poster
Rep: Reputation: 38
Quote:
cp -ra /home/production/Test1 "/mnt/backup_prod/Test2-$(date +"%m-%d-%y-%r")"
This worked perfect! This atomatically copies the directory and timestamps it into the folder name. Freaking Perfect. Thank you!
 
Old 06-22-2016, 09:58 PM   #10
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,345

Rep: Reputation: 226Reputation: 226Reputation: 226
Cool

Quote:
Originally Posted by biosboy4 View Post
This worked perfect! This automatically copies the directory and timestamps it into the folder name. Freaking Perfect. Thank you!
I'm glad I could help, thanks for the update.
 
Old 06-23-2016, 11:38 AM   #11
biosboy4
Member
 
Registered: Aug 2015
Distribution: Devuan/LFS
Posts: 239

Original Poster
Rep: Reputation: 38
Ok, this is working great, however I would like to add more uniqueness to the foldernames. This way I can tell which machine the files came from.

How might one combine a rename functionality of this sort:

Quote:
for filename in *foo*; do mv "$filename" "${filename//foo/bar}"; done
Note that I would like to assign a specific machine identifier to the folder name, not the filenames.


with this sort:

Quote:
cp -ra /home/production/Test1 "/mnt/backup_prod/Test2-$(date +"%m-%d-%y-%r")"
I'm just going to separate the scripts so that each script pulls the data from only one machine, therefore I can statically assign the unique machine numbers/names to each sript. I will also need the timestamps though to keep from overwriting data previously pulled from that machine.
 
  


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
Rename network interface (Rename ppp0 ...) Nicolas1390 Linux - Newbie 2 08-08-2011 03:14 AM
Is there a daemon to auto rename a file? xmrkite Linux - Software 3 09-16-2010 11:58 AM
USB pen drives not auto prompting or auto mounting on KDE3.5 (PCLOS) k.king Linux - General 1 03-17-2009 04:16 PM
auto file rename based on time wx_jason Linux - Newbie 10 07-10-2003 12:14 PM
auto rename contents of a DIR bkeating Linux - General 1 11-05-2002 07:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:58 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