dotfiles/scripts/sedrename

48 lines
692 B
Plaintext
Raw Normal View History

2017-04-22 18:04:17 +02:00
#!/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