Skip to Content

Substring Replace Strings In A Filename

This nested for loop substring replaces two parts of the filename GagHstqLEOlv5GU5kEDXmmpAoGQ=257-234.msg1. The script was used to restore missing blobs found in a mailbox backup to the live mailstore.


for d in $(find -type d)
do
  for f in $(ls $d)
  do
    f1=${f/*=}
    f2=${f1/msg*/msg}
    echo "mv -f $d/$f $d/$f2"
  done
done

This site was helpful for figuring this out:
http://tldp.org/LDP/abs/html/string-manipulation.html