简体   繁体   English

在linux中使用bash查找文件夹中的所有音频文件

[英]find all audio files in a folder using bash in linux

When I am using wildcards with ls command, it works.当我在ls命令中使用通配符时,它可以工作。

$ 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 命令不起作用

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

What is the error in the line?线路中的错误是什么?

I think this should work for you我认为这应该适合你

   find ~ -name "*.mp3" -o -name "*.ogg"

-o is equivalent to boolean or -o 相当于布尔值or

If you enable extglob ( shopt -s extglob ), you can use *.@(ogg|mp3) .如果启用 extglob ( shopt -s extglob ),则可以使用*.@(ogg|mp3)

shopt -s extglob
printf '%s\n' *.@(mp3|ogg)

If you need recursion too, enable globstar (requires bash 4.0 or newer)如果您也需要递归,请启用 globstar(需要 bash 4.0 或更新版本)

shopt -s extglob globstar
printf '%s\n' **/*.@(mp3|ogg)

When you use ls *.{mp3,ogg} , you are combining brace expansion and pathname expansion.当您使用ls *.{mp3,ogg} ,您将结合大括号扩展和路径名扩展。 What happens is:发生的事情是:

ls *.{mp3,ogg}  
ls *.mp3 *.ogg  # after brace expansion
ls '*.mp3' 1.ogg 2.ogg 3.ogg # after pathname expansion

If there's no matching files for a glob, the glob will just be passed on unchanged.如果 glob 没有匹配的文件,则 glob 将原封不动地传递。 And ls will treat it as a literal filename; ls会将其视为文字文件名; it doesn't know about globs/wildcards.它不知道全局/通配符。

The find ~ -iname '*.{mp3,ogg}' doesn't work because find doesn't do brace expansion, that's a bash feature. find ~ -iname '*.{mp3,ogg}'不起作用,因为find不进行大括号扩展,这是 bash 功能。

This one provides you with even those files which do not have mp3 or audio extension.这甚至为您提供了那些没有mp3 或音频扩展名的文件。

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

which interprets to:这解释为:

find . -print0 find . -print0 Find (list) every file and output with a null terminator find . -print0查找(列出)每个文件并使用空终止符输出

xargs -0 file -F '//' Run file(1) with the stdin (or piped input), delimited by null character, as the first argument. xargs -0 file -F '//'使用标准输入(或管道输入)运行file(1) ),以空字符分隔,作为第一个参数。

file -F '//' Delimit file name and its type by // as it neither appears in a UNIX filename nor as a file type. file -F '//'划界文件名和它的类型由//因为它既不出现在文件名UNIX也不作为文件类型。

awk -F '//' '...' Use the // delimiter as field separator. awk -F '//' '...'使用//分隔符作为字段分隔符。 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. awk 脚本不区分大小写匹配第二个字段中出现的audio (即file(1)报告的文件类型),并为每个匹配打印文件路径。

Here is one I just did .这是我刚做的。 . . . .

for .ogg and .mp3对于 .ogg 和 .mp3

         find Music | grep '/*.ogg\|/*.mp3' | sort -u

find does not support the full shell wildcard syntax (specifically, not the curly braces). find不支持完整的 shell 通配符语法(特别是不支持花括号)。 You'll need to use something like this:你需要使用这样的东西:

find ~ -iname '*.mp3' -o -iname '*.ogg'

关于什么?

file * | grep audio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM