Need help with bash


Forum: Programming and Scripting
Topic: Need help with bash
started by: ramdisc

Posted by ramdisc on Oct. 27 2005,15:53
Hello,

I need help writing a piece of bash.

What I am trying to do is get a bash script to target single digit file names and convert them to double digits. Lets say this is what I have in my folder:

1.jpg
10.jpg
11.jpg
12.jpg
2.jpg
20.jpg
3.jpg
4.jpg
5.jpg
1.txt
2.txt

I would like the bash script to rename 1.jpg, 2.jpg, 3.jpg, 4.jpg, and 5.jpg into 01.jpg, 02.jpg, 03.jpg, 04.jpg and 05.jpg. At the same time not mistakening the 11.jpg and 12.jpg as as 1.jpg and 2.jpg

The bash script will only target *.jpg formats so conflicts with 1.txt and 2.txt does not occour.

Okay, this is my newbism:

Code Sample

seq=(`seq 1 9`)
file=(ls $seq.jpg)
if $file
then
for loop in $file
do
mv $loop 0$loop
done


Like I said, my newbism.

How do I make this script functional? Please help.

Thanks in advance.

Posted by SaidinUnleashed on Oct. 27 2005,16:17
moved the the appropriate area.

~J.P.
Posted by cbagger01 on Oct. 27 2005,17:00
Try using the "awk" or "gawk" programs to parse the filename straing and report back the size of the filename in characters.

Search google for

man awk

or maybe

awk guide

awk help

to learn more about how to use the awk command.

Posted by clacker on Oct. 27 2005,17:09
ramdisc, I was able to do it in bash as follows (the whole thing is on a single line):

ls [0-9].jpg | cut -d "." -f1 | while read i ; do mv "$i.jpg"  "0$i.jpg" ; done

ls [0-9].jpg returns only files that start with a single digit and end in .jpg.  This gets piped into cut, which breaks the input lines on the periods and returns field one (the part before the period).  While read i sets up a loop that sets the variable $i to each file's prefix and processes them individually.  Then mv moves the old name to the new name.

You could also then write a similar command line to change 2 number names in to three number names.

Posted by cbagger01 on Oct. 27 2005,17:17
clacker,

You da man!  (or da woman, if appropriate)

No sissy awk scripts need apply.

Posted by mikshaw on Oct. 27 2005,17:42
for i in [1-9].jpg; do mv $i 0$i; done

No sissy pipes need apply :p

Posted by clacker on Oct. 27 2005,18:05
Very nice!  Small, simple, efficient, and it does what you want.

I'll try to spend less time playing with my pipes  :;):

Posted by NotTheMama on Oct. 28 2005,06:31
mikshaw,

I thought you would write it as:
for i in [1-9].jpg; do mv {,0}$i; done

Like your tip in the post: < Bash, Tips for commandline and shell scripts >

But great syntax...... :)

Posted by clacker on Oct. 28 2005,12:29
NotTheMama, thanks for that link.  I was looking for it before but couldn't find it.

So what's the best way to reverse the process?  Remove the leading zero?  I had a complicated mess until I looked at the link from NotTheMama and the Advanced Bash-Scripting Guide and tossed in some parameter substitution.  But I'd bet there's a simpler way:

for i in 0[1-9].jpg; do mv $i ${i#0}; done

I wish I knew all this before when I was renaming files by stripping away the first part (always the same) and it took forever using the rename button in emelfm.

Posted by mikshaw on Oct. 28 2005,15:11
NotTheMama: I would have if the filenames were longer, but "mv {,0}$i" doesn't save any bytes or keystrokes from "mv $i 0$i" when you're working with single-character filenames. It's actually one additional stroke, plus an extra shift, and it's more difficult to read =o)
I'm glad people appreciate my input....i certainly appreciate all the knowledge I get from others here.

Clacker:  I can never remember how the string substitution works.  I recently bugged RobertS to help me out, but i still have to refer to the ABS guide to figure out when to use # or ## or %, etc

Posted by idm909 on Jan. 16 2006,00:33
same thing, less code=
Code Sample
for i in `ls [0-9].jpg`; do mv $i 0$i; done

Posted by idm909 on Jan. 16 2006,00:38
Quote (Guest @ Jan. 15 2006,19:33)
same thing, less code=
Code Sample
for i in `ls [0-9].jpg`; do mv $i 0$i; done

Sorry my bad, carelessly I did not read to end of thread where there were even more succinct versions.
Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.