1

Is there a generalized solution for getting only the deepest directories? Counting slash characters won't work, because if I change to --max-depth=4, the deepest directories won't all have the same number of slashes.

The two directories indicated by <<<< are the ones I want.

-bash-4.1$ du --max-depth=3 -h /Database/9.6/backups > x.x
du: cannot read directory `/Database/9.6/backups/lost+found': Permission denied
-bash-4.1$ cat x.x
16K     /Database/9.6/backups/lost+found
142G    /Database/9.6/backups/pgbackrest/archive/localhost  <<<<
142G    /Database/9.6/backups/pgbackrest/archive
4.9T    /Database/9.6/backups/pgbackrest/backup/localhost   <<<<
4.9T    /Database/9.6/backups/pgbackrest/backup
5.0T    /Database/9.6/backups/pgbackrest
5.0T    /Database/9.6/backups
4
  • Please define "deepest". Is it "locally deepest"? I guess not. E.g. in your example lost+found is locally deepest (it has no subdirectories) but you don't want it. So do you want "globally deepest"? In your example lost+found is not globally deepest because its depth is less than some other depth in the entire directory tree in question. This makes sense, but please confirm. If you want "globally deepest", is the depth of the deepest known in advance? Commented Sep 10, 2022 at 5:25
  • @KamilMaciorowski the subject line says "Only print terminal directories", and that's what I want: to only print the terminal directories, when limited by --max-depth=3.
    – RonJohn
    Commented Sep 10, 2022 at 5:29
  • So "directories of certain depth" and the depth is known in advance. Right? Commented Sep 10, 2022 at 5:31
  • @KamilMaciorowski yes, depth is known in advance. That’s demonstrated by use of the —max-depth option in the question.
    – RonJohn
    Commented Sep 10, 2022 at 13:35

1 Answer 1

1

Have you considered locating the directories with find?
It has both -mindepth and -maxdepth :)

$ du -h -cs $(find -maxdepth 4 -mindepth 4 -type d)
4.0K    ./ackups/pgbackrest/backup/localhost
4.0K    ./backups/pgbackrest/archive/localhost
8.0K    total
6
  • That worked. The only change I I had to make was to add 2> /dev/null.
    – RonJohn
    Commented Sep 10, 2022 at 5:13
  • 2
    The unquoted $(find …) will break the solution if pathnames contain spaces and/or other "troublesome" characters. The best fix is find … -print0 | du --files0-from - …. The OP's tools may or may not support these options. Commented Sep 10, 2022 at 5:50
  • @RonJohn if my answer workes for you, consider marking it as the accepted one :)
    – criztovyl
    Commented Sep 10, 2022 at 6:26
  • @criztovyl I try and wait 24 hours to see if other answers arrive.
    – RonJohn
    Commented Sep 10, 2022 at 13:36
  • @KamilMaciorowski good point. The files in that directory have a known naming format, so is not relevant to my current situation.
    – RonJohn
    Commented Sep 10, 2022 at 13:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .