Is it possible to get only the results from find that are directory paths? Using find with some option, or using grep or some other utility the results are piped into as a filter?

I thought something like find | grep */$ might work, but it doesn't. From some other testing where I "grepped for" a folder with a specific name it seems like I get a hit for folder_name$ but not for folder_name/$. This seems conterintuitive. How can I grep for lines that end with /?


Yes, the -type d option is used for this.

For example:

$ find /boot -type d

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)
| improve this answer | |
  • thanks! I looked at the manpage, but I still find them a bit bewildering, must have missed it. :) – user36719 Aug 24 '13 at 15:28

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy