Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude this / current / dot folder from find "type d"

People also ask

How do I exclude a directory in find?

We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command. The directory “bit” will be excluded from the find search!

How do you use prune in find command?

Prune usage:$ find . -prune . The find command works like this: It starts finding files from the path provided in the command which in this case is the current directory(.). From here, it traverses through all the files in the entire tree and prints those files matching the criteria specified.

How do I find the current directory in Ubuntu terminal?

To print the current working directory, we use the pwd command in the Linux system. pwd (print working directory) – The pwd command is used to display the name of the current working directory in the Linux system using the terminal.


Not only the recursion depth of find can be controlled by the -maxdepth parameter, the depth can also be limited from “top” using the corresponding -mindepth parameter. So what one actually needs is:

find . -mindepth 1 -type d

POSIX 7 solution:

find . ! -path . -type d

For this particular case (.), golfs better than the mindepth solution (24 vs 26 chars), although this is probably slightly harder to type because of the !.

To exclude other directories, this will golf less well and requires a variable for DRYness:

D="long_name"
find "$D" ! -path "$D" -type d

My decision tree between ! and -mindepth:

  • script? Use ! for portability.
  • interactive session on GNU?
    • exclude .? Throw a coin.
    • exclude long_name? Use -mindepth.

I use find ./* <...> when I don't mind ignoring first-level dotfiles (the * glob doesn't match these by default in bash - see the 'dotglob' option in the shopt builtin: https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html).

eclipse tmp # find .
.
./screen
./screen/.testfile2
./.X11-unix
./.ICE-unix
./tmux-0
./tmux-0/default
eclipse tmp # find ./*
./screen
./screen/.testfile2
./tmux-0
./tmux-0/default

Well, a simple workaround as well (the solution was not working for me on windows git bash)

find * -type d

It might not be very performant, but gets the job done, and it's what we need sometimes.

[Edit] : As @AlexanderMills commented it will not show up hidden directories in the root location (eg ./.hidden), but it will show hidden subdirectories (eg. ./folder/.hiddenSub). [Tested with git bash on windows]