scripts/file_utils/prefixsubdirs.sh

58 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
#
# prefixsubdirs.sh, Copyright © 2018, 2021 Matteo Cypriani
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
# For each directory provided on the command-line, this script prefixes its
# subdirectorie(s) with its name and a separator (" ; " by default).
set -e
#set -x
SEPARATOR=' ; '
MVOPTS='-v'
prefix_subdirs()
(
dir="$1"
cd "$dir"
files="$(echo -- *)"
[ "$files" = '*' ] && return 0
for file in * ; do
[ -d "$file" ] || continue
mv $MVOPTS "$file" "${dir}${SEPARATOR}${file}"
done
)
if [ $# -lt 1 ] ; then
echo "Usage: $(basename "$0") DIRECTORY [DIRECTORY [...]]" >&2
exit 1
fi
while [ -n "$1" ] ; do
dir=$1
shift
if [ ! -d "$dir" ] ; then
printf 'Warning! "%s" is not a directory: ignoring.\n' "$dir" >&2
continue
fi
if [ "$dir" = '.' ] ; then
printf 'Warning! Using "." is not (yet) supported and the outcome is most likely not what you want: ignoring.\n' >&2
continue
fi
printf 'Entering "%s" ...\n' "$dir"
prefix_subdirs "$dir"
done
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4