Find the answer to your Linux question:
Results 1 to 9 of 9
need to list all files and in all sub-directories that have a particular extension like *.mpg from a newbie looking at the man pages it would go something like ls ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    2

    LS command line bash help please!

    need to list all files and in all sub-directories that have a particular extension like *.mpg

    from a newbie looking at the man pages it would go something like
    ls *.mpg -R but no joy there...

    thanks!!

  2. #2
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Welcome to the forums!

    `ls` does a lot, but in these cases you probably want to use `find`.

    Something like:
    Code:
    find /path/to/files -name \*.mpg -print
    Can't tell an OS by it's GUI

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    2
    Thanks Freston,
    any way to expand on your example to exclude certain directories? I guess I have a ton in my $RECYCLE.BIN which is making it difficult to find what I want...

    rg

  4. #4
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    231
    Experimented with my own thing - nothing distasteful - and it responded well to
    Code:
    find ~/* -name '*\.mp3' -print
    'find' is an unfriendly bugger. And they seem to have changed it, at least its 'info'. The best thing about it in the past was its extensive 'info'.
    All power is violence; all power is evil.
    Money As Debt

  5. #5
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Quote Originally Posted by greener
    any way to expand on your example to exclude certain directories?
    Easiest way is to pipe through `grep -v`, something like this:
    Code:
    find /path/to/files -name \*.mpg -print | grep -v "trash"
    Can't tell an OS by it's GUI

  6. #6
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    231
    Quote Originally Posted by greener View Post
    Thanks Freston,
    any way to expand on your example to exclude certain directories? I guess I have a ton in my $RECYCLE.BIN which is making it difficult to find what I want...

    rg
    Code:
    find ~/* -name '*\.mp3' -print|grep -v NameOfDirectoryToExclude
    Would be the standard way to do that. The output of the find command is piped to the input of the grep command and grep zaps, the -v option, anything with NameOfDirectoryToExclude in the string.
    All power is violence; all power is evil.
    Money As Debt

  7. #7
    Linux Newbie blnl's Avatar
    Join Date
    Jan 2009
    Location
    The Netherlands
    Posts
    146

    Post

    Find is indeed not easy to use, but it is a very powerful tool.

    If you want to learn about find read this tutorial:
    Beginners Linux Guide; Using the find command
    Fedora 13, GNOME
    HP Compaq NC6000, Intel Pentium M Processor 725, ATI Mobility Radeon 9600, Intel PRO 2200BG
    HP Compaq DC7100, Intel Pentium 4 Processor 541, Intel 915G Express, D-Link DWL-510 AirPlus G

  8. #8
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    Quote Originally Posted by myposts View Post
    You are almost there, but "-R" option to be used before the names ls -R will go recursively. To get specific file names, pipeline it for a filter: ls -R | grep "mpg"

    The "find" command will work as well, however, "find" is very resource consuming, because it reads file descriptors, "ls" (if used without "-l") reads only directory entries and so very fast and efficient.
    Yabut...
    ls -R *.mpg will only work if there actually is a matching file in the specified directory. Also, the output of find is much friendlier as a list to be piped to some other tool. It just gives the full filespec of each matching file, and no cruft to filter out.

    --- rod.
    Last edited by oz; 06-03-2011 at 10:50 PM. Reason: removed SPAM from quote
    Stuff happens. Then stays happened.

  9. #9
    Linux Newbie blnl's Avatar
    Join Date
    Jan 2009
    Location
    The Netherlands
    Posts
    146
    Obviously there are many different ways to do the same thing.

    As OP requested files in ALL directories a small correction is required.
    Code:
    ls -Ra | grep mpg
    Fedora 13, GNOME
    HP Compaq NC6000, Intel Pentium M Processor 725, ATI Mobility Radeon 9600, Intel PRO 2200BG
    HP Compaq DC7100, Intel Pentium 4 Processor 541, Intel 915G Express, D-Link DWL-510 AirPlus G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...