Несколько наивный подход:
#!/bin/sh
for dir do
# get list of directories under the directory $dir
set -- "$dir"/*/
# if there are more than one, continue with the next directory
# also continue if we didn't match anything useful
if [ "$#" -gt 1 ] || [ ! -d "$1" ]; then
continue
fi
# the pathname of the subdirectory is now in $1
# move everything from beneath the subdirectory to $dir
# (this will skip hidden files)
mv "$1"/* "$dir"
# remove the subdirectory
# (this will fail if there were hidden files)
rmdir "$1"
done
Использованиеbash
:
#!/bin/bash
for dir do
# get list of directories under the directory $dir
subdirs=( "$dir"/*/ )
# if there are more than one, continue with the next directory
# also continue if we didn't match anything useful
if [ "${#subdirs[@]}" -gt 1 ] || [ ! -d "${subdirs[0]}" ]; then
continue
fi
# the pathname of the subdirectory is now in ${subdirs[0]}
# move everything from beneath the subdirectory to $dir
# (this will skip hidden files)
mv "{subdirs[0]}"/* "$dir"
# remove the subdirectory
# (this will fail if there were hidden files)
rmdir "${subdirs[0]}"
done
Оба сценария будут запускаться как
$./script.sh dir1 dir2 dir3
или
$./script.sh */
, чтобы запустить его во всех каталогах в текущем каталоге.
Помимо предостережений в коде, это также не приведет к перелинковке символических ссылок. Для этого вам придется пройтись по всем возможным местам в файловой системе и найти ссылки, указывающие на подкаталог в /folder
, и воссоздать их так, чтобы они указывали на правильное новое место. Я не буду писать код далеко что здесь.
Кроме того, при перемещении элементов из подкаталога не выполняется проверка, чтобы убедиться, что в /folder
нет записей с такими же именами.