Find Command – How to Filter the Result to Return Only Directories

bashfindgrepshell

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 /?

Best Answer

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)
Related Question