204

I just want to get the files from the current dir and only output .mp4 .mp3 .exe files nothing else. So I thought I could just do this:

ls | grep \.mp4$ | grep \.mp3$ | grep \.exe$

But no, as the first grep will output just mp4's therefor the other 2 grep's won't be used.

Any ideas? PS, Running this script on Slow Leopard.

3
  • 1
    This really is the wrong approach -- instead of using grep, use shopt -s nullglob and then just refer to *.exe *.mp3 *.mp4. See mywiki.wooledge.org/ParsingLs Sep 19, 2009 at 6:15
  • 3
    I can't figure out whether or not "Slow Leopard" was a typo... May 19, 2018 at 1:24
  • 1
    @Wowfunhappy hahaha, definitely was a typo, I recall thinking Snow Leopard was quite fast.
    – Mint
    May 20, 2018 at 4:50

12 Answers 12

426

Why not:

ls *.{mp3,exe,mp4}

I'm not sure where I learned it - but I've been using this.

5
  • 1
    This isn't working for me because the extension I am using is for a directory, so the ls is listing the contents of the directory. Aug 27, 2013 at 2:23
  • 1
    @RichardVenable add the -d switch to prevent that directories are recursed. Oct 5, 2013 at 16:50
  • 15
    I like this solution but it seems to fail if you are missing any one of the filetypes. For example, you have mp3 but no .exe (Mac OSX, zsh)
    – JHo
    Dec 21, 2013 at 13:42
  • 4
    I redirected stderr to /dev/null to avoid ls: *.exe: No such file or directory eg: ls *.{zip,tar.gz,tar} 2>/dev/null
    – Isaac
    Sep 6, 2017 at 0:42
  • 3
    When I run ls foo*.{tar.gz,zip} directly in a shell it works, but when put this inside a shell script latest=$(ls -I '.done' -tr ${pkgprefix}.{tar.gz,zip} | tail -1) I got an error message: ls: cannot access 'bamtools*.{tar.gz,zip}': No such file or directory, any smarter guy can refined the answer.
    – Kemin Zhou
    Feb 9, 2018 at 19:54
59

egrep -- extended grep -- will help here

ls | egrep '\.mp4$|\.mp3$|\.exe$'

should do the job.

4
  • Thats it! Thanks Just realized I should have it case insensitive, so I'm using: ls | egrep -i '\.mp4$|\.mp3$|\.exe$ Incase anyone else needs help with that one day. Im always surprised by the speed I get my answer on here.
    – Mint
    Sep 19, 2009 at 3:36
  • I can't see how this would work. ls without any options produces output in columns. Anchoring to the end of the line will not match properly.
    – camh
    Sep 19, 2009 at 6:44
  • 4
    @camh: ls to a terminal (or with -C option) produces multi-column output. ls to a pipe (or with -1) has single column output. (Compare output of ls with ls | cat).
    – mob
    Sep 19, 2009 at 7:07
  • There's a missing apostrophe at the end. Other than that it seems to work.
    – Björn
    Oct 27, 2017 at 14:45
48

Use regular expressions with find:

find . -iregex '.*\.\(mp3\|mp4\|exe\)' -printf '%f\n'

If you're piping the filenames:

find . -iregex '.*\.\(mp3\|mp4\|exe\)' -printf '%f\0' | xargs -0 dosomething

This protects filenames that contain spaces or newlines.

OS X find only supports alternation when the -E (enhanced) option is used.

find -E . -regex '.*\.(mp3|mp4|exe)'
4
  • 2
    On Mac OS X: find . -iregex '.*\(mp3\|mp4\|exe\)'
    – andilabs
    Nov 8, 2013 at 11:07
  • 4
    andi, that didn't work for me on mac os. But this did: find -E . -regex '.*(mp3|mp4|exe)'
    – Jo Jo
    Aug 28, 2014 at 17:42
  • This solution will load files like: mysupermp3.jpg consider adding the $ at the end of the regex and the \\. before the extensions
    – Panthro
    Feb 22, 2017 at 12:45
  • 1
    @Panthro: actually, it won't. Find appears to use implied anchors. The dot is a good idea (only one backslash is needed). Feb 28, 2017 at 0:24
20

the easiest way is to just use ls

ls *.mp4 *.mp3 *.exe
6
  • 2
    Thanks, but I already tried that and I didn't like the errors you get when there is no file. Thou I could of fixed that by doing: ls *.mp4 *.mp3 *.exe 2> /dev/null Only thought of that now thou :P
    – Mint
    Sep 19, 2009 at 3:40
  • 1
    I am surprised that ls doesn't have some sort of silent option.
    – MitMaro
    Sep 19, 2009 at 3:47
  • 3
    In bash, you can do "set -o nullglob", and you wont get the errors.
    – camh
    Sep 19, 2009 at 4:10
  • 3
    My mistake - that should be "shopt -s nullglob" not the set -o command
    – camh
    Sep 19, 2009 at 5:54
  • 2
    Or just use "echo": echo *.mp4 *.mp3 *.exe Sep 19, 2009 at 6:02
13

No need for grep. Shell wildcards will do the trick.

ls *.mp4 *.mp3 *.exe

If you have run

shopt -s nullglob

then unmatched globs will be removed altogether and not be left on the command line unexpanded.

If you want case-insensitive globbing (so *.mp3 will match foo.MP3):

shopt -s nocaseglob
2
  • Same comment as I gave to Good Time Tribe.
    – Mint
    Sep 19, 2009 at 3:54
  • 2
    I am trying to use this with the -R option to list matching files in subdirectories but the option doesn't seem to have any effect and I still only get the current directory's results.
    – Chris
    Feb 13, 2019 at 9:51
10

Just in case: why don't you use find?

find -iname '*.mp3' -o -iname '*.exe' -o -iname '*.mp4'
1
  • 1
    I found this worked instead - find . -name '*.mkv' -o -name '*.flv' (adding as many -o clauses as required). I needed the . to indicate the directory and the flag -name not -iname - I'm on macOS 10.13.
    – Chris
    Feb 13, 2019 at 9:55
7

For OSX users:

If you use ls *.{mp3,exe,mp4}, it will throw an error if one of those extensions has no results.

Using ls *.(mp3|exe|mp4) will return all files matching those extensions, even if one of the extensions had 0 results.

4
  • 6
    I got a syntax error using your example??? 'bash: syntax error near unexpected token `('
    – nagordon
    May 27, 2015 at 18:17
  • 2
    I guess you mean ls *.@(mp3|exe|mp4). You need shopt -s extglob for this to work. By the way, ls is useless too, you could just do printf '%s\n' *.@(mp3|exe|mp4). Jan 4, 2017 at 20:15
  • 1
    I get the syntax unexpected token message on macOS 10.13
    – Chris
    Feb 13, 2019 at 9:39
  • what if my folder has .h & .hpp files & I only want to list .h Nov 17, 2022 at 10:35
5

In case you are still looking for an alternate solution:

ls | grep -i -e '\\.tcl$' -e '\\.exe$' -e '\\.mp4$'

Feel free to add more -e flags if needed.

3
ls | grep "\.mp4$
\.mp3$
\.exe$"
2
  • Thanks, but a bit inconvenient using up several lines.
    – Mint
    Sep 19, 2009 at 3:38
  • This + mdfind yields the best / fastest searches EVER! mdfind -name querystring | grep "\.h$" finds all headers with quesrystring in the file title. pronto.
    – Alex Gray
    Nov 14, 2012 at 18:32
0

ls -R | findstr ".mp3"

ls -R => lists subdirectories recursively

0

it is easy try to use this command :

ls | grep \.txt$ && ls | grep \.exe
-1

Here is one example that worked for me.

find <mainfolder path> -name '*myfiles.java' | xargs -n 1 basename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.