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! |
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 |
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 ~ |
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. |
^grail is right (as usual). A while loop is better:
Code:
#!/bin/bash Quote:
|
Me, I'd just run over the final target dir and change the trailing tilde to say "_$(date +%F)".
KISS. |
Quote:
Code:
i=0 |
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.
|
@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?
|
Quote:
|
Quote:
|
ahhh .. cool .. I was just a bit lost :)
|
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... |
Yes or even better using a bash 4's hash
Code:
declare -A found |
Quote:
But bash would have a memory limit... |
All times are GMT -5. The time now is 03:09 AM. |