-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_empty_dirs.sh
executable file
·42 lines (34 loc) · 1.19 KB
/
find_empty_dirs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
# Script to find top-level directories that are either empty or only contain empty subdirectories
# Change directory to the one provided as an argument, or use the current directory
cd "${1:-.}"
# Function to check if a directory contains only empty directories
contains_only_empty_dirs() {
local dir=$1
# If directory is empty, return true
if [ -z "$(ls -A "$dir")" ]; then
return 0
fi
# Check each item in the directory
for item in "$dir"/*; do
# If item is a file, return false
if [ -f "$item" ]; then
return 1
fi
# If item is a non-empty directory, return false
if [ -d "$item" ] && [ -n "$(ls -A "$item")" ]; then
return 1
fi
done
# If all checks pass, return true
return 0
}
# Find and process all directories
find . -type d | while read -r dir; do
if contains_only_empty_dirs "$dir"; then
# Print the directory, stripping the leading './'
echo "${dir#./}"
# Prevent further exploration of its subdirectories
find "$dir" -mindepth 1 -type d -printf "%P\n" | sort -r | xargs -I{} echo "$dir/{}/"
fi
done | sort -r | awk -F/ '!seen[$1]++'