1

Possible Duplicate:
Batch rename files in Linux

I have a list of 1000 files in a directory in my linux system like this:

01-Star_Network.en.srt  
02-Subsets.en.srt  
03-Function_Comparision.en.srt

I want .en eliminated from all files, and rename them to:

01-Star_Network.srt  
02-Subsets.srt  
03-Function_Comparision.srt

Is there any bash, python, java or PHP script to do this?

1
  • 1
    It should be easy with any one of the tools you mention. The question is... what have you tried?
    – psoft
    Nov 20, 2012 at 11:53

3 Answers 3

1

Just use this command :

rename 's/\.en\.srt$/.srt/' *.srt

rename is Perl rename, it's the default on Ubuntu :

$ LANG=C update-alternatives --display rename
rename - auto mode
  link currently points to /usr/bin/prename
/usr/bin/prename - priority 60
  slave rename.1.gz: /usr/share/man/man1/prename.1.gz
Current 'best' version is '/usr/bin/prename'.

It's installed with perl Ubuntu package itself.

2
  • 1
    This will remove the first instance of ".en.": "01-.en.wikipedia.org.en.srt" will become "01-.wikipedia.org.en.srt". If you want to remove only the final "en", you could do 's/\.en\.srt$/.srt/'.
    – diwhyyyyy
    Nov 20, 2012 at 10:26
  • post editec accordingly Nov 20, 2012 at 10:30
1

The following command will do that:

find . -name '*.en.srt' | sed 'h; s/.en.srt$/.srt/; H; x; s/\n/ /' | xargs -l mv
  • find finds the files to be renamed
  • sed edits the filename
  • xargs calls the mv command for each filename to do the job

To check first what you are doing, try adding 'echo' just before mv, like this:

find . -name '*.en.srt' | sed 'h; s/.en.srt$/.srt/; H; x; s/\n/ /' | xargs -l echo mv
0

rename from util-linux should do that too:

rename .en.srt .srt *.en.srt

Not the answer you're looking for? Browse other questions tagged .