LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 11-22-2009, 12:00 AM   #1
surfrock66
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Rep: Reputation: 0
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!
 
Old 11-22-2009, 12:16 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Code:
# printf "%05d" 9
00009
 
Old 11-22-2009, 12:25 AM   #3
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
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.
 
Old 11-22-2009, 01:00 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
or
Code:
for file in eval {00001..$FINISH}
do
    mv "test${file}.png" $DIR
done
 
Old 11-22-2009, 09:55 AM   #5
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 11-22-2009, 12:09 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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
 
Old 11-22-2009, 03:37 PM   #7
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by colucix View Post
Code:
mv $(seq -f 'test%05.0f.png' $START $FINISH) $DIR
Very impressive. I didn't know about seq.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
AWK script: moving, copying and renaming files uprjamaja Programming 10 12-05-2006 01:06 PM
moving files that have spaces in variables -bash scripting bhar0761 Programming 10 09-22-2005 07:30 AM
Script For Moving Files In Subdirectories darkmage Linux - General 3 12-17-2002 06:34 PM


All times are GMT -5. The time now is 12:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration