I'm trying to replace a guid without hyphens from some file names.

I have the regex done, I think, however I can't seem to get either the escaping correct or the replacement commands to work with each other.

Here's my regex



With file names like this


I've seen this answer Bash shell script to locate and remove substring within a filename but it's not quite the same...

I think this should work, but it doesn't complain of an error ?

newname=`echo "$filename" | sed -e 's/\([0-9a-f]{32}\)\.png/\1.png/'`

Using string manipulations in the shell:

for name in *.png; do
    # remove everything after the last '-' including the dash
    # and add the '.png' extension back
    echo mv "$name" "$newname"

This assumes that all files that you'd like to rename are .png files in the current directory.

Run this once and remove the echo if it seems like it's doing the right thing.


In sed BRE (basic regular expressions) you should also escape curly braces {}:

newname=`echo "$filename" | sed 's/-[0-9a-f]\{32\}//g'`

To move/rename the file:

mv "$filename" "$newname"
  • Thanks, although again this doesn't produce an error, that doesn't rename the files – Jules May 21 '17 at 19:59
  • your command doesn't rename the file at all. It was a substitution. You should deal with mv command additionally – RomanPerekhrest May 21 '17 at 20:01
  • should that be piped also? – Jules May 21 '17 at 20:05
  • @Jules, no, line by line, see my update. – RomanPerekhrest May 21 '17 at 20:07
  • Do I need for around it? – Jules May 21 '17 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.