-
-
Save tavi-vi/9bbd140aeffd307baaac09b1a54cdfe2 to your computer and use it in GitHub Desktop.
fsed: Mass file renaming using sed syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)