我正在使用find -type f命令从某个起始目录递归查找所有文件。但是,我想阻止某些目录输入和提取其中的文件名。所以基本上我正在寻找类似的东西:
find . -type f ! -name "avoid_this_directory_please"
Run Code Online (Sandbox Code Playgroud)
有没有其他功能可以替代?
Kus*_*nda 49
这是该-prune选项的用途:
find . -type d -name 'avoid_this_directory_please' -prune -o -type f -print
Run Code Online (Sandbox Code Playgroud)
这基本上是说“如果有一个名为 的目录avoid_this_directory_please,请不要输入它,否则,如果它是一个常规文件,则打印其路径名”。
您还可以根据任何其他条件修剪目录,例如来自顶级搜索路径的完整路径名:
find . -type d -path './some/dir/avoid_this_directory_please' -prune -o -type f -print
Run Code Online (Sandbox Code Playgroud)
Ste*_*uch 15
为了避免目录尝试 -path 测试:
find . -type f ! -path '*/avoid_this_directory_please/*'
Run Code Online (Sandbox Code Playgroud)
来自man find,在该-path部分中,显然结合了其他 2 个答案
To ignore a whole directory tree, use -prune rather than
checking every file in the tree. For example, to skip the directory
`src/emacs' and all files and directories under it, and print the
names of the other files found, do something like this:
find . -path ./src/emacs -prune -o -print
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102029 次 |
| 最近记录: |