Skip to content

Instantly share code, notes, and snippets.

@tavi-vi
Created July 12, 2022 19:42
Show Gist options
  • Save tavi-vi/9bbd140aeffd307baaac09b1a54cdfe2 to your computer and use it in GitHub Desktop.
Save tavi-vi/9bbd140aeffd307baaac09b1a54cdfe2 to your computer and use it in GitHub Desktop.
fsed: Mass file renaming using sed syntax
#!/bin/sh
if [ "$1" = '-f' ]
then
fflag='1'
shift
fi
while read i
do
test "$i" = '' && continue
bn=$(basename "$i")
dn=$(dirname "$i")
nbn=""
nbn=$(echo $bn | sed "$@")
[ "$bn" != "$nbn" ] \
&& cmd="$cmd mv \"$dn/$bn\" \"$dn/$nbn\";"
nfls="$nfls$F$dn/$nbn"
F=':' # don't start with a delimeter
done
if [ ! "$cmd" = "" ]
then
if [ ! "$fflag" = 1 ]
then
{
echo $cmd | sed 's/; /\
/g'
printf "Continue? (y/N): "
read resp </dev/tty
resp=$(echo $resp | sed -E 's/(.).*/\1/')
echo
} 1>&2
case $resp in
Y|y)
;;
N|n|*)
exit 1
;;
esac
fi
eval $cmd
fi
echo $nfls | tr ':' '\n'
@tavi-vi
Copy link
Author

tavi-vi commented Jul 12, 2022

Should work on most posix systems, pipe a list of files to this, and use sed commands and flags to edit the filenames. It prompts you before making changes. Doesn't handle files with newlines or colons in them (Or, it'll try, but will explode)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment