File: rm-empty-dirs

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 210,672 kB
  • sloc: cpp: 291,093; xml: 200,238; sh: 3,779; ansic: 1,447; python: 393; makefile: 240; perl: 82; pascal: 79
file content (26 lines) | stat: -rwxr-xr-x 947 bytes parent folder | download | duplicates (12)
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
#!/bin/sh

# Use this script to tidy up a directory tree after deleting files (e.g.
# when uninstalling MuseScore). The script takes as argument a path (or
# a space-delimited list of paths) to recently deleted files:
#   e.g. "/usr/local/share/mscore-dev-2.1/manual/plugins/harmony.html"
# If the file still exists nothing happens. If it's parent directory is
# empty then that is deleted. The parent's parent is deleted if it is
# now empty, and so on until all empty parents in the path are deleted.

numempty=0

for filepath in "$@" ; do
  #echo "File: $filepath"
  dirpath=$(dirname "$filepath")
  while true ; do
    #echo "Directory: $dirpath"
    rmdir "$dirpath" 2>/dev/null || continue 2
    # Note: this is safe because `rmdir` only removes empty directories.
    #echo "Removed empty directory: $dirpath"
    numempty=$(($numempty+1))
    dirpath=$(dirname "$dirpath")
  done
done

echo "$0: $numempty empty directories were removed."