LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-15-2008, 02:30 PM   #1
tommytomthms5
Member
 
Registered: Sep 2007
Distribution: debian based
Posts: 305

Rep: Reputation: 30
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

Code:
find `pwd` "*" |grep .mp3 >> ~/list.txt
Any help is appreciated!!! Thanks.
 
Old 10-15-2008, 02:35 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Assuming you use bash:

Code:
find . -name "*.mp3"  > ~/list.txt
`pwd` is needlessly complicated, . can be used to represent the current working directory.

>> appends to a file or creates it if it doesn't exist.
> empties the file if it exists then writes to it, or creates it if it doesn't exist.

Last edited by arizonagroovejet; 10-15-2008 at 02:37 PM. Reason: Forgot quotes
 
Old 10-15-2008, 02:35 PM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
Quote:
Originally Posted by tommytomthms5 View Post
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.
 
Old 10-15-2008, 02:39 PM   #4
tommytomthms5
Member
 
Registered: Sep 2007
Distribution: debian based
Posts: 305

Original Poster
Rep: Reputation: 30
okay thanks for clearing up the >> vs > and I'll try again
 
Old 10-15-2008, 02:46 PM   #5
evaluatinglinux
Member
 
Registered: Oct 2008
Posts: 45

Rep: Reputation: 15
Off the top of my head, i think something like:
grep -i ".mp3" || ls */*

should do the trick!

Debian Kernel

Last edited by evaluatinglinux; 10-25-2008 at 03:50 AM.
 
Old 10-15-2008, 02:51 PM   #6
tommytomthms5
Member
 
Registered: Sep 2007
Distribution: debian based
Posts: 305

Original Poster
Rep: Reputation: 30
Sorry if this is a double post but I want to show what worked perfectly for those with similar problems...

Code:
find /home/t/music -name "*.mp3" > /home/t/list.txt
now can simply use:

Code:
mplayer -shuffle -playlist ~/list.txt
for my linux media player!!!
 
Old 10-15-2008, 02:53 PM   #7
Randux
Senior Member
 
Registered: Feb 2006
Location: Siberia
Distribution: Slackware & Slamd64. What else is there?
Posts: 1,705

Rep: Reputation: 54
I'm pretty sure 'reclusively' is not a word unless you're talking about Herman the Hermit.
 
Old 10-15-2008, 02:53 PM   #8
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by evaluatinglinux View Post
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...
 
Old 10-15-2008, 03:21 PM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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:
Code:
$ find . -print0 | xargs -r0 file | grep MPEG.*III | cut -f1 -d:
./Justin and Frieda (Filtered)
./Justin and Frieda (Raw)

Last edited by PTrenholme; 10-15-2008 at 03:32 PM. Reason: Added example
 
Old 10-15-2008, 03:25 PM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
Quote:
Originally Posted by arizonagroovejet View Post
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:
  1. 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).
  2. Once you press enter, the console keeps waiting for your input
  3. You write, and press control+d when you are done
  4. 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.

Last edited by i92guboj; 10-15-2008 at 03:31 PM.
 
Old 10-15-2008, 03:28 PM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
Quote:
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...
 
Old 10-15-2008, 04:12 PM   #12
tommytomthms5
Member
 
Registered: Sep 2007
Distribution: debian based
Posts: 305

Original Poster
Rep: Reputation: 30
uhh guys this topic is solved I don't understand why you want a better solution then what I used....


Quote:
Originally Posted by tommytomthms5 View Post
Sorry if this is a double post but I want to show what worked perfectly for those with similar problems...

Code:
find /home/t/music -name "*.mp3" > /home/t/list.txt
now can simply use:

Code:
mplayer -shuffle -playlist ~/list.txt
for my linux media player!!!
 
Old 10-15-2008, 08:46 PM   #13
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by i92guboj View Post
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 11:06 PM. Reason: Typo
 
Old 10-15-2008, 08:53 PM   #14
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
Quote:
Originally Posted by PTrenholme View Post
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.
That's the only universal truth
 
  


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
Script to list files in a given directory Azzath Programming 8 04-03-2008 08:02 AM
find and list files modified since system was booted Mike144 Linux - Newbie 5 04-09-2006 09:03 PM
Want to make mp3 files from audio CD barrythai Mandriva 7 03-27-2006 12:46 AM
How do I make the media players play .asf files? xSTELLA Linux - Newbie 11 07-08-2005 02:12 AM
Why can't I play may MP3 and media files in RH 9.0 hashim_qadri Linux - Software 4 06-09-2005 10:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:09 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration