How to find only directories without subdirectories? [duplicate]
Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
Find directories that do not contain subdirectories
4 answers
Is there a way in linux to look through a directory tree for only those directories that are the ends of branches (I will call them leaves here), i.e., dircetories with no subdirectories in them? I looked at this question but it was never properly answered.
So if I have a directory tree
root/
├── branch1
│ ├── branch11
│ │ └── branch111 *
│ └── branch12 *
└── branch2
├── branch21 *
└── branch22
└── branch221 *
can I find only the directories that are the end of their branch (the ones marked with*
), so looking only at the number of directories, not at the number of files? In my real case I am looking for the ones with files, but they're a subset of the 'leaves' that I want to find in this example.
linux shell-script find ls directory-structure
marked as duplicate by Peter Cordes, Community♦ Jan 29 at 12:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 3 more comments
This question already has an answer here:
Find directories that do not contain subdirectories
4 answers
Is there a way in linux to look through a directory tree for only those directories that are the ends of branches (I will call them leaves here), i.e., dircetories with no subdirectories in them? I looked at this question but it was never properly answered.
So if I have a directory tree
root/
├── branch1
│ ├── branch11
│ │ └── branch111 *
│ └── branch12 *
└── branch2
├── branch21 *
└── branch22
└── branch221 *
can I find only the directories that are the end of their branch (the ones marked with*
), so looking only at the number of directories, not at the number of files? In my real case I am looking for the ones with files, but they're a subset of the 'leaves' that I want to find in this example.
linux shell-script find ls directory-structure
marked as duplicate by Peter Cordes, Community♦ Jan 29 at 12:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"
– RoVo
Jan 28 at 13:23
Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.
– alle_meije
Jan 28 at 13:30
so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.
– RoVo
Jan 28 at 14:00
No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.
– alle_meije
Jan 28 at 14:38
2
but isn't that exactly what the most upvoted answer from your linked question does.
– RoVo
Jan 28 at 14:41
|
show 3 more comments
This question already has an answer here:
Find directories that do not contain subdirectories
4 answers
Is there a way in linux to look through a directory tree for only those directories that are the ends of branches (I will call them leaves here), i.e., dircetories with no subdirectories in them? I looked at this question but it was never properly answered.
So if I have a directory tree
root/
├── branch1
│ ├── branch11
│ │ └── branch111 *
│ └── branch12 *
└── branch2
├── branch21 *
└── branch22
└── branch221 *
can I find only the directories that are the end of their branch (the ones marked with*
), so looking only at the number of directories, not at the number of files? In my real case I am looking for the ones with files, but they're a subset of the 'leaves' that I want to find in this example.
linux shell-script find ls directory-structure
This question already has an answer here:
Find directories that do not contain subdirectories
4 answers
Is there a way in linux to look through a directory tree for only those directories that are the ends of branches (I will call them leaves here), i.e., dircetories with no subdirectories in them? I looked at this question but it was never properly answered.
So if I have a directory tree
root/
├── branch1
│ ├── branch11
│ │ └── branch111 *
│ └── branch12 *
└── branch2
├── branch21 *
└── branch22
└── branch221 *
can I find only the directories that are the end of their branch (the ones marked with*
), so looking only at the number of directories, not at the number of files? In my real case I am looking for the ones with files, but they're a subset of the 'leaves' that I want to find in this example.
This question already has an answer here:
Find directories that do not contain subdirectories
4 answers
linux shell-script find ls directory-structure
linux shell-script find ls directory-structure
edited Jan 28 at 19:55
K7AAY
612624
612624
asked Jan 28 at 12:42
alle_meijealle_meije
198117
198117
marked as duplicate by Peter Cordes, Community♦ Jan 29 at 12:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Peter Cordes, Community♦ Jan 29 at 12:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"
– RoVo
Jan 28 at 13:23
Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.
– alle_meije
Jan 28 at 13:30
so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.
– RoVo
Jan 28 at 14:00
No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.
– alle_meije
Jan 28 at 14:38
2
but isn't that exactly what the most upvoted answer from your linked question does.
– RoVo
Jan 28 at 14:41
|
show 3 more comments
please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"
– RoVo
Jan 28 at 13:23
Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.
– alle_meije
Jan 28 at 13:30
so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.
– RoVo
Jan 28 at 14:00
No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.
– alle_meije
Jan 28 at 14:38
2
but isn't that exactly what the most upvoted answer from your linked question does.
– RoVo
Jan 28 at 14:41
please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"
– RoVo
Jan 28 at 13:23
please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"
– RoVo
Jan 28 at 13:23
Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.
– alle_meije
Jan 28 at 13:30
Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.
– alle_meije
Jan 28 at 13:30
so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.
– RoVo
Jan 28 at 14:00
so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.
– RoVo
Jan 28 at 14:00
No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.
– alle_meije
Jan 28 at 14:38
No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.
– alle_meije
Jan 28 at 14:38
2
2
but isn't that exactly what the most upvoted answer from your linked question does.
– RoVo
Jan 28 at 14:41
but isn't that exactly what the most upvoted answer from your linked question does.
– RoVo
Jan 28 at 14:41
|
show 3 more comments
4 Answers
4
active
oldest
votes
To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find
's ! -empty
find rootdir -type d -links 2 ! -empty
Checking the hard links with -links 2
should work for traditional UNIX file systems. The -empty
condition is not part of the POSIX standard, but should be available on most Linux systems.
According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.
3
In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…
– Kamil Maciorowski
Jan 28 at 20:13
@KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.
– Bodo
Jan 29 at 7:51
add a comment |
You could use nested find
and count number of subdirectories:
find . -type d
( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )
Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawnssh
to run a pipeline for every directory (empty or not) in the tree.
– Peter Cordes
Jan 29 at 5:11
add a comment |
With zsh
:
leafdirs=(**/*(ND/Fl2))
Sets the $leafdirs
array with the list of matching leaf directories.
You can print it one per line with:
printf '%sn' $leafdirs
Or loop over them with:
for dir ($leafdirs) something with $dir
That's more or less a translation of @Bodo's GNU find
answer, so like it works only on traditional Unix-like file systems like ext4
that implement .
and ..
as actual directory entries.
**/
: any level of subdirectories(NF/Fl2)
: glob qualifierN
: enablenullglob
for that one glob: don't fail if there's no match. Instead, result in an empty listD
: enabledotglob
for that one glob: look inside hidden dirs, and don't skip hidden files/
: select files of type directory only (like-type d
)F
: select full directories: directories containing at least one entry other than.
and..
(like GNUfind
's! -empty
)l2
: only dirs with a link count of 2 (like-links 2
). One for the dir's entry in its parent directory and one for.
in it. Any subdir would add one to the link count because of the..
entry in those.
add a comment |
If the */
filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.
With find
:
find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print
Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.
The -empty
predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:
find root -type d
-exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
Or, a bit more efficiently,
find root -type d -exec sh -c '
dir=$1
set -- "$dir"/*/
[ -d "$1" ] && exit 1
set -- "$dir"/*
[ -e "$1" ]' sh ; -print
Or, employing the -links
predicate that I had forgotten about (thanks Bodo):
find root -type d
-links 2
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find
's ! -empty
find rootdir -type d -links 2 ! -empty
Checking the hard links with -links 2
should work for traditional UNIX file systems. The -empty
condition is not part of the POSIX standard, but should be available on most Linux systems.
According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.
3
In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…
– Kamil Maciorowski
Jan 28 at 20:13
@KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.
– Bodo
Jan 29 at 7:51
add a comment |
To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find
's ! -empty
find rootdir -type d -links 2 ! -empty
Checking the hard links with -links 2
should work for traditional UNIX file systems. The -empty
condition is not part of the POSIX standard, but should be available on most Linux systems.
According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.
3
In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…
– Kamil Maciorowski
Jan 28 at 20:13
@KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.
– Bodo
Jan 29 at 7:51
add a comment |
To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find
's ! -empty
find rootdir -type d -links 2 ! -empty
Checking the hard links with -links 2
should work for traditional UNIX file systems. The -empty
condition is not part of the POSIX standard, but should be available on most Linux systems.
According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.
To find only those leaf directories that contain files, you can combine an answer of the referenced question https://unix.stackexchange.com/a/203991/330217 or similar questions https://stackoverflow.com/a/4269862/10622916 or https://serverfault.com/a/530328 with find
's ! -empty
find rootdir -type d -links 2 ! -empty
Checking the hard links with -links 2
should work for traditional UNIX file systems. The -empty
condition is not part of the POSIX standard, but should be available on most Linux systems.
According to KamilMaciorowski's comment the traditional link count semantics for directories is not valid for Btrfs. This is confirmed in https://linux-btrfs.vger.kernel.narkive.com/oAoDX89D/btrfs-st-nlink-for-directories which also mentions Mac OS HFS+ as an exception from the traditional behavior. For these file systems a different method is necessary to check for leaf directories.
edited Jan 29 at 7:49
answered Jan 28 at 13:13
BodoBodo
1,476212
1,476212
3
In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…
– Kamil Maciorowski
Jan 28 at 20:13
@KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.
– Bodo
Jan 29 at 7:51
add a comment |
3
In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…
– Kamil Maciorowski
Jan 28 at 20:13
@KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.
– Bodo
Jan 29 at 7:51
3
3
In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…
– Kamil Maciorowski
Jan 28 at 20:13
In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…
– Kamil Maciorowski
Jan 28 at 20:13
@KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.
– Bodo
Jan 29 at 7:51
@KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.
– Bodo
Jan 29 at 7:51
add a comment |
You could use nested find
and count number of subdirectories:
find . -type d
( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )
Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawnssh
to run a pipeline for every directory (empty or not) in the tree.
– Peter Cordes
Jan 29 at 5:11
add a comment |
You could use nested find
and count number of subdirectories:
find . -type d
( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )
Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawnssh
to run a pipeline for every directory (empty or not) in the tree.
– Peter Cordes
Jan 29 at 5:11
add a comment |
You could use nested find
and count number of subdirectories:
find . -type d
( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )
You could use nested find
and count number of subdirectories:
find . -type d
( -exec sh -c 'find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | grep -cz "^" >/dev/null 2>&1' _ ; -o -print )
edited Jan 28 at 14:38
answered Jan 28 at 13:01
RoVoRoVo
3,141216
3,141216
Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawnssh
to run a pipeline for every directory (empty or not) in the tree.
– Peter Cordes
Jan 29 at 5:11
add a comment |
Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawnssh
to run a pipeline for every directory (empty or not) in the tree.
– Peter Cordes
Jan 29 at 5:11
Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns
sh
to run a pipeline for every directory (empty or not) in the tree.– Peter Cordes
Jan 29 at 5:11
Tested on BTRFS, yes this works without relying on link-count=2. But it's not fast or efficient. It spawns
sh
to run a pipeline for every directory (empty or not) in the tree.– Peter Cordes
Jan 29 at 5:11
add a comment |
With zsh
:
leafdirs=(**/*(ND/Fl2))
Sets the $leafdirs
array with the list of matching leaf directories.
You can print it one per line with:
printf '%sn' $leafdirs
Or loop over them with:
for dir ($leafdirs) something with $dir
That's more or less a translation of @Bodo's GNU find
answer, so like it works only on traditional Unix-like file systems like ext4
that implement .
and ..
as actual directory entries.
**/
: any level of subdirectories(NF/Fl2)
: glob qualifierN
: enablenullglob
for that one glob: don't fail if there's no match. Instead, result in an empty listD
: enabledotglob
for that one glob: look inside hidden dirs, and don't skip hidden files/
: select files of type directory only (like-type d
)F
: select full directories: directories containing at least one entry other than.
and..
(like GNUfind
's! -empty
)l2
: only dirs with a link count of 2 (like-links 2
). One for the dir's entry in its parent directory and one for.
in it. Any subdir would add one to the link count because of the..
entry in those.
add a comment |
With zsh
:
leafdirs=(**/*(ND/Fl2))
Sets the $leafdirs
array with the list of matching leaf directories.
You can print it one per line with:
printf '%sn' $leafdirs
Or loop over them with:
for dir ($leafdirs) something with $dir
That's more or less a translation of @Bodo's GNU find
answer, so like it works only on traditional Unix-like file systems like ext4
that implement .
and ..
as actual directory entries.
**/
: any level of subdirectories(NF/Fl2)
: glob qualifierN
: enablenullglob
for that one glob: don't fail if there's no match. Instead, result in an empty listD
: enabledotglob
for that one glob: look inside hidden dirs, and don't skip hidden files/
: select files of type directory only (like-type d
)F
: select full directories: directories containing at least one entry other than.
and..
(like GNUfind
's! -empty
)l2
: only dirs with a link count of 2 (like-links 2
). One for the dir's entry in its parent directory and one for.
in it. Any subdir would add one to the link count because of the..
entry in those.
add a comment |
With zsh
:
leafdirs=(**/*(ND/Fl2))
Sets the $leafdirs
array with the list of matching leaf directories.
You can print it one per line with:
printf '%sn' $leafdirs
Or loop over them with:
for dir ($leafdirs) something with $dir
That's more or less a translation of @Bodo's GNU find
answer, so like it works only on traditional Unix-like file systems like ext4
that implement .
and ..
as actual directory entries.
**/
: any level of subdirectories(NF/Fl2)
: glob qualifierN
: enablenullglob
for that one glob: don't fail if there's no match. Instead, result in an empty listD
: enabledotglob
for that one glob: look inside hidden dirs, and don't skip hidden files/
: select files of type directory only (like-type d
)F
: select full directories: directories containing at least one entry other than.
and..
(like GNUfind
's! -empty
)l2
: only dirs with a link count of 2 (like-links 2
). One for the dir's entry in its parent directory and one for.
in it. Any subdir would add one to the link count because of the..
entry in those.
With zsh
:
leafdirs=(**/*(ND/Fl2))
Sets the $leafdirs
array with the list of matching leaf directories.
You can print it one per line with:
printf '%sn' $leafdirs
Or loop over them with:
for dir ($leafdirs) something with $dir
That's more or less a translation of @Bodo's GNU find
answer, so like it works only on traditional Unix-like file systems like ext4
that implement .
and ..
as actual directory entries.
**/
: any level of subdirectories(NF/Fl2)
: glob qualifierN
: enablenullglob
for that one glob: don't fail if there's no match. Instead, result in an empty listD
: enabledotglob
for that one glob: look inside hidden dirs, and don't skip hidden files/
: select files of type directory only (like-type d
)F
: select full directories: directories containing at least one entry other than.
and..
(like GNUfind
's! -empty
)l2
: only dirs with a link count of 2 (like-links 2
). One for the dir's entry in its parent directory and one for.
in it. Any subdir would add one to the link count because of the..
entry in those.
edited Jan 28 at 17:30
answered Jan 28 at 17:23
Stéphane ChazelasStéphane Chazelas
305k57576930
305k57576930
add a comment |
add a comment |
If the */
filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.
With find
:
find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print
Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.
The -empty
predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:
find root -type d
-exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
Or, a bit more efficiently,
find root -type d -exec sh -c '
dir=$1
set -- "$dir"/*/
[ -d "$1" ] && exit 1
set -- "$dir"/*
[ -e "$1" ]' sh ; -print
Or, employing the -links
predicate that I had forgotten about (thanks Bodo):
find root -type d
-links 2
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
add a comment |
If the */
filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.
With find
:
find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print
Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.
The -empty
predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:
find root -type d
-exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
Or, a bit more efficiently,
find root -type d -exec sh -c '
dir=$1
set -- "$dir"/*/
[ -d "$1" ] && exit 1
set -- "$dir"/*
[ -e "$1" ]' sh ; -print
Or, employing the -links
predicate that I had forgotten about (thanks Bodo):
find root -type d
-links 2
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
add a comment |
If the */
filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.
With find
:
find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print
Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.
The -empty
predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:
find root -type d
-exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
Or, a bit more efficiently,
find root -type d -exec sh -c '
dir=$1
set -- "$dir"/*/
[ -d "$1" ] && exit 1
set -- "$dir"/*
[ -e "$1" ]' sh ; -print
Or, employing the -links
predicate that I had forgotten about (thanks Bodo):
find root -type d
-links 2
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
If the */
filename globbing pattern expands to something that is not the name of a directory, then the current directory has no (non-hidden) subdirectories.
With find
:
find root -type d -exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ; ! -empty -print
Note that this would treat a symbolic link to a directory in a leaf directory as a directory since the pattern would traverse the symbolic link.
The -empty
predicate is not standard, but often implemented. Without it, you would do something similar as with detecting subdirectories:
find root -type d
-exec sh -c 'set -- "$1"/*/; [ ! -d "$1" ]' sh ;
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
Or, a bit more efficiently,
find root -type d -exec sh -c '
dir=$1
set -- "$dir"/*/
[ -d "$1" ] && exit 1
set -- "$dir"/*
[ -e "$1" ]' sh ; -print
Or, employing the -links
predicate that I had forgotten about (thanks Bodo):
find root -type d
-links 2
-exec sh -c 'set -- "$1"/*; [ -e "$1" ]' sh ; -print
edited Jan 28 at 21:36
answered Jan 28 at 13:16
KusalanandaKusalananda
130k17246406
130k17246406
add a comment |
add a comment |
please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"
– RoVo
Jan 28 at 13:23
Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.
– alle_meije
Jan 28 at 13:30
so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.
– RoVo
Jan 28 at 14:00
No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.
– alle_meije
Jan 28 at 14:38
2
but isn't that exactly what the most upvoted answer from your linked question does.
– RoVo
Jan 28 at 14:41