Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can't write a regex code for that. Microsoft Windows has changed my files' names and I want to remove the date from this names.

How can I do this? (Names are below.)

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

Tutorials didn't do well for me. Because there is some special character and united numbers.

share|improve this question
2  
Please don't pictures of text. Copy the text here, and apply code formatting. – muru Mar 12 at 9:25
    
So essentially you wanna change every icon from icon-something (date).png to icon-something.png ? ( remove parenthesis ) ? – Serg Mar 12 at 9:26
    
I've edited muru. @Serg, I want to change these names like that: icon-monitor (date).png to icon-monitor.png and icon-notes (date).png to icon-notes.png – Interesting Knox Mar 12 at 9:28
    
Have a look on this: tips.webdesign10.com/… – Mostafa Ahangarha Mar 12 at 9:29
    
I've looked up but there is no any example with "complicated strings" and other characters like "(". But I've learned some tricks and my problem has solved. Thank you. – Interesting Knox Mar 12 at 9:44
up vote 13 down vote accepted

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, or
  • s/ \(\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.

share|improve this answer
    
It's working perfectly. Thank you! – Interesting Knox Mar 12 at 9:40
    
There's another very similar rename command shipped by ubuntu: prename. The exact same args should work for it, too, but the implementation is different. – Peter Cordes Mar 12 at 21:34
    
@PeterCordes that is the rename I'm using here (aka perl-rename). The other rename (from util-linux) isn't the default, so I don't mention it unless necessary. – muru Mar 12 at 21:36
2  
On my Ubuntu 15.10: /usr/bin/prename is from the perl package. /usr/bin/rename is a symlink, through /etc/alternatives/rename, to /usr/bin/file-rename from the rename package. That package's description says it's "intended to replace the version currently supplied by the perl package", so I guess it's the better choice. – Peter Cordes Mar 12 at 21:58
    
@PeterCordes Hmm. Guess I should start using new releases more often. – muru Mar 12 at 21:59

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.

share|improve this answer
2  
It's better than other bash reply. Thank you. – Interesting Knox Mar 12 at 10:07

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
share|improve this answer
    
Thank you! But it's complicated for me. :) – Interesting Knox Mar 12 at 9:41

Using bash and parameter expansion, run the following code from the same directory where you have the files.

for file in *.png ; do NAME="${file%%\ \(*}"; EXT="${file##*.}" ; mv "$file" "$NAME"."$EXT"   ; done  

Sample run

$> ls                                                                                                                             
icon-culture (2015_09_04 06_58_44 UTC).png  icon-disk (2015_09_04 06_58_44 UTC).png
$> for file in *.png ; do NAME="${file%%\ \(*}"; EXT="${file##*.}" ; mv "$file" "$NAME"."$EXT"   ; done                               
$> ls
icon-culture.png  icon-disk.png
share|improve this answer
    
Another good way. Thank you! – Interesting Knox Mar 12 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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