LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2011, 03:17 AM   #1
zookman
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
Help with bash script to rename multiple files


Hi, can anyone help me with a bash script to give sensible names to a large number of photos. I hope to be able to run a script with an argument which will become the filename followed by a number beginning at 1. I'm hoping for something that works along the lines of

Code:
./file_rename.sh Summer2009_

Summer2009_0001
Summer2009_0002
Summer2009_0003
I'm new to Linux and have so far managed to write the following script which doesn't quite produce the result I'm looking for.

Code:
#!/bin/bash
#file_rename.sh

FILE_LIST=`ls`
a="1"

for i in $FILE_LIST; do
  mv $i $1_$a.JPG
  a=$[$a+1]
done

exit 0
The problem with this is that the files are then listed in the following order:

Summer2009_1
Summer2009_10
Summer2009_2

Any help would be much appreciated.

Thanks.
 
Old 05-09-2011, 03:23 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
You need some utility which is able to print out zero padded numbers. For example using printf you can try something like this:
Code:
for i in $FILE_LIST
do
  echo mv $i $(printf "%s_%03d.jpg" $1 $a)
  ((a++))
done
Hope this helps.
 
1 members found this post helpful.
  


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
bash - rename multiple files with dots in filename eldude543 Programming 20 05-20-2016 11:17 PM
Another script to rename multiple files Becklan Programming 9 08-10-2010 09:18 AM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Rename multiple files in Bash damianpfister Programming 5 11-15-2009 08:30 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:02 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration