| 1 | #!/bin/sh |
| 2 | |
| 3 | # Use this script to tidy up a directory tree after deleting files (e.g. |
| 4 | # when uninstalling MuseScore). The script takes as argument a path (or |
| 5 | # a space-delimited list of paths) to recently deleted files: |
| 6 | # e.g. "/usr/local/share/mscore-dev-2.1/manual/plugins/harmony.html" |
| 7 | # If the file still exists nothing happens. If it's parent directory is |
| 8 | # empty then that is deleted. The parent's parent is deleted if it is |
| 9 | # now empty, and so on until all empty parents in the path are deleted. |
| 10 | |
| 11 | numempty=0 |
| 12 | |
| 13 | for filepath in "$@" ; do |
| 14 | #echo "File: $filepath" |
| 15 | dirpath=$(dirname "$filepath") |
| 16 | while true ; do |
| 17 | #echo "Directory: $dirpath" |
| 18 | rmdir "$dirpath" 2>/dev/null || continue 2 |
| 19 | # Note: this is safe because `rmdir` only removes empty directories. |
| 20 | #echo "Removed empty directory: $dirpath" |
| 21 | numempty=$(($numempty+1)) |
| 22 | dirpath=$(dirname "$dirpath") |
| 23 | done |
| 24 | done |
| 25 | |
| 26 | echo "$0: $numempty empty directories were removed." |