How can I append characters to a files name in linux, using a regex? I've seen these two posts

  1. https://stackoverflow.com/questions/6985873/change-names-of-multiple-files-linux
  2. How to rename multiple files by replacing word in file name?

but haven't been able to get the answers to work for me. I'm also not sure if this belongs here, stack overflow or in another sub-site.

I've been trying

for FILE in \d+_\d+\.png; do echo "$FILE" $(echo "$FILE" | sed 's/\./_t\./'); done

but that doesn't seem to recognize the regexed filename as a regex.

So for data samples my files currently are


and I want them to be


The files are scattered amongst five directories. Thanks.

  • One thing is you lost mv command. It should read do mv. – baf Apr 3 '15 at 19:27
  • I want to see the changes before I execute them, just incase I'm doing something stupid. – user3783243 Apr 3 '15 at 19:31
  • Sure. In bash this should work for FILE in [0-9]*_[0-9]*.png. Plus + will not work. – baf Apr 3 '15 at 19:36
  • @baf + works, \d doesn't. + and * are quite different, * will allow the value not be present, + requires at least one occurrence of the value. – user3783243 Apr 6 '15 at 14:55
  • I know the difference between + and *, but plus doesn't work for simple bash expansion. It will work for grep regex, but not in pure bash. See bash glob patterns. Anyway I think your script with my tweaks is the simplest solution, no need for grep and other stuff :) – baf Apr 6 '15 at 19:50
[vagrant@vm-one ~]$ ls
1111_2222.png  1111_4444.png  1111_6666.png
[vagrant@vm-one ~]$ for f in `ls | egrep '[0-9]+_[0-9]+\.png'`; do mv "$f" "${f/.png/_t.png}"; done
[vagrant@vm-one ~]$ ls
1111_2222_t.png  1111_4444_t.png  1111_6666_t.png


  1. The ls | egrep snippet has been retrieved from here

  2. The mv $f ${f/.png/_t.png} based on this answer

  • Perfect, that did it. How does that ${f/.png/_t.png} work? – user3783243 Apr 3 '15 at 19:54
  • Thanks, works great, thanks for the source links as well. – user3783243 Apr 3 '15 at 19:58

This works for me in my test..

shopt -s globstar; 
for i in **; do
    test -f ${i} || continue;
    echo mv $i ${i%%.${ext}}_t.${ext}; 
shopt -u globstar;

It expects you to be in the correct base directory. Note it makes no assumptions as to what extension the file is or what the 'start' of the filename format is. Be careful.


Simple (maybe too much), but also works in this case:

find . -regextype posix-extended \
  -regex '.*[[:digit:]]{4}_[[:digit:]]{4}\.png' \
  -type f -exec rename .png _t.png {} \;
  • This will rename all pngs though, right? – user3783243 Apr 3 '15 at 23:02
  • You can always adjust the regex to your needs. – dawud Apr 3 '15 at 23:05
  • Aha, didn't know about the -regex flag. Thanks. I'm not privileged enough to be able to up vote unfortunately. – user3783243 Apr 6 '15 at 14:20

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.