-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelete-empty-folders
More file actions
executable file
·33 lines (31 loc) · 1.24 KB
/
Copy pathdelete-empty-folders
File metadata and controls
executable file
·33 lines (31 loc) · 1.24 KB
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
#!/usr/bin/env bash
# Recursively find all empty subfolders in a given directory
# and prompt the user whether to delete every individual empty folder,
# and act accordingly.
# If no directory is given, use the current current working directory.
# Ignore everything inside .git dirs.
#
# Usage:
#
# delete-empty-folders [<ROOT-DIR>]
#
# Examples:
#
# delete-empty-folders # Use the CWD as root dir
# delete-empty-folders ~/Downloads # Use given directory as root dir
#
# author: andreasl
root_folder="${1:-${PWD}}"
# let `find` do the emptiness test and the .git filtering. Note that a parent only holding
# empty folders is not itself empty at scan time and is therefore never offered, not even
# after its children have been deleted - rerun the script to collapse such a nest one level
# at a time.
# The folder list arrives on file descriptor 3, so that stdin stays free for the confirmation
# prompt.
while IFS= read -r -d '' dir <&3; do
echo "$dir"
# -r raw, i.e. don't mangle backslashes; -e newline after input is read;
# -n1 capture 1 character, no ENTER needed
read -r -e -n1 -p "Delete [y/n]?: " do_delete
[ "$do_delete" == "y" ] && rmdir "$dir"
done 3< <(find "$root_folder" -type d -empty -not -path '*/.git/*' -print0)