LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 05-24-2016, 11:03 AM   #1
kilee
LQ Newbie
 
Registered: Nov 2009
Posts: 22

Rep: Reputation: 0
Backup adding numbers when using mv command


Hello
I'm moving all zip files I have into a single folder with this:

find / -iname "*.zip" -type f -exec mv {} /Volumes/500/zip/ \;

Works perfect, but I'd like to make backups in case of name conflicts. -b looks like the answer, but the backups add a ~ symbol to the extension.

file.zip
file.zip~ and so on.

Is it possible to number the files before the extension?

file.zip
file1.zip
file2.zip

Thanks!
 
Old 05-24-2016, 11:28 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,567
Blog Entries: 19

Rep: Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448
Yes, it is possible. You would need to write a script containing a for loop. I'm not quite sure of the syntax but it would be something like
Code:
number=1
for file in `find / -iname "*.zip" -type f`
do
mv $file `echo ${file%%".zip"}$number.zip`
number=$number+1
done
The echo command should strip the suffix off, insert a number, and put the suffix back on again.
 
Old 05-24-2016, 11:29 AM   #3
dab1414
Member
 
Registered: May 2011
Location: OK, USA
Distribution: Slackware 14.1_64
Posts: 76

Rep: Reputation: 52
http://linux.die.net/man/1/mv

add "--backup=t" for numbered backups

EDIT: sorry this still puts number behind extension. "mv --suffix=.bak" will get rid of the ~

Last edited by dab1414; 05-24-2016 at 11:40 AM.
 
Old 05-24-2016, 11:39 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You should not use a for loop if there is any possibility of word splitting occurring. Simply change to a while loop and you should be fine.

You also need to include some sort of test for the repeated file names as simply adding an ever increasing number will also give a numbered copy to something that only exists once.
 
Old 05-24-2016, 12:44 PM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
^grail is right (as usual). A while loop is better:
Code:
#!/bin/bash

i=1
while read filename; do
    echo ${filename%%".zip"}$i.zip
    ((i++))
done < <(find . -iname "*.zip")

exit 0
Also:
Quote:
Originally Posted by grail
You also need to include some sort of test for the repeated file names as simply adding an ever increasing number will also give a numbered copy to something that only exists once.
 
Old 05-24-2016, 06:59 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Me, I'd just run over the final target dir and change the trailing tilde to say "_$(date +%F)".
KISS.
 
2 members found this post helpful.
Old 05-26-2016, 02:21 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by HMW View Post
^grail is right (as usual). A while loop is better:
Code:
#!/bin/bash

i=1
while read filename; do
    echo ${filename%%".zip"}$i.zip
    ((i++))
done < <(find . -iname "*.zip")

exit 0
I think it is cleaner as:
Code:
i=0
find . -iname "file*.zip" | while read filename; do
   ((i++))
done
if ((i)) ; then
    target=${file%%".zip"}$number.zip
else
    target=file.zip
fi
But then that is just me.
 
Old 05-26-2016, 03:31 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
It should be mentioned that using 'mv' is ballsy because many many things can go wrong and cause you headaches. You shouldn't be using 'mv' unless you already have a backup, or don't care much about the data. Making a backup, copying files from one place to another, verifying, double-checking, and only then deleting, is a much more reliable route.
 
Old 05-26-2016, 03:33 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@jpollard - I am not quite understanding your code? You are counting all the files that are duplicates and then seeing if positive you set a variable ... at what point are you going to do the copy/move?
 
Old 05-26-2016, 04:26 AM   #10
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,149

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Wink

Quote:
Originally Posted by szboardstretcher View Post
It should be mentioned that using 'mv' is ballsy because many many things can go wrong and cause you headaches. You shouldn't be using 'mv' unless you already have a backup, or don't care much about the data. Making a backup, copying files from one place to another, verifying, double-checking, and only then deleting, is a much more reliable route.
Agree with this one, do an rsync to the destination folder once done then delete the source. Safer way. KISS method.
 
Old 05-26-2016, 05:43 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by grail View Post
@jpollard - I am not quite understanding your code? You are counting all the files that are duplicates and then seeing if positive you set a variable ... at what point are you going to do the copy/move?
Afterwards. The snippit is more just to identify the possible name.
 
Old 05-26-2016, 09:22 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
ahhh .. cool .. I was just a bit lost
 
Old 05-26-2016, 01:22 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Ran across another way... using shell array variables.

Instead of counting the list, if the list of names is put into an array you can get the size of the array...
 
Old 05-26-2016, 04:15 PM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes or even better using a bash 4's hash
Code:
declare -A found
destdir=/Volumes/500/zip

find . -iname "file*.zip" | while read filename; do
  name=$(basename $filename)
  target=${name%%".zip"}${found["$name"]}.zip
  ((found["$name"]++))
  echo "mv $filename $destdir/$target"
done

Last edited by keefaz; 05-26-2016 at 04:19 PM.
 
Old 05-26-2016, 05:03 PM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by keefaz View Post
Yes or even better using a bash 4's hash
Code:
declare -A found
destdir=/Volumes/500/zip

find . -iname "file*.zip" | while read filename; do
  name=$(basename $filename)
  target=${name%%".zip"}${found["$name"]}.zip
  ((found["$name"]++))
  echo "mv $filename $destdir/$target"
done
Well... that would work - depending on the total number of files. Using perl would be unbounded (since the hash can be a disk file instead of memory resident). Not sure about python, but I believe it can be unbounded as well.

But bash would have a memory limit...
 
  


Reply



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
Adding bitwise two numbers modulo 2. stf92 Programming 8 06-08-2013 11:24 AM
error in script for adding two numbers krap Linux - Newbie 3 08-09-2008 01:05 PM
Javascript adding two numbers. antis Programming 2 07-24-2008 05:53 AM
adding binary numbers in java please help!! trscookie Programming 9 02-28-2006 03:16 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM

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

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