16 lines
628 B
Bash
Executable File
16 lines
628 B
Bash
Executable File
#!/bin/sh -e
|
|
# Flatten folder hierarchies
|
|
# Moves subfolders up one layer, replacing the slash by underscore unless the parent folder has the same name.
|
|
# Args: depth of leaf folders to move up
|
|
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
|
|
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
|
|
find -maxdepth $depth -empty -type d -delete
|