
ディレクトリパスに対してのみ検索結果を取得できますか?いくつかのオプションでfindを使用するか、grepまたは他のユーティリティを使用して結果をフィルタにリンクしますか?
私は似たようなことがうまくいくとfind | grep */$
思いましたが、そうではありません。特定の名前のフォルダを「検索」した別のテストでヒットを取得しているようですが、folder_name$
何もありませんfolder_name/$
。これは直観にずれているようです。で終わる行を見つけるためにgrepする方法は/
?
答え1
はい、これは-type d
このオプションの目的です。
たとえば、
$ find /boot -type d
/boot
/boot/grub
/boot/grub/locale
/boot/grub/fonts
/boot/grub/i386-pc
マニュアルページの関連部分は次のとおりです。
-type c
File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link; this is never true if the -L option or the
-follow option is in effect, unless the symbolic link is
broken. If you want to search for symbolic links when -L
is in effect, use -xtype.
s socket
D door (Solaris)
答え2
サプリメントとしてピレウスゼロの答え、ディレクトリに解決されるシンボリックリンクを含める場合:
GNUを使って次を見つけてください。
find . -xtype d
POSIX的に:
find . -exec test -d {} \; -print
次のように最適化できます。
find . \( -type d -o -type l -exec test -d {} \; \) -print
ディレクトリツリーを降りるときにシンボリックリンクをたどるには、次のようにします。
find -L . -type d
ディレクトリとディレクトリへのシンボリックリンクを報告します。シンボリックリンクを望まない場合:
GNUを使って次を見つけてください。
find -L . -xtype d
POSIX的に:
find -L . -type d ! -exec test -L {} \; -print
そしてzsh
:
print -rC1 -- **/*(ND/) # directories
print -rC1 -- **/*(ND-/) # directories, or symlinks to directories
print -rC1 -- ***/*(ND/) # directories, traversing symlinks
print -rC1 -- ***/*(ND-/) # directories or symlinks to directories,
# traversing symlinks