Bash

查找與名稱匹配的所有文件或目錄(無內容)

  • February 5, 2022

我正在嘗試編寫一個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

您可以修剪**目錄類型的匹配項:

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

來自man find

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

引用自:https://unix.stackexchange.com/questions/689340