find all audio files in a folder using bash in linux

Viewed 12712

When I am using wildcards with ls command, it works.

$ ls '*.{mp3,ogg}'  # Showing only two formats in the command
cannot access *.mp3: No such file or directory
1.ogg 2.ogg 3.ogg

but using find command doesn't work

$ find ~ -iname '*.{mp3,ogg}'

What is the error in the line?

6 Answers

This one provides you with even those files which do not have mp3 or audio extension.

find -print0 | xargs -0 file -F '//' | awk -F '//' 'tolower($2) ~ /audio/ { print $1 }'

which interprets to:

find . -print0 Find (list) every file and output with a null terminator

xargs -0 file -F '//' Run file(1) with the stdin (or piped input), delimited by null character, as the first argument.

file -F '//' Delimit file name and its type by // as it neither appears in a UNIX filename nor as a file type.

awk -F '//' '...' Use the // delimiter as field separator. The awk script case-insensitively matches the occurrence of audio in the second field (ie the file type as reported by file(1)) and for every match, prints the file path.

Related