LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-22-2005, 10:03 AM   #1
mad_juno
LQ Newbie
 
Registered: Jul 2005
Posts: 3

Rep: Reputation: 0
bash scripting: problem with generating filenames


Before the problem: I am completely new to bash (been writing it for about 2 weeks), and now I have been stuck with this problem for more than 3 hours now, though most probably the solution is elementary...

The problem: I need to rename files so that the names would be 001 002 003 ... 010 ... 011 ... 198 etc. (all the names must be 3 characters long, yep, i'm sure there wount be more than 999 files)

It is obvious I just have to have a variable that has a 1 added to it in every step of the loop, but what do I do to get the zeros in the beggining of the variable outputed? Note that the number of '0' changes so I can't do like this:
/whatever/whatever/"0${i}"

Can somebody PLEASE tell me how to generate the numbers (the '0' at the beggining of the finenames are important)? I could easily do this in c++, but now I feel completely stuck... I just can't think of a good way to do this in bash... PLEASE HELP!

Last edited by mad_juno; 07-22-2005 at 10:09 AM.
 
Old 07-22-2005, 10:33 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

You can use seq to create the (zero padded) numbers:

seq -f "%03g" 8 12
008
009
010
011
012

Hope this helps.
 
Old 07-22-2005, 10:57 AM   #3
mad_juno
LQ Newbie
 
Registered: Jul 2005
Posts: 3

Original Poster
Rep: Reputation: 0
seq --format=%03g 1 10
worked for me!

THANK YOU SO MUCH!!!
 
  


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 scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
bash scripting problem Sammy2ooo Linux - General 3 12-13-2004 12:31 PM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM
BASH scripting problem deadlock Programming 5 08-15-2003 04:33 AM
bash scripting problem raven Programming 7 03-10-2003 05:48 PM


All times are GMT -5. The time now is 05:14 AM.

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