当我使用带ls
命令的通配符时,它可以工作。在Linux中使用bash查找文件夹中的所有音频文件
$ ls '*.{mp3,ogg}' # Showing only two formats in the command
cannot access *.mp3: No such file or directory
1.ogg 2.ogg 3.ogg
但使用find命令不起作用
$ find ~ -iname '*.{mp3,ogg}'
什么是该行的错误?
当我使用带ls
命令的通配符时,它可以工作。在Linux中使用bash查找文件夹中的所有音频文件
$ ls '*.{mp3,ogg}' # Showing only two formats in the command
cannot access *.mp3: No such file or directory
1.ogg 2.ogg 3.ogg
但使用find命令不起作用
$ find ~ -iname '*.{mp3,ogg}'
什么是该行的错误?
我想这应该为你
find ~ -name "*.mp3" -o -name "*.ogg"
-o相当于布尔or
find
不支持完整的shell通配符语法(具体而言,不是大括号)。你需要使用这样的事情:
find ~ -iname '*.mp3' -o -iname '*.ogg'
如果启用extglob(shopt -s extglob
)工作,你可以使用*[email protected](ogg|mp3)
。
shopt -s extglob
printf '%s\n' *[email protected](mp3|ogg)
如果需要递归也使globstar(需要的bash 4.0或更新版本)
shopt -s extglob globstar
printf '%s\n' **/*[email protected](mp3|ogg)
当您使用ls *.{mp3,ogg}
,要结合括号扩展和路径扩展。会发生什么情况是:
ls *.{mp3,ogg}
ls *.mp3 *.ogg # after brace expansion
ls '*.mp3' 1.ogg 2.ogg 3.ogg # after pathname expansion
如果glob没有匹配的文件,glob只会传递不变。而ls
会将其视为文字文件名;它不知道关于globs /通配符。
find ~ -iname '*.{mp3,ogg}'
不起作用,因为find
不做大括号扩展,这是一个bash功能。
这是我刚才做的一个。 。 。
为.OGG和MP3播放
find Music | grep '/*.ogg\|/*.mp3' | sort -u
这人会为你提供甚至那些做不有MP3或音频扩展名的文件。
find . -print0 | xargs -0 file | grep -i audio| cut -f 1 -d ':'
它解释到:
find . -print0
查找(列表)的每个文件和输出作为与空终止
xargs -0 file
运行file
(命令)与标准输入(或管道输入),分隔作为第一个参数的空字符。
grep -i audio
获取其中包含字audio
(不区分大小写)
cut -f 1 -d ':'
削减:
delemited输入线和打印的第一部分。
Brace扩展和路径名扩展不会为引用的单词完成,所以您必须运行'ls *。{mp3,ogg}'来获取该输出,而不是'ls'*。{mp3,ogg}'' – geirha 2012-08-13 05:29:45
那么m4a文件等?你应该依靠'file'来代替。 – Blauhirn 2017-11-27 15:20:41