blob: a4b833f6804271507255149c7930f2217ad2a9d9 (
plain)
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
34
35
36
37
38
|
#!/bin/sh
# flatten the directory tree
# example: dir1/dir2/file1 -> dir1_dir2_file1
DIR="${1:-.}"
_flatten(){
cmd="$1"
find "$DIR" -mindepth 2 -type f -exec bash -c 'file=${1#./}; '"$cmd"' "$file" "${file//\//_}"' _ '{}' \;
}
toflat=$(_flatten "echo mv")
if [ -z "$toflat" ]; then
echo "Abort: nothing to do"; exit
fi
echo "Flattening will do:"
echo "$toflat"
printf "Proceed? [y/N] "
read -r yn
case $yn in
[Yy]* ) ;;
* ) echo "Abort" ; exit;;
esac
_flatten "mv"
todel="$(find . -type d -empty -print)"
[ -z "$todel" ] && exit
echo "Empty subdirectories:"
echo "$todel"
printf "Remove subdirectories? [y/N]"
read -r yn
case $yn in
[Yy]* ) find . -type d -empty -delete;;
* ) echo "Abort" ; exit;;
esac
|