bash 在linux中使用bash查找文件夹中的所有音频文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11928440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find all audio files in a folder using bash in linux
提问by user5198
When I am using wildcards with lscommand, 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?
线路中的错误是什么?
回答by Rajeev
I think this should work for you
我认为这应该适合你
find ~ -name "*.mp3" -o -name "*.ogg"
-o is equivalent to boolean or
-o 相当于布尔值 or
回答by geirha
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. What happens is:
当您使用 时ls *.{mp3,ogg},您将结合大括号扩展和路径名扩展。发生的事情是:
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. And lswill treat it as a literal filename; it doesn't know about globs/wildcards.
如果 glob 没有匹配的文件,则 glob 将原封不动地传递。而ls将其当作一个文本文件名; 它不知道全局/通配符。
The find ~ -iname '*.{mp3,ogg}'doesn't work because finddoesn't do brace expansion, that's a bash feature.
在find ~ -iname '*.{mp3,ogg}'因为不工作find没有做括号扩展,这是一个bash的功能。
回答by Chris Sullivan
Here is one I just did . . .
这是我刚做的。. .
for .ogg and .mp3
对于 .ogg 和 .mp3
find Music | grep '/*.ogg\|/*.mp3' | sort -u
回答by Hritik
This one will provide you with even those files which do nothave a mp3 or audio extension.
这甚至会为您提供那些没有mp3 或音频扩展名的文件。
find . -print0 | xargs -0 file | grep -i audio| cut -f 1 -d ':'
which interprets to:
这解释为:
find . -print0Find (list) every file and output as with a null terminator
find . -print0查找(列出)每个文件并输出为空终止符
xargs -0 fileRun file(command) with the stdin (or piped input), delimited by null character, as the first argument.
xargs -0 filefile使用标准输入(或管道输入)运行(命令),以空字符分隔,作为第一个参数。
grep -i audioGet the line which contains the word audio(case insensitive)
grep -i audio获取包含单词的行audio(不区分大小写)
cut -f 1 -d ':'cut the input delemited by :and print the first part.
cut -f 1 -d ':'剪切由 分隔的输入:并打印第一部分。
回答by Greg Hewgill
finddoes not support the full shell wildcard syntax (specifically, not the curly braces). You'll need to use something like this:
find不支持完整的 shell 通配符语法(特别是不支持大括号)。你需要使用这样的东西:
find ~ -iname '*.mp3' -o -iname '*.ogg'
回答by downtheroad
what about?
关于什么?
file * | grep audio

