List files with certain extensions with ls and grep

BashShellMacosGrep

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

Why not:

ls *.{mp3,exe,mp4}

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

Solution 2 - Bash

egrep -- extended grep -- will help here

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

should do the job.

Solution 3 - Bash

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)'

Solution 4 - Bash

the easiest way is to just use ls

ls *.mp4 *.mp3 *.exe

Solution 5 - Bash

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

Solution 6 - Bash

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

find -iname '*.mp3' -o -iname '*.exe' -o -iname '*.mp4'

Solution 7 - Bash

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.

Solution 8 - Bash

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.

Solution 9 - Bash

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

Solution 10 - Bash

ls -R | findstr ".mp3"

ls -R => lists subdirectories recursively

Solution 11 - Bash

it is easy try to use this command :

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

Solution 12 - Bash

Here is one example that worked for me.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMintView Question on Stackoverflow
Solution 1 - Bashmeder omuralievView Answer on Stackoverflow
Solution 2 - BashmobView Answer on Stackoverflow
Solution 3 - BashDennis WilliamsonView Answer on Stackoverflow
Solution 4 - BashJoshua KView Answer on Stackoverflow
Solution 5 - BashcamhView Answer on Stackoverflow
Solution 6 - BashP ShvedView Answer on Stackoverflow
Solution 7 - BashHai VuView Answer on Stackoverflow
Solution 8 - Bashjames2doyleView Answer on Stackoverflow
Solution 9 - BashJeff McView Answer on Stackoverflow
Solution 10 - BashvardamanpkView Answer on Stackoverflow
Solution 11 - BashMohamed HassouView Answer on Stackoverflow
Solution 12 - BashJyoti PrakashView Answer on Stackoverflow