使用 -regex 搜索文件时如何使用 find 指定文件路径

Ada*_*ter 3 find

我想使用find正则表达式在目录中搜索一些文件。

如果我做

cd dir
find -E . -type f -regex '^\..*[^~]'
Run Code Online (Sandbox Code Playgroud)

我得到目录中与正则表达式匹配的文件列表。

但是,如果我这样做

find -E ~/dir -type f -regex '^\..*[^~]'
Run Code Online (Sandbox Code Playgroud)

IE

find -E /home/adam/dir -type f -regex '^\..*[^~]'
Run Code Online (Sandbox Code Playgroud)

我没有输出。find使用正则表达式时如何指定要搜索的目录?

这是 BSD find,因为我在 Mac 上。

Gil*_*il' 6

的参数-regex必须匹配找到的整个路径。类似的命令find .查找类似的路径./dir/subdir/somefile,而类似的命令find ~/dir查找类似的路径/home/adam/dir/subdir/somefile。所以你的正则表达式必须与/home/adam开头的部分匹配 。

该命令find -E . -type f -regex '^\..*[^~]'查找名称不包含换行符且不以~. 在.一开始总是匹配由于路径开头./

如果您要查找点文件,则需要允许目录前缀。以下命令显示名称不以 结尾的点文件~

find -E /whatever -regex '(\n|.)*/\.[^/]*[^~]'
Run Code Online (Sandbox Code Playgroud)

但这更容易表达-name

find /whatever -name '.*[!~]'
Run Code Online (Sandbox Code Playgroud)