Results 1 to 4 of 4
I know it's a question that is asked often, but I still can't figure out a solution for my specific situation.
I have a directory full of files with names ...
- 04-09-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 2
File renaming
I know it's a question that is asked often, but I still can't figure out a solution for my specific situation.
I have a directory full of files with names like these (These are the actual file names: note that they include spaces):
1 - Light My Fire
2 - Crystal Ship
....
10 - People Are Strange
For my MP3 player to play these files in the correct order, they need to be sorted alphabetically. So I need leading 0's on the file names beginning with 0 through 9. In other words, I want:
01 - Light My Fire
02 - Crystal Ship
...
10 People Are Strange
I have spent a couple of days playing with find -exec and various forms of xargs, and am still having no luck.
Any advice?
Thanks.
- 04-09-2010 #2
Did you try rename command -- man page says
rename will rename the specified files by replacing the first occur-
rence of from in their name by to.
-V, --version
Display version information and exit.
For example, given the files
foo1, ..., foo9, foo10, ..., foo278, the commands
rename foo foo0 foo?
rename foo foo0 foo??
will turn them into foo001, ..., foo009, foo010, ..., foo278.
And
rename .htm .html *.htm
will fix the extension of your html files.
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 04-09-2010 #3Linux Newbie
- Join Date
- Sep 2004
- Location
- UK
- Posts
- 160
Starting point, you should be able to turn that into a script.Code:ls > /tmp/tmp.lst cat /tmp/tmp.lst | while read a b do i=`printf "%03d" $a` echo "mv \"$a $b\" \"$i $b\"" done
In a world without walls and fences, who needs Windows and Gates?
- 04-10-2010 #4Just Joined!
- Join Date
- Apr 2010
- Posts
- 2
Thanks
Thanks to both of you who responded. I tried the rename command, but couldn't figure out a perl expression to do exactly what I wanted. (I'm totally new at this.)
I did manage to adapte the shell script, and it works great. Just in case anybody else finds this thread, here's what I did to it:
Thanks again.ls | grep "^[0-9] -"> /tmp/tmp.lst
cat /tmp/tmp.lst | while read a b
do
i=`printf "%02d" $a`
mv "$a $b" "$i $b"
done


Reply With Quote