3

Problem:I have given dir structure

├── kat11
│   ├── kat21
│   │   └── Dokument\ bez\ nazwy
│   └── kat22
│       ├── kat31
│       │   └── Dokument\ bez\ nazwy
│       └── kat32
│           └── Dokument\ bez\ nazwy
├── kat12
│   └── kat21
│       └── Dokument\ bez\ nazwy
├── kat13
│   └── Dokument\ bez\ nazwy
└── kat14
    └── Kat21
        └── Kat32
            └── Dokument\ bez\ nazwy

I want to list only all the child folders with full path.

eg

1. /kat14/kat21/kat32/Dokument\ bez\ nazwy
2. /kat11/kat22/kat31/Dokument\ bez\ nazwy
3. /kat11/kat22/kat32/Dokument\ bez\ nazwy
4. /kat12/kat21/Dokument\ bez\ nazwy
1
6

In a small python script:

 #!/usr/bin/env python3
import os
import sys

src = sys.argv[1]
for root, dirs, files in os.walk(src):
    for dr in dirs:
        directory = root+"/"+dr
        if len([sub for sub in os.listdir(directory) \
                if os.path.isdir(directory+"/"+sub)]) == 0:
            print(directory)

To use it

  • Copy it into an empty file, save it as count_empty.py
  • Run it with the targeted directory as an argument:

    python3 /path/to/count_empty.py '<source_directory>'
    

How it works

  • python's os.walk() walks through directories and sub- directories
  • The line

    if len([sub for sub in os.listdir(directory) if os.path.isdir(directory+"/"+sub)]) == 0:
    

    subsequently counts the number of subdirectories on each of them. If it is zero (no subdirectories), the directory and its path is printed.

Test on a simple package folder on my desktop:

$ '/home/jacob/Bureaublad/pscript_5.py' '/home/jacob/Bureaublad/0.5.3' 
/home/jacob/Bureaublad/0.5.3/nonotifs-0.5.3/manpages
/home/jacob/Bureaublad/0.5.3/nonotifs-0.5.3/miscellaneous
/home/jacob/Bureaublad/0.5.3/nonotifs-0.5.3/icon
/home/jacob/Bureaublad/0.5.3/nonotifs-0.5.3/code
/home/jacob/Bureaublad/0.5.3/nonotifs-0.5.3/debian/source
4

A simple find should be enough:

find /path/to/dir -type d -empty

For example:

$ tree foo
foo
├── 1
│   ├── 1
│   ├── 2
│   └── 3
├── 2
│   ├── 1
│   ├── 2
│   └── 3
└── 3
    ├── 1
    ├── 2
    └── 3

12 directories, 0 files
$ find foo -type d -empty
foo/2/2
foo/2/3
foo/2/1
foo/3/2
foo/3/3
foo/3/1
foo/1/2
foo/1/3
foo/1/1
$ touch foo/1/1/a foo/2/1/a foo/3/1/a 
$ find foo -type d -empty            
foo/2/2
foo/2/3
foo/3/2
foo/3/3
foo/1/2
foo/1/3

If these directories can contain files, then this would be better, but expensive:

find foo -type d -exec sh -c 'find "$0" -mindepth 1 -type d -printf z | grep -q z || printf "%s\n" "$0"' {} \;

This Stack Overflow post has a rather neat solution:

find /path/to/dir -type d -links 2

Since a directory without subdirectories only has two hard links, one two its parent and one to itself.

The question, phrased differently, has been asked before on Stack Overflow, Super User and Unix & Linux:

5
  • On the same directory as I tested it before, it gives me one empty directory, while there are 7 (that is, with no subdirs), does find make a difference in files/folders? May 30 '16 at 8:19
  • @JacobVlijm Your output is weird. It counts a directory with empty subdirectories as empty. O.o
    – muru
    May 30 '16 at 8:21
  • no it doesn't, it counts folders with no subfolders as empty, which was the question. May 30 '16 at 8:22
  • Then why is nonotifs-0.5.3 the first line of your output? It clearly has subfolders in it.
    – muru
    May 30 '16 at 8:23
  • Note that "empty" is not what the question asked for, there can be files in the folder. The command only returns folders with no content at all. May 30 '16 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.