I\'m trying to use GNU find to find only the directories that contain no other directories, but may or may not contain regular files.
My best guess so far has been:
I just found another solution to this that works on both Linux & macOS (without find -exec
)!
It involves sort
(twice) and awk
:
find dir -type d | sort -r | awk 'a!~"^"$0{a=$0;print}' | sort
sort the find
output in reverse order
use awk
to omit lines if the current line is a prefix of the previous line
sort
them (so it looks like the normal find
output)You can use -links if your filesystem is POSIX compliant (ie, a directory has a link for each subdirectory in it, a link from its parent and a link to self, thus a count of 2 link if it has no subdirectories).
The following command should do what you want:
find dir -type d -links 2
However, it does not seems to work on Mac OS X (as @Piotr mentionned). Here is another version that is slower, but does work on Mac OS X. It is based on his version, with correction to handle whitespace in directory names:
find . -type d -exec sh -c '(ls -p "{}"|grep />/dev/null)||echo "{}"' \;
What about this one ? It's portable and it doesn't depend on finnicky linking counts. Note however that it's important to put root/folder
without the trailing /.
find root/folder -type d | awk '{ if (length($0)<length(prev) || substr($0,1,length(prev))!=prev) print prev; prev=($0 "/") } END { print prev }'
@Sylvian solution didn't work for me on mac os x for some obscure reason. So I've came up with a bit more direct solution. Hope this will help someone:
find . -type d -print0 | xargs -0 -IXXX sh -c '(ls -p XXX | grep / >/dev/null) || echo XXX' ;
Explanation:
ls -p
ends directories with '/'(ls -p XXX | grep / >/dev/null)
returns 0 if there is no directories-print0
&& -0
is to make xargs handle spaces in directory namesI have some oddly named files in my directory trees that confuse awk
as in
@AhmetAlpBalkan 's answer. So I took a slightly different approach
p=;
while read c;
do
l=${#c};
f=${p:0:$l};
if [ "$f" != "$c" ]; then
echo $c;
fi;
p=$c;
done < <(find . -type d | sort -r)
As in the awk
solution, I reverse sort. That way if the directory path is a subpath of the previous hit, you can easily discern this.
Here p
is my previous match, c
is the current match, l
is the length of the current match, f
is the first l
matching characters of the previous match. I only echo
those hits that don't match the beginning of the previous match.
The problem with the awk
solution offered is that the matching of the beginning of the string seems to be confused if the path name contains things such as +
in the name of some of the subdirectories. This caused awk
to return a number of false positives for me.
Here is solution which works on Linux and OS X:
find . -type d -execdir bash -c '[ "$(find {} -mindepth 1 -type d)" ] || echo $PWD/{}' \;
or:
find . -type d -execdir sh -c 'test -z "$(find "{}" -mindepth 1 -type d)" && echo $PWD/{}' \;