Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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?
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.
@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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.