How to find only directories without subdirectories?

6,550

Solution 1

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.

Solution 2

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 \)

Solution 3

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

Solution 4

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 '%s\n' $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 qualifier
  • N: enable nullglob for that one glob: don't fail if there's no match. Instead, result in an empty list
  • D: enable dotglob 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 GNU find'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.
Share:
6,550

Related videos on Youtube

alle_meije
Author by

alle_meije

Updated on September 18, 2022

Comments

  • alle_meije
    alle_meije almost 2 years

    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.

    • pLumo
      pLumo over 5 years
      please clarify: "no matter how many files they contain" vs " I am looking for the ones with files"
    • alle_meije
      alle_meije over 5 years
      Sorry. That comment was because of the previous post, where the answer with the most votes looked for empty directories vs directories with subdirectories.
    • pLumo
      pLumo over 5 years
      so you explicitly want to look for not (!) empty directories ? Then you should accept Bodo's answer.
    • alle_meije
      alle_meije over 5 years
      No, I mean that I am looking for directories that don't contain subdirectories, irrespective of whether or not they contain files.
    • alle_meije
      alle_meije over 5 years
      You're right. I tested this by putting a file in all directories: for f in $(find root -type d);do cd $f;touch test.txt;cd /tmp;done and then the find answer works. The accepted answer returns nothing.
    • alle_meije
      alle_meije over 5 years
      SO "do not recommend deleting questions with answers" -- not sure what to do.
  • Kamil Maciorowski
    Kamil Maciorowski over 5 years
    In Btrfs any directory reports link count of 1. I guess it's not a "normal UNIX file system" then…
  • Peter Cordes
    Peter Cordes over 5 years
    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.
  • Bodo
    Bodo over 5 years
    @KamilMaciorowski Thanks for the hint. I added a paragraph about exceptions.