LinuxQuestions.org
Review your favorite Linux distribution.
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-06-2008, 08:09 PM   #1
rickenbacherus
Member
 
Registered: Mar 2003
Location: colorado springs. colorado
Distribution: Debian-Sid 2.6.24-rt1
Posts: 290

Rep: Reputation: 30
BASH - convert single digits to double digits.


I have a directory of files like this:
Code:
1.ogg
2.ogg
3.ogg
10.ogg
11.ogg
How to convert it to this:
Code:
01.ogg
02.ogg
03.ogg
10.ogg
11.ogg
 
Old 05-06-2008, 08:16 PM   #2
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
My scripting is rubbish, but something like

for i in $(ls ?.ogg); mv $i "0$i"; done
 
Old 05-06-2008, 10:48 PM   #3
rickenbacherus
Member
 
Registered: Mar 2003
Location: colorado springs. colorado
Distribution: Debian-Sid 2.6.24-rt1
Posts: 290

Original Poster
Rep: Reputation: 30
Looks like printf is the tool to use here if I can just get the syntax right.
 
Old 05-06-2008, 11:28 PM   #4
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
I think my code should be

for i in $(ls ?.ogg); mv "$i" 0"$i"; done
 
Old 05-07-2008, 01:52 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
You are missing a "do" and the ls is unnecessary:

Code:
for i in ?.ogg; do mv $i 0$i; done
 
Old 05-07-2008, 02:00 AM   #6
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Ah yes - I did say I'm rubbish at scripting

Does work though rickenbacherus (either mine with the do or jillagre's without the quotes)
 
Old 05-07-2008, 02:40 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by rickenbacherus View Post
Looks like printf is the tool to use here if I can just get the syntax right.
The loop by billymayday revised by jlliagre works fine. Anyway for other uses the syntax of the printf command is
Code:
printf "%02d.ogg" number
where the format is C-style and the %02d is intended to pad with zeros. If you simply use %2d the one-digit numbers will have a starting blank space.
 
Old 05-07-2008, 06:53 AM   #8
rickenbacherus
Member
 
Registered: Mar 2003
Location: colorado springs. colorado
Distribution: Debian-Sid 2.6.24-rt1
Posts: 290

Original Poster
Rep: Reputation: 30
Ah perfect - all good solutions. Thanks!
 
  


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 script to preserve only first digits alenD Linux - Software 6 04-14-2008 12:51 PM
grep and digits gawain Linux - Software 7 02-20-2008 02:13 AM
How to format a long double number in C++ to display as many digits as possible. TruongAn Programming 4 11-09-2005 10:11 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
input single integer value, output the value as separate digits hubabuba Programming 2 03-02-2005 10:42 AM


All times are GMT -5. The time now is 06:45 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