Batch Rename Files in Folder – How to Rename Multiple Files


How can I strip out some text from filenames in a folder?

I'm currently attempting:

rename s/"NEWER_(.*?)"//g *

But nothing it getting renamed

I have a parent folder, with a bunch of sub-folders, within are files, that OneDrive thought would be a great idea to append a " (NEWER_timestamp)" to, and I'd like to remove that.

Example File Names:

getyou.ico (NEWER_1417529079.87)
o7pm.ico (NEWER_1417529184.89)
o7th.ico (NEWER_1417529135.81)

Best Answer

Try the following:

find /path/to/parrent-dir -type f -exec rename -n 's:[^/]*(.*) .*$:$1:' {} +

./o7th.ico (NEWER_1417529135.81)                                 renamed as /o7th.ico
./sub-dir (NEWER_1417529135.81)/getyou.ico (NEWER_1417529079.87) renamed as /sub-dir (NEWER_1417529135.81)/getyou.ico
./sub-dir (NEWER_1417529135.81)/o7pm.ico (NEWER_1417529184.89)   renamed as /sub-dir (NEWER_1417529135.81)/o7pm.ico
./getyou.ico (NEWER_1417529079.87)                               renamed as /getyou.ico
./o7pm.ico (NEWER_1417529184.89)                                 renamed as /o7pm.ico
  • All [^/]*(.*) .*$ matches only the last part of the path that doesn't contain a /. And
  • In above regex, (.*) is a group of matching everything after last / and before a space. Its back-reference will be $1.
  • .*$ matches everything to end$ of files name after space.
  • Finally in replacement part of rename s/.../REPLACEMENT/, we just kept the matched group that is between last \ and a space(.*) which is known as group of matches.
Related Question