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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
11-22-2009, 12:00 AM
|
#1
|
LQ Newbie
Registered: Nov 2009
Posts: 21
Rep:
|
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!
|
|
|
11-22-2009, 12:16 AM
|
#2
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
# printf "%05d" 9
00009
|
|
|
11-22-2009, 12:25 AM
|
#3
|
Member
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Rep:
|
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)
Last edited by bartonski; 11-22-2009 at 12:30 AM.
|
|
|
11-22-2009, 01:00 AM
|
#4
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
or
Code:
for file in eval {00001..$FINISH}
do
mv "test${file}.png" $DIR
done
|
|
|
11-22-2009, 09:55 AM
|
#5
|
Member
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Rep:
|
Quote:
Originally Posted by ghostdog74
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.
|
|
|
11-22-2009, 12:09 PM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
Last edited by colucix; 11-22-2009 at 12:46 PM.
Reason: corrected code
|
|
|
11-22-2009, 03:37 PM
|
#7
|
Member
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Rep:
|
Quote:
Originally Posted by colucix
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 02:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|