Jay Taylor's notes
back to listing indexUse GNU find to show only the leaf directories
[web search]- Home
-
- Public
- Stack Overflow
- Tags
- Users
- Jobs
-
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:
find dir -type d \( -not -exec ls -dA ';' \)
but this just gets me a long list of "."
Thanks!
-
When using -exec, the {} argument is expanded to the path of the currently inspected filesystem object (file / directory / ...). So you should have used the following command to print the directories : find dir -type d \( -not -exec ls -dA {} \; \) – Sylvain Defresne Nov 24 '10 at 17:47
-
-
Same question on Super User: Using “find” to list only directories with no more childs – Gilles Nov 24 '10 at 23:25
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 "{}"' \;
-
-
@SylvainDefresne, any idea if it will work on NetApp file system over NFS? – Oz123 Aug 12 '13 at 8:57
-
I just used the first version (-links 2) on an NetApp over NFS. So the answer is yes. – Paul Holbrook Aug 1 '14 at 12:37
-
Similarly, the simple soln doesn't seem to work in Cygwin (windows 7), but the extended OSx version does – Eric B. Jan 5 '15 at 19:16
-
in my btrfs system directories have link count 1, so this doesn't work. – miguel.negrao May 19 '16 at 11:03
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
Explanation:
sort the
find
output in reverse order- now you have subdirectories appear first, then their parents
use
awk
to omit lines if the current line is a prefix of the previous line- (this command is from the answer here)
- now you eliminated "all parent directories" (you're left with parent dirs)
sort
them (so it looks like the normalfind
output)- Voila! Fast and portable.
@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 '/'- so
(ls -p XXX | grep / >/dev/null)
returns 0 if there is no directories -print0
&&-0
is to make xargs handle spaces in directory names
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/{}' \;
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 }'
Your Answer
Not the answer you're looking for? Browse other questions tagged bash shell find gnu or ask your own question.
asked |
7 years, 8 months ago |
viewed |
11,843 times |
active |
Get the weekly newsletter! In it, you'll get:
- The week's top questions and answers
- Important community announcements
- Questions that need answers
see an example newsletter
Linked
Related
Hot Network Questions
-
Multiple alignment in math mode without much space
-
Optimal strategy for cutting a sausage?
-
What a Fascinating Thing You Are
-
Why is storing passwords in version control a bad idea?
-
How to express pouring to the very top of a receptacle?
-
Alien Number System
-
Why does PRC devalue its currency on purpose, but Turkey is worried about the devaluation of its currency?
-
Is there an effect comparable to Dimensional Anchor in 5e?
-
Is the discovery of tuff amongst dinosaur fossils in Utah incongruous scientific consensus about dating of the fossils?
-
Late 1960's Comic Book with H shaped space ship
-
Why is peer review so random?
-
Naming in math: from red herrings to very long names
-
How to easily find the coefficients of a cubic polynomial and its plot for the given 4 points
-
What are some ways to explain why a ghoul feeds exclusively on humans?
-
How to avoid a hostile takeover of first authorship during medical leave?
-
Created a user with user add and it's not listed in users list
-
grep with heredoc in function
-
Kid throwing ice cream cone back to the vendor
-
How do you unlock rank 2 and 3 alchemy recipes in World of Warcraft: Battle for Azeroth?
-
How do I communicate to my players that a door is, for the time being, absolutely locked to them?
-
How do you explain to a 5th grader why division by zero is meaningless?
-
Is the proposal to create a US Space Force being taken seriously?
-
How could a public DNS server return bad results
-
Pre-gen Drow Rogue has 0 STR modifier with Strength of 8, why?
site design / logo © 2018 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2018.8.15.31327