我认为最简单的方法是使用:
find . -name "??????????*"
Run Code Online (Sandbox Code Playgroud)
其中?
字符数等于n
. 很简单,因为很难忘记它。
但最好的方法是使用该-regex
选项来查找包含n
或更多字符的文件名:
find . -regextype posix-egrep -regex ".*[^/]{n}"
Run Code Online (Sandbox Code Playgroud)
wheren
应该是一个自然数(最小文件名长度)。
查看man find
更多关于。
您可以将find
命令与-regex
测试一起使用
$ find /path/to/folder -regextype posix-basic -regex '.*/.\{5,\}'
Run Code Online (Sandbox Code Playgroud)
或者
$ find /path/to/folder -regextype posix-extended -regex '.*/.{5,}'
Run Code Online (Sandbox Code Playgroud)
请注意,这-regex
是路径匹配而不是文件匹配- 因此您还需要匹配前导.*/
,在 5 个以上字符的文件名之前
或者,对于纯 bash 解决方案,您可以启用扩展的 shell globbing,然后使用!(@(?|??|???|????))
表示“任何不匹配一两个或三个或四个字符的任何内容”的模式
$ shopt -s extglob
$ ls -d /path/to/folder/!(@(?|??|???|????))
Run Code Online (Sandbox Code Playgroud)
如果要包含子目录,也可以启用该globstar
选项并添加**
通配符,即
$ shopt -s extglob globstar
$ ls -d /path/to/folder/**/!(@(?|??|???|????))
Run Code Online (Sandbox Code Playgroud)
例如
$ ls -d **/!(@(?|??|???|????))
abcde abcdef abcdefg subdir subdir/abcde subdir/abcdef subdir/abcdefg
Run Code Online (Sandbox Code Playgroud)
而非反向匹配(短于 5 个字符的文件)是
$ ls -d **/@(?|??|???|????)
a ab abc abcd subdir/a subdir/ab subdir/abc subdir/abcd
Run Code Online (Sandbox Code Playgroud)
之后要取消设置选项,请使用
$ shopt -u extglob globstar
Run Code Online (Sandbox Code Playgroud)