Sunday, May 5, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 3963  / 1 Year ago, wed, november 30, 2022, 9:31:17

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

More From » command-line

 Answers
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
" "$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:




[#15049] Friday, December 2, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arsleyeuth

Total Points: 72
Total Questions: 121
Total Answers: 112

Location: North Korea
Member since Mon, Oct 31, 2022
2 Years ago
;