dotfiles/.local/bin/scripts/denest

18 lines
677 B
Plaintext
Raw Normal View History

2022-06-27 09:24:06 +00:00
#!/bin/sh -e
2024-02-20 18:54:51 +00:00
# Flatten folder hierarchies
2024-05-31 08:57:17 +00:00
# Moves subfolders up N layers, replacing the slash by underscore unless the parent folder has the same name.
# Args:
# - [N - depth of leaf folders to move up]
# - [dirs to run in - default PWD]
2023-04-27 12:28:04 +00:00
depth=$(expr 1 \& $# \| 2)
case $1 in ([0-9]|[0-9][0-9]) depth=$1; shift;; esac
find "$@" -mindepth $depth -maxdepth $depth -depth -type d | while read folder; do
2022-06-27 09:24:06 +00:00
newname="$(echo "$folder" | sed 's|^./||;s|/|_|g')"
mv -v "$folder" "$newname"
parent="$(dirname "$folder")"
test "$(basename "$folder")" = "$(basename "$parent")" &&
rm -d "$parent" &&
mv -v "$newname" "$parent"
done
2024-05-31 08:57:17 +00:00
find "$@" -maxdepth $depth -empty -type d -delete