'ls' .. or something to identify filenames that have a (space)(numeral) pattern
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
'ls' .. or something to identify filenames that have a (space)(numeral) pattern
I have a bunch of files named ...file 7.mp3, file 8.mp3, file 9.mp3, file 10.mp3, file 11.mp3 ... etc.
I want to find a way to list only those files that have a 1 digit numeral, so I can write a script to rename them to ~07.mp3, ~08.mp3, etc. for better sorting.
I've been messing around with ls for quite a while without getting it right.
I had to do a bit of this myself a while back. I used rename to do the work:
Code:
rename " " " 0" *\ [0-9].mp3
That command assumes a space occurs immediately before the single-digit number and that there is only one space in the filename.
Of course, you can replace the "mp3" with another * if you have other extensions, but be careful... it could throw things off if any of the filenames have periods as part of the name.
Last edited by Dark_Helmet; 05-05-2006 at 10:13 PM.
Thanks a lot. The "[0-9]" was the part I couldn't get right. Should have thought about grep syntax. My files have more than one space in the name, but I know how to get the script done now.
Well, I thought I knew how to do this... All the files I'm interested in are in a 'nested' directory. Let's say DirLevel0, DirLevel1, and DirLevel2. The actual files are all the bottom directory.
Here's my script:
Code:
#!/bin/sh
SONG=0
ALBUMTITLE="Album01"
for file in DirLevel0/*/*/*\ [0-9].mp3
do ALBUMKEY=${file:38:7}
if test $ALBUMTITLE=$ALBUMKEY; then SONG=$(($SONG+1))
else SONG=1
fi
ALBUMTITLE=$ALBUMKEY
file1=${file//\ ?.mp3/\ 0$SONG.mp3}
# echo $file
# echo $file1
# mv "$file" "$file1"
done
The problem seems to be in the 'if' statement. All the correct files are being selected, but when the 'ALBUMKEY' changes, the counter (SONG) keeps on incrementing. It never goes back to 1.
Just in case it's not obvious, I am a real novice at scripting, and I hope someone can point out my obvious error while I'm sleeping. Thanks.
I changed the script to match up with my regular formatting...
Code:
#!/bin/sh
SONG=0
ALBUMTITLE="Album01"
for file in DirLevel0/*/*/*\ [0-9].mp3
do
ALBUMKEY=${file:38:7}
if [ "${ALBUMTITLE}x" = "${ALBUMKEY}x" ] ; then
((SONG=SONG+1))
else
SONG=1
fi
ALBUMTITLE=${ALBUMKEY}
file1=${file//\ ?.mp3/\ 0$SONG.mp3}
# echo $file
# echo $file1
# mv "$file" "$file1"
done
The script seems to work as I expect, but I don't know exactly what the file setup is.
The SONG counter will reset when ALBUMKEY changes. So double-check that your ALBUMKEY assignment is doing what you expect. Does the text you want for ALBUMKEY always start at the 38th character (including the path)?
Last edited by Dark_Helmet; 05-07-2006 at 12:15 AM.
Does the text you want for ALBUMKEY always start at the 38th character (including the path)?
Yes, but that will change whan I get out of my testing environment, and into the real directories. With some very minor twiddling, this script is now working. There does seem to be an extraneous "x" in your 'if test' statement. Thanks again.
What I'm working on is converting a 64 cd set of the King James Bible. I'm only about half way thru the process right now. Since you've looked at this a couple times, maybe you (or someone) could suggest a means of getting the leading 0 in there from the beginning. I am ripping the files to .wav, then running this script to encode and name them.
Code:
#!/bin/sh
MP3NO=1
for file in tmp/*.wav
do lame -m m -V 7 "$file" "tmp/Holy Bible (KJV-Jon Sherberg) OT20 Proverbs $MP3NO.mp3"
MP3NO=$(($MP3NO+1))
done
Actually, there was a missing 'x' in the if statement Which I've now added. Adding the 'x' on each argument guarantees no mysterious comparison failures. That is, if ALBUMTITLE or ALBUMKEY ever evaluate to an empty string, the comparison won't croak. Without the 'x' (or some other letter/number), an empty string will cause the script to fail with an "expected two arguments" error or something similar.
As for the second script, that's easy. You just need a new if statement and another variable:
Code:
#!/bin/sh
MP3NO=1
for file in tmp/*.wav
do
if [ ${MP3NO} -lt 10 ] ; then
MP3NO_TEXT="0${MP3NO}"
else
MP3NO_TEXT="${MP3NO}"
fi
lame -m m -V 7 "$file" "tmp/Holy Bible (KJV-Jon Sherberg) OT20 Proverbs $MP3NO_TEXT.mp3"
MP3NO=((MP3NO+1))
done
There may be a more elegant way of doing the same thing, but that works.
Last edited by Dark_Helmet; 05-07-2006 at 12:25 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.