Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On Windows I used to quickly run a dir *.mp3 to find all files with an mp3 extension in the current directory. Is there a similarly quick way to do it with bash? The ls command seems to have a way to ignore a pattern, but not to show only the pattern. I can do find . -maxdepth 1 -iname '*.mp3' or ls|grep -i '\.mp3$' but neither of these flow out of my fingers in half a second or less)

Any quicker alternatives?

share|improve this question
Note that it is the interpreter (bash) that does the wildcard substitution. Thus, ls *.mp3 could result in a very long list (out of memory / command line too long), depending on how many files that match the globbing pattern. bash actually calls "ls aGoodSong1.mp3 boringSong2.mp3 coolSong.mp3" (if you have those 3 files). A workaround: ls | grep -i '[.]mp3$' (where ls lists all files and grep shows only the .mp3 files) – MattBianco Nov 26 '13 at 12:19
Just for fun, try echo *.mp3 – MattBianco Nov 26 '13 at 12:25
up vote 10 down vote accepted

Have you tried ls *.mp3?

share|improve this answer
Ooh, er, that's a bit strange. It works! I'm sure that was the first thing I tried way back when. Is there any situation where this doesn't work? Thanks for responding:) – Andy Jun 17 '10 at 11:16
Yep, it won't show '*.MP3' files. You'll need to use grep -i in order to ignore the case – kolypto Jun 17 '10 at 11:28
Or just do ls *.mp3 *.MP3 – Paul R Jun 17 '10 at 11:47
... but: ls *.[mM][pP]3 will. – JRobert Jun 17 '10 at 11:48
I've been meaning to post about this for some time. Maybe it was a problem with dir /s *.mp3... anyways, thanks for an answer! – Andy Jun 17 '10 at 12:14

Note that in bash, the star wildcard is resolved by the command line interpreter before the resulting array of strings is then passed to the command proper. On the Windows (ex-DOS) command line, the star wildcard is passed to the command 'as is', which then has to sadly deal with it. See the Wikipedia globbing entry

Try this to see how the wildcard behaves:

# Display all "positional parameters" (as they are called), passed to the script:    
# COUNT is the number of positional paramaters

while [[ $COUNT -gt 0 ]]; do
   echo "'$1'"
   # pop positional argument 1 off the stack of positional arguments
   let COUNT=$COUNT-1
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .