How I can list all files of a certain type in a directory?

For example, if I want to see all MP3 files in the current folder, what do I do?


Disregarding suffix, you can use the file command to identify audio files. That is more complicated than an simple ls command can do, so one would use a script. For example (ID3 seems to be file jargon for MP3):

for name in "$@"
    case $(file "$name") in
        ls -l "$name"

Putting that in your executable path (such as ~/bin as "ls-audio") you could then do

ls-audio *

to list details of just the MP3 audio files.

  • #!/bin/sh - is that really sh and not bash? - if you mean bash, then change to bash. – Hannu Sep 1 '15 at 9:32
  • If I wanted to use a bash-specific feature, I would use bash. This script does not rely upon any bash-specific features (just POSIX). – Thomas Dickey Sep 1 '15 at 20:43
  • It seems I might have not stated it clearly enough. Hint: is sh on OSX just a hard link to bash? For Ubuntu 14.04 it is a link to dash... you might have problems arising from the fact that you're not getting what you expect - POSIX or not, the degree of compliance varies. – Hannu Sep 2 '15 at 11:44
  • It's a symbolic link (not a hard link). However, your point is obscure, since I tested with dash on Debian before posting my answer. If you have some specific criticism of the script's use of POSIX features, that would be useful. – Thomas Dickey Sep 2 '15 at 21:58

Using OS X spotlight:

mdfind -onlyin /path/to/directory  "kMDItemKind == 'MP3 audio'"

You can simply go to the directory that you wish to query by using the cd command to get to the directory you want to query. Than type in ls *.mp3 to list all files with the extension of the file you wish to find.

For example if you want to see all .txt files on your desktop you can simple type cd ~/Desktop and next type in ls *.txt which will give you the result of

  • 3
    That lists files whose suffix is ".mp3" (it's possible to have mp3 files without the suffix). – Thomas Dickey Aug 31 '15 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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