I would like to have a regex that will rename my files. Microsoft Windows has changed my files' names and I want to remove the parenthetical datetime substring from the filenames.
Here are some sample filenames:
icon-culture (2015_09_04 06_58_44 UTC).png
icon-disk (2015_09_04 06_58_44 UTC).png
icon-download (2015_09_04 06_58_44 UTC).png
icon-drop (2015_09_04 06_58_44 UTC).png
icon-file (2015_09_04 06_58_44 UTC).png
icon-film (2015_09_04 06_58_44 UTC).png
icon-flag (2015_09_04 06_58_44 UTC).png
icon-folder (2015_09_04 06_58_44 UTC).png
icon-garbage (2015_09_04 06_58_44 UTC).png
icon-graph (2015_09_04 06_58_44 UTC).png
icon-heart (2015_09_04 06_58_44 UTC).png
icon-help (2015_09_04 06_58_44 UTC).png
icon-lock (2015_09_04 06_58_44 UTC).png
icon-map (2015_09_04 06_58_44 UTC).png
icon-media (2015_09_04 06_58_44 UTC).png
icon-money (2015_09_04 06_58_44 UTC).png
icon-monitor (2015_09_04 06_58_44 UTC).png
icon-notes (2015_09_04 06_58_44 UTC).png
icon-openmail (2015_09_04 06_58_44 UTC).png
icon-phone (2015_09_04 06_58_44 UTC).png
icon-photo (2015_09_04 06_58_44 UTC).png
My desired filenames after renaming are:
icon-culture.png
icon-disk.png
icon-download.png
icon-drop.png
icon-file.png
icon-film.png
icon-flag.png
icon-folder.png
icon-garbage.png
icon-graph.png
icon-heart.png
icon-help.png
icon-lock.png
icon-map.png
icon-media.png
icon-money.png
icon-monitor.png
icon-notes.png
icon-openmail.png
icon-phone.png
icon-photo.png
The tutorials that I found didn't do well for me because there are some special characters and numbers.
Best Answer
You can use the
rename
command:s/ \(.*?\)//
is a simple, if broad, expression, matching a space followed by parentheses-enclosed stuff. You can pick more precise expressions like:s/ \(.*?\)\.png$/.png/
- like the previous, but matching only if followed by.png
and the end of the filename, ors/ \(\d{4}(_\d\d){2} \d\d(_\d\d){2} UTC\)\.png/.png/
- matching the date pattern shown in these files, and followed.png
.The
-n
option is for testing the command. Run without it if you're satisfied with the results.