dotfiles/config/scripts/sedrename

48 lines
695 B
Bash
Executable File

#!/usr/bin/env bash
# Rename a list of files with a sed pattern
usage() {
echo "Usage: $0 PATTERN [-d] < filelist"
echo
echo "Arguments:"
echo " PATTERN Sed pattern to apply"
echo
echo "Options:"
echo " -d Dry run"
exit 1
}
if [[ -z "$1" ]]; then
usage
fi
pattern="$1"
dry=1
if [[ -n "$2" ]]; then
if [[ "$2" = '-d' ]]; then
dry=0
else
usage
fi
fi
while read src
do
dst="$(echo "$src" | sed "$pattern")"
if [[ $? != 0 ]]; then
echo "ERREUR Invalid sed pattern"
exit 2
fi
if [[ $dry == 0 ]]; then
echo "$src""$dst"
else
mv -- "$src" "$dst"
fi
done