查找与名称匹配的所有文件或目录(不含内容)

查找与名称匹配的所有文件或目录(不含内容)

我正在尝试编写一个find命令来搜索andreas其中包含该名称的所有文件或目录(为了清楚起见,添加了正斜杠)。

~$ find . -iname '*andreas*'
./Documents/Resume - Andreas/
./Documents/Resume - Andreas/Resume-Andreas-Renberg.odf
./Documents/Resume - Andreas/Resume-Andreas-Renberg.pdf
./Documents/Resume - Andreas/Resume-Andreas-Renberg-v2.pdf
./Documents/Resume - Andreas/Resume-Andreas-Renberg-v3.pdf
./Documents/Resume - Andreas/Resume-Andreas-Renberg-final.pdf
./Pictures/Trip with Andreas to Isengard/
./Pictures/Profiles/andreas-renberg.jpg
./.cache/junk/nothing here/hide-from-andreas.tar.gz

几乎正是我需要的,但该列表中有大量冗余,导致我的其他脚本出现问题。

Find 已经告诉我有关该目录的信息./Documents/Resume - Andreas/,因此我不想find列出该目录中所有匹配的内容。我希望输出看起来像这样:

~$ find . -iname '*andreas*' [command]
./Documents/Resume - Andreas/
./Pictures/Trip with Andreas to Isengard/
./Pictures/Profiles/andreas-renberg.jpg
./.cache/junk/nothing here/hide-from-andreas.tar.gz

答案1

你可以修剪类型匹配目录

find . -iname '*andreas*' -print -type d -prune

man find

-prune True; if the file is a directory, do not descend into it.

相关内容