Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 10 down vote accepted

Have you tried ls *.mp3?

share|improve this answer
2  
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
2  
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
add comment

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:

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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