Fun with SED!

(Found this sitting in my blog’s drafts folder.. only 11 years old but all of it still just as valid today!)

A couple of shell scripts for renaming files on the command line.

# Replaces JPG in any file with jpg 
for ii in * ; do ARG1=`echo $ii | sed 's/JPG/jpg/g'` ; mv $ii $ARG1 ; done

Stany’s renaming scripts:

#!/bin/bash
# Mass REname of english filenames
# relies on fixfilename regexp engine

PATH=$PATH:/Users/stany/bin
echo
for ii in * ; do ARG1=`echo $ii | fixfilename `   ; echo -n "." ; mv "$ii"  "$ARG1" ; done
echo
#!/bin/bash
# the fixfilename regexp engine

PATH=$PATH:/raid0/tools
sed 's@(@@g;s@)@@g;s/ /_/g;s/\,//g;s/\&/and/g;s/_-_/-/g;s/-_/-/g;s/\!//g;s/\;//g;s/\]//g;s/\[//g;s/%20/_/g' 

A combined version…

#!/bin/bash
for ii in * ; do ARG1=`echo $ii | sed 's@(@@g' | sed 's@)@@g' | sed 's/ /_/g' | sed 's/\,//g' | sed 's/\&/and/g' | sed 's/_-_/-/g' | sed 's/-_/-/g' | sed 's/\!//g' |  sed s/\;//g | sed 's/\]//g' |  sed 's/\[//g' | sed 's/%20/_/g'  `  ; echo -n "." ; mv "$ii"  "$ARG1" ; done
echo

This script will remove the first 4 characters of a filename:

for ii in * ; do FOO=`echo $ii | sed 's/^....//g'`; echo mv \"$ii\" \"$FOO\" ; done

This will echo what it actually does, and place quotes in front and after each filename. In the for ii in * bit you can adjust the regex to match the files you need. So for ii in *.txt will match all the files ending in .txt

Remove the echo to run it, or pipe into shell: | sh