[bash] List files with certain extensions with ls and grep

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.

This question is related to bash shell macos grep

The answer is


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

the easiest way is to just use ls

ls *.mp4 *.mp3 *.exe

Here is one example that worked for me.

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

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

egrep -- extended grep -- will help here

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

should do the job.


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.


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

it is easy try to use this command :

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

ls -R | findstr ".mp3"

ls -R => lists subdirectories recursively


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

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

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.


Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to grep

grep's at sign caught as whitespace cat, grep and cut - translated to python How to suppress binary file matching results in grep Linux find and grep command together Filtering JSON array using jQuery grep() Linux Script to check if process is running and act on the result grep without showing path/file:line How do you grep a file and get the next 5 lines How to grep, excluding some patterns? Fast way of finding lines in one file that are not in another?