Filtering the result of the find command, so that it returns only directories
Yes, the -type d
option is used for this.
For example:
$ find /boot -type d
/boot
/boot/grub
/boot/grub/locale
/boot/grub/fonts
/boot/grub/i386-pc
Here's the relevant section of the man page:
-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)
As an addition to Zero Piraeus' answer, if you want to include symlinks that resolve to directories:
with GNU find:
find . -xtype d
POSIXly:
find . -exec test -d {} \; -print
which you can optimise to
find . \( -type d -o -type l -exec test -d {} \; \) -print
If you want to follow symlinks when descending the directory tree, you'd do:
find -L . -type d
which would report directories and symlinks to directories. If you don't want the symlinks:
with GNU find:
find -L . -xtype d
POSIXly:
find -L . -type d ! -exec test -L {} \; -print
With 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