LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SED - Need help removing characyers from filenames (https://www.linuxquestions.org/questions/linux-newbie-8/sed-need-help-removing-characyers-from-filenames-938094/)

kgoerbig 04-04-2012 10:48 AM

SED - Need help removing characyers from filenames
 
I have hundreds of .zip N64 games. I want to remove certain characters from the filenames, but I'm terrible wihth SED and regular expressions. The files look like this:

Paperboy (U) [!].zip
Paper Mario (U) [!].zip
Penny Racers (U) [!].zip
Perfect Dark (U) (V1.1) [!].zip
PGAeurotour.zip
Pilotwings64.zip
Pokemon Puzzle League (U) [!].zip
Pokemon Snap Station (U) [!].zip
Pokemon Snap (U) [!].zip
Pokemon Stadium 2 (U) [!].zip
Pokemon Stadium (U) (V1.1) [!].zip
Polaris SnoCross (U) [!].zip
Powerpuff Girls, The - Chemical X-Traction (U) [!].zip
Power Rangers - Lightspeed Rescue (U) [!].zip
Premier Manager 64 (E) [!].zip

I want to remove every instance of these characters from every filename, and remove the spaces.

(U) [!] (E) (V1.1)

Any help would be appreciated.

uhelp 04-04-2012 12:16 PM

Code:

for file in * ; do      # will act on all files in the working dir
    temp=${file%%(*}        #get everthing up to the first "("
    temp2=${temp//' '/_}    # translate spaces to "_"  ; adjust to youre needs
    mv "$file" "${temp2}.zip"    # and rename the file
done

It does not test, whether it is a ".zip"
It only assumes this.

grail 04-04-2012 12:37 PM

Easy enough to insure .zip by adding to the wildcard in the for loop:
Code:

for file in *.zip ; do
Also, no need for the quotes around the space in the underscore translation.

As a precaution to test the script, simply place an echo prior to mv and you will see a print out of what will happen once removed :)


All times are GMT -5. The time now is 07:03 AM.