How to rename files to exclude the datetime stamp?
You can use the rename
command:
$ rename -n 's/ \(.*?\)//' *.png
icon-culture (2015_09_04 06_58_44 UTC).png renamed as icon-culture.png
icon-disk (2015_09_04 06_58_44 UTC).png renamed as icon-disk.png
icon-download (2015_09_04 06_58_44 UTC).png renamed as icon-download.png
icon-drop (2015_09_04 06_58_44 UTC).png renamed as icon-drop.png
icon-file (2015_09_04 06_58_44 UTC).png renamed as icon-file.png
icon-film (2015_09_04 06_58_44 UTC).png renamed as icon-film.png
icon-flag (2015_09_04 06_58_44 UTC).png renamed as icon-flag.png
icon-folder (2015_09_04 06_58_44 UTC).png renamed as icon-folder.png
icon-garbage (2015_09_04 06_58_44 UTC).png renamed as icon-garbage.png
icon-graph (2015_09_04 06_58_44 UTC).png renamed as icon-graph.png
icon-heart (2015_09_04 06_58_44 UTC).png renamed as icon-heart.png
icon-help (2015_09_04 06_58_44 UTC).png renamed as icon-help.png
icon-lock (2015_09_04 06_58_44 UTC).png renamed as icon-lock.png
icon-map (2015_09_04 06_58_44 UTC).png renamed as icon-map.png
icon-media (2015_09_04 06_58_44 UTC).png renamed as icon-media.png
icon-money (2015_09_04 06_58_44 UTC).png renamed as icon-money.png
icon-monitor (2015_09_04 06_58_44 UTC).png renamed as icon-monitor.png
icon-notes (2015_09_04 06_58_44 UTC).png renamed as icon-notes.png
icon-openmail (2015_09_04 06_58_44 UTC).png renamed as icon-openmail.png
icon-phone (2015_09_04 06_58_44 UTC).png renamed as icon-phone.png
icon-photo (2015_09_04 06_58_44 UTC).png renamed as icon-photo.png
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.
You could try the following python code snippet
import os
import glob
files = glob.glob('*')
for file in files:
var1 = file.find(' (')
var2 = file.find(')')+1
filename = file[:var1] + file[var2:]
os.rename(file, filename)
- glob finds all files which satisfy the regex argument
- You iterate through the list and modify the name of the file
rename
changes the name of the file
Using bash
parameter expansion:
for file in *.png; do mv -i "$file" "${file%% *}".png; done
${file%% *}
will discard the unwanted portion of the filename starting from space. Then the extension .png
is added after the filename while mv
-ing.