LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 01-08-2017, 05:47 PM   #1
StepNjump
Member
 
Registered: Jul 2011
Posts: 32

Rep: Reputation: Disabled
copying files in terminal and keep older versions


Recently, I just learned how to write simple shell scripts. One of them simply duplicate certain files on my system that happen to be crucial to me for backup purposes.

cp /dir/dir/file1 /samedir/samedir/file1.bak
My scripts gets executed every time I sign on the system. However it always overwrites file1.bak, which is not intended nor wanted. I would like to find a way to keep all created files separated. Maybe something like file1(1).bak, file1(2).bak or something of the like. Strangely enough, that works great when copying and pasting in X, but not by using the cp command.

VMS was great for certain things, namely that every time we would write to a file called file.bak, it would keep all previous versions by adding a tailing ':' followed by the version number of that file at the end of the file ie: file.bak:1, file.bak:2, file.bak:521, etc...

We used to get rid of previous versions with the purge command purge file.bak

Any suggestions ?

Thanks,


StepNjump
 
Old 01-08-2017, 06:08 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
man cp
Code:
       --backup[=CONTROL]
              make a backup of each existing destination file

       -b     like --backup but does not accept an argument
pseudo code
Code:
if [[ -f filename.bak ]] ; then 
cp source destanation/filename(1).bak
fi
but it is a little more complicated if you already have a filename(1).bak ...
http://www.tldp.org/LDP/abs/html/fto.html

you could use this. Just modify it to your needs:

This function searches the directory and finds where I left off at. returning that highest number to a file I added a numbering system like you're talking of. filename(1).bak filename(2).bak etc


Code:
#!/bin/bash

countnum="$(find /home/userx/testscript/lost -name "*.mp3" | wc -l )"

FILES="$(cd /home/userx/testscript/lost/ && ls *.mp3)"

MAXNUM="$(find ~/testscript/lost -type f -name "*.mp3" | wc -l )"
typeset -i i
i=0

 function getLastNum()
 {
for file in $FILES 
do

echo $file
      # nstring=${file##*/} #gets just the file name
      
       lstring=${file##*"("} #chops off Lside to the ( leaving num )
       rstring=${lstring%%")"*} #chops off Rside to the ) leaving the num
  
       arr[$i]=$rstring
       (( i++ ))
  done  


 for (( a=0; a <  $countnum ; a++ )) ; do

       if (( arr[a+1] < arr[a] )); then
                store=arr[$a]
		arr[$a]=arr[$a+1]
		arr[$a+1]=$store
                BigNum=$store
       fi
done



return $BigNum

}
getLastNum
as such
Code:
userx@voider~:>> nstring="noere(1)"
userx@voider~:>> lstring=${nstring##*"("}
userx@voider~:>> echo $lstring
1)

userx@voider~:>> rstring=${lstring%%")"*} 
userx@voider~:>> echo $rstring
1
now you will know where you left off at.

Code:
lastNum="$(( $BigNum+1 ))"
gives you the next number to use.


another snippet of my script to give you an idea on how to get the just the file name then add the number to the next file. using basename and dirname too can be used instead if you like.

Code:
 c=$FILENAME
 
 xpath=${c%/*} 
 
 xbase=${c##*/}
 xfext=${xbase##*.}
 xpref=${xbase%.*}
 path=${xpath}
  pref=${xpref}
 ext=${xfext}
 

newFile="$pref-(Lost-Song-Number-("$lastNum")).mp3"

Manipulating Strings

Bash String Manipulation Examples – Length, Substring, Find and Replace

Last edited by BW-userx; 01-08-2017 at 07:13 PM.
 
Old 01-09-2017, 06:48 AM   #3
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
Yea VMS! Append date? Append :#++ Idk cpv x -> x:1 then x:2 then x:3 ...
man cpv: ToBeWritten by LQscriptGuru. Works like VMS, incr :<ver#> if dest exists.

Last edited by Jjanel; 01-09-2017 at 11:15 PM.
 
Old 01-09-2017, 07:07 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Seems like you need a solid
Code:
cp /dir/{file1,file1.bak}
pre-flight:
Code:
echo cp /dir/{file1,file1.bak}

Last edited by Habitual; 01-09-2017 at 03:18 PM.
 
Old 01-09-2017, 02:28 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
how about
Code:
cp file file.$(date +%s).bak
?

and:
Code:
man date
(funny, that reads like mandate).
 
  


Reply

Tags
backup, copying, versions



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
Equivalent to --remove-source-files for rsync in older versions of rsync anon091 Linux - Server 7 08-11-2016 10:30 AM
Can I delete files in trash that is older than 10 days with a terminal command? linustalman Ubuntu 3 09-29-2010 04:19 AM
/etc/hosts & /etc/services files reverting to older versions after reboot. Tantalis Slackware 4 07-13-2009 05:59 PM
Copying files in terminal theseph Linux - Newbie 65 12-18-2008 02:07 PM
copying a lot of files with bash or terminal. rob-n Linux - Newbie 3 04-13-2004 06:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 12:20 PM.

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
Open Source Consulting | Domain Registration