Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
first of all, some background...I am really new to writing BASH scrips...but I find them wonderful...I am trying to write some BASH scrip, but got some problem and no good reference on the web
I have a folder of mp3s, sorter in this way:
/home/user/mp3/artist/albumname/01.mp3
/home/user/mp3/artist/albumname/02.mp3
/home/user/mp3/artist/albumname/03.mp3
...
okay?
but now I want to go through the whole /home/usr/mp3 directory and rename each file like artist_albumname_track#.mp3...
I am wondering couple of things...
1) say I do an ls, it lists the files out...how do I get that into a for loop for that I get to rename them?
3) how does ls distinguish a file from a directory????
3) say I want a variable dirname to have the result of running an ls command (or command in general), how do I do that??
ls > cur writes the result to a FILE, but I want a variable (list) to have the value...
4) for dirname in *
do
cd dirname
done
doesn't work since * would gives directories but as well as files...
i wouldn't use ls for this, i'd use a find statement.
find /home/you/mp3 -iname *.mp3
and then you'll instalty get a very simple list of all mp3's. ls isn't the best for this really.
to test if something is a directory you'd use a test statement like
if [ $filename -d ]
then...
i think that's the right syntax, check the test manpage for confirmation.
watch out for spaces as well if you do soemthing like
for i in `ls /home`
then each time you have a newline OR space it will iterate with that as teh main variable. i saw something on here a while back for how to use entire spaces, have a search around.
persaonlyl i'd say use perl to do this, it's very very similar and no more involved, it'd just make it a lot simpler, as you can pull out the parts of the directory structure so much easier.
Ok, I'll try to write down an example to show you Perl ain't needed for this :-]
We assert all mp3's are under the root /home/user/mp3, and all names of mp3's are to be taken from the subdir's artist/albumname 01.mp3 to produce your "artist_albumname_track#.mp3".
What we need is a command that will find us all files that end on ".mp3". Written out it will look like:
root=/home/user/mp3; find ${root} -type f -name "*.mp3"
Execute this and you'll see the list. The "type -f" isn't really needed int his case, but will come in handy when there are other filetypes dispersed over the search area you don't want to pick up (sockets, for instance).
Now we want to use these names to rename the file. What we do is assign each result of the find query to a variable:
root=/home/user/mp3; find ${root} -type f -name "*.mp3" | while read raw; do
echo ${raw}; done
Execute this and you'll see the list. Not from using "find", but by echoing the contents of the variable.
Each variable looks like "/home/user/mp3/artist/albumname/01.mp3", so we can't use it to rename it to something else. What we need is to split the variable into chewable bits. I like to use arrays' for that:
root=/home/user/mp3; find ${root} -type f -name "*.mp3" | tr "/" " " | while read raw; do m=( ${raw} ); echo ${m[@]}; done
Execute this and you'll see the list. Not from using "find", but by echoing the contents of the array.
Arrays count from zero, and we need parts 3,4 and 5 to rename them mp3's:
root=/home/user/mp3; find ${root} -type f -name "*.mp3" | tr "/" " " | while read raw;
do m=( ${raw} )
echo mv ${raw} ${root}/${raw[3]}_${raw[4]}_${raw[5]}; done
That should do it. If you're convinced this works for you, just remove the last "echo" to commence renaming.
More? Look up the ABS or Bash Advanced Scripting Guide at tldp.org.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.