Question about moving a range of files with a Bash script
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.
Question about moving a range of files with a Bash script
Greetings, I'm new to bash scripting. I am writing a script to help me in making gif images from a large video. I've written bash code to successfully take an input video, output 2 things: a video at the desired output size with 10 fps, and a png for each frame in that video (let's say, 59000 images) then the script watermarks all of those images.
The images are named, say, test00001.png - test59704.png
Now, seeing as Nautilus chokes and dies a firey death on a folder with that many files, I want to be able to have a command where I move a subset of files to another folder. I see it taking the start frame number, the end frame number, and the folder name, and automatically running a for loop moving them all. I understand how to do that in theory, but in making my for loop, how can I preserve my incrementing number to have 5 digits?
Let's say I input $START $FINISH and $DIR
Code:
mkdir $DIR
for ((i=$START;i<=$FINISH;i+=1)); do
mv "test"$i".png" $DIR"/test"$i".png"
done
I'm not even sure if that would work, but before I can test it, the problem would be if I want to copy frames 9-200, the script would look for "test9.png" instead of "test00009.png" and I don't know how to force 5 digits. I thought about doing some sort of conditional switch testing the range of the value, but that seemed inelegant. Any help is appreciated!
Ah. I see, it's counting 'eval' as the first element in the list. You can ditch the eval, and get the same result, minus 'testeval.png'.
I think perl will do something with the leading zeros in such a context, but in shell scripts, anything that looks like a number will be turned in to a number at the parsing stage, thus you need printf to format a number in to a string with leading 0s after the fact.
This does not require a for loop, since all the filenames are generated on the command line, using the specified format. The only limit could be the long argument list (the shell will complain about that, without actually moving anything). If this is the case, a for loop - as those ones suggested above - is the suitable solution.
You can also create a function, accepting three arguments: the start number, the end one and the destination directory:
This manages only fixed file names, but you can develop the function adding some checks and trying to manage different file names, as well. Just to give you an idea.
Last edited by colucix; 11-22-2009 at 12:46 PM.
Reason: corrected code
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.