Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (3) </ [1] 2 3 >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: Need help with bash< Next Oldest | Next Newest >
ramdisc Offline





Group: Members
Posts: 1
Joined: Oct. 2005
Posted: Oct. 27 2005,15:53 QUOTE

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.
Back to top
Profile PM 
SaidinUnleashed Offline





Group: Members
Posts: 1428
Joined: Mar. 2004
Posted: Oct. 27 2005,16:17 QUOTE

moved the the appropriate area.

~J.P.

--------------
They say if you play a Microsoft CD backwards, you hear satanic messages. That's nothing, cause if you play it forwards, it installs Windows.

Unleash the power of the TILDE~~~
Back to top
Profile PM AOL MSN YIM 
cbagger01 Offline





Group: Members
Posts: 4264
Joined: Oct. 2003
Posted: Oct. 27 2005,17:00 QUOTE

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.
Back to top
Profile PM 
clacker Offline





Group: Members
Posts: 570
Joined: June 2004
Posted: Oct. 27 2005,17:09 QUOTE

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.
Back to top
Profile PM 
cbagger01 Offline





Group: Members
Posts: 4264
Joined: Oct. 2003
Posted: Oct. 27 2005,17:17 QUOTE

clacker,

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

No sissy awk scripts need apply.
Back to top
Profile PM 
11 replies since Oct. 27 2005,15:53 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (3) </ [1] 2 3 >/
reply to topic new topic new poll
Quick Reply: Need help with bash

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code