Script to reclusively find all media (mp3) files and make a list
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
Script to reclusively find all media (mp3) files and make a list
Okay what I need to do is make a list with full path names e.g. /home/t/file.mp3 of all *.mp3 files in all sub-directories of (for simplicity sake) my working directory...
I have gotten some luck with this:
Code:
find `pwd` -name "*.mp3" >> ~/list.txt
But for some reason it list ALL files in in the search and doesn't filter anything....
I have also tried many additions to the above like
Okay what I need to do is make a list with full path names e.g. /home/t/file.mp3 of all *.mp3 files in all sub-directories of (for simplicity sake) my working directory...
I have gotten some luck with this:
Code:
find `pwd` -name "*.mp3" >> ~/list.txt
That one should work, as long as you correctly quote the pattern, like you did in that example above. Also, there's no need to use pwd, just use the dot to mean "the current working dir".
Code:
find . -name "*.mp3" > ~/list.txt
Note that you are using >>, so, if you run this multiple times the contents will append... Maybe that's the problem. Maybe you ran another find command with a bigger output before that one that you posted. If that's the case, either make sure that the list.txt files doesn't exist, or use a single > to overwrite it.
Off the top of my head, i think something like:
grep -i ".mp3" || ls */*
should do the trick!
I'm trying to think of a polite way of pointing out that command doesn't even make sense let alone do what the OP wants but all I can up with is this...
Of course there is no requirement that MP3 files have a .mp3 file "extension" since a the name of a Linux file does not determine the type of the file. That's done by the "Magic Number" inside the file itself.
I think that you probably want something like find . -print0 | xargs -r0 file | grep MPEG | cut -f1 -d:, although that command doesn't check that Type is III, so it will list all your MPEG files, not just the MP3 ones.
Here's a sample output with the level III test included:
I'm trying to think of a polite way of pointing out that command doesn't even make sense let alone do what the OP wants but all I can up with is this...
That command will do the following:
Open an input buffer, which you can feed via stdin (using the keyboard). That what grep does when it's not fed via a file on command line, or via a pipe (probably what the poster thought it would do, if it wasn't because the pipe is not ||, but a single |, and the order of the members of the should-be-pipe are reversed as well).
Once you press enter, the console keeps waiting for your input
You write, and press control+d when you are done
Now, if you wrote "mp3" somewhere on your input, nothing will happen, if you did not, then ls will be run.
As you see, completely absurd for the purpose of this thread.
He probably meant:
Code:
ls */* | grep -i mp3
It's much less versatile than find so I wouldn't bother with that either way.
I think that you probably want something like find . -print0 | xargs -r0 file | grep MPEG | cut -f1 -d:, although that command doesn't check that Type is III, so it will list all your MPEG files, not just the MP3 ones.
Because of that, we can expect more accuracy and less fuzziness if we just assume that mp3 files will have [mM][pP]3 extension, which is right 99.9% of the cases. Not to speak about the processing time of your way on a big collection compared to a simple file name check with find...
Because of that, we can expect more accuracy and less fuzziness if we just assume that mp3 files will have [mM][pP]3 extension, which is right 99.9% of the cases. Not to speak about the processing time of your way on a big collection compared to a simple file name check with find...
Well, not more accuracy, because you will miss some MP3 files, and include any file with mp3 anywhere in the name. (You probably want your regular expression to be grep -i '\.mp3$' since an unescaped . matches any character, and, without the $, it will match anywhere in the name string.)
And, as I demonstrated in my example, you would miss any MP3 files without the ".mp3" somewhere in their name. That example also demonstrates how to restrict your output to type III MP files.
So your"preferred" solution is both less accurate and more "fuzzy" than mine. I will, however, concede that my solution does add one extra processing step, so you do pay a small price for accuracy and precision.
As we like to point out, Linux is about choice, so you can choose whatever method suits you. My inclination is to make a few assumptions as I can about things, and to test any assumptions that I can, but you may feel differently.
Last edited by PTrenholme; 10-15-2008 at 10:06 PM.
Reason: Typo
Well, not more accuracy, because you will miss some MP3 files, and include any file with mp3 anywhere in the name. (You probably want your regular expression to be grep -i '\.mp3$' since an unescaped . matches any character, and, without the $, it will match anywhere in the name string.)
No, you want "find", which is what I suggested on first place, and also "the right tool" for this case. That way there's no fuzziness at all (-name "*.[mM][pP]3" is the closest that you can get at all without analyzing file contents). I agree that ls's and grep's are not a smart solution for this concrete case.
Quote:
As we like to point out, Linux is about choice, so you can choose whatever method suits you. My inclination is to make a few assumptions as I can about things, and to test any assumptions that I can, but you may feel differently.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.