Link to home
Start Free TrialLog in
Avatar of sunilbains
sunilbains

asked on

Renaming files after deleting string at the end

Hi ,
I have set of files which has one XYZ name appended at the end.
I want to remove that XYZ from the file name using KSH or perl. .
for example. filename: 123456.xml.XYZ
I need to change it to  123456.xml only.

There are 100's of such files and all have XYZ at the end.

Can Somebody help plz. ?
Avatar of Tintin
Tintin


#!/bin/ksh
for file in `find /some/path -name "*.XYZ"`
do
  mv $file ${file%%.XYZ}
done

Open in new window

Some distributions ship a tool for that out of the box. Look for an executable called "rename". Syntax varies; in Ubuntu it takes a perl regexp as rule.

Executing

rename "s/XYZ$//" *

would remove XYZ from the end of every file or directory in the current directory.
(I think Tintin got his mv parameters back-to-front)
No, the order's alright.
Oh yeah, my bad, he's REMOVING the XYZ, not adding it.  That'll teach me to post when I'm tired.
its better to use while read loop when going over files to eliminate problems of whitespace filenames. Otherwise, additional quoting must be done for the for loop method

find /path .... | while  read FILE
do
 # do something
done

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial