LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Question about moving a range of files with a Bash script (https://www.linuxquestions.org/questions/linux-newbie-8/question-about-moving-a-range-of-files-with-a-bash-script-770723/)

surfrock66 11-22-2009 12:00 AM

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!

ghostdog74 11-22-2009 12:16 AM

Code:

# printf "%05d" 9
00009


bartonski 11-22-2009 12:25 AM

try

Code:

mkdir $DIR
for i in  { 1..$FINISH }; do
    file=$(printf "test%05d.png" $i)
    mv $file $DIR
done

(note that '{ 1..$FINISH }' isn't portable)

ghostdog74 11-22-2009 01:00 AM

or
Code:

for file in eval {00001..$FINISH}
do
    mv "test${file}.png" $DIR
done


bartonski 11-22-2009 09:55 AM

Quote:

Originally Posted by ghostdog74 (Post 3765426)
or
Code:

for file in eval {00001..$FINISH}
do
    mv "test${file}.png" $DIR
done


That didn't work for me. I tried

Code:

#! /bin/bash
for file in eval {00001..00050}
do
    echo "test${file}.png"
done

and got

Code:

testeval.png
test1.png
test2.png
test3.png
test4.png
test5.png
test6.png
test7.png
test8.png
test9.png
test10.png
test11.png

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.

colucix 11-22-2009 12:09 PM

An alternative using seq:
Code:

mv $(seq -f 'test%05.0f.png' $START $FINISH) $DIR
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:
Code:

function mvpng () {
  mv $(seq -f 'test%05.0f.png' $1 $2) $3
}

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.

bartonski 11-22-2009 03:37 PM

Quote:

Originally Posted by colucix (Post 3765887)
Code:

mv $(seq -f 'test%05.0f.png' $START $FINISH) $DIR

Very impressive. I didn't know about seq.


All times are GMT -5. The time now is 11:23 AM.