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
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.
exclude .? Throw a coin.
exclude long_name? Use -mindepth.
find /path/ ! -path "/path/first" ! -path "/path/second"
is this only way?
find / ! -regex '/\(a\|b\)/.*'
or more simply, pipe through grep. To not recurse, the above would be very inefficient and you should use -prune
: stackoverflow.com/questions/1489277/…
find
with grep
to exclude the directory but the parent directory was still there, causing everything to be deleted anyway.
find
, you'd need to check for prefixes: stackoverflow.com/questions/17959317/… But a Bash for loop can handle it :-)
\!
) to be on the safe side. All the examples in my machine's man find
have it escaped so it seems like it's probably a Good Idea™. Edit — Just noticed it even explicitly says: ! expr True if expr is false. This character will also usually need protection from interpretation by the shell.
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
-exec
option. For example, if you try find dir/* -type d -exec rmdir {} \;
, you will see errors.
rmdir
and are most likely telling you that the directories are not empty since find
will do a depth-first search into the directories, showing the parents before their children.
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]
Pipe it to sed. Don't forget the -r that extend regular expression.
find . -type d | sed -r '/^\.$/d'
Success story sharing
find . -mindepth 1 -maxdepth 1 -type d ...