1753417 Members
5073 Online
108793 Solutions
New Discussion юеВ

Re: altering file name

 
SOLVED
Go to solution
Shaf_1
Advisor

altering file name

Hello,

I have a number of files that I need to alter the file name. The current file names are similar to this:

XYZ8976
XYZ7654
XYZ8765

What I want to do is put a underscore (_) after the charater 'Z' so I would want the files names to look like this:

XYZ_8976
XYZ_7654
XYZ_8765

How could I do this in UNIX?

Thanks,

Shaf
2 REPLIES 2
Alexander Chuzhoy
Honored Contributor
Solution

Re: altering file name

if all the files are in the same folder and begin with XYZ the following perl script will do the work,copy it to a file and execute "perl filename".Define the actual folder with XYZ files instead $folder:
chdir $folder;
foreach my $file (glob "XYZ*") {
my $newfile = $file;
$newfile =~ s/^XYZ/XYZ_/;
if (-e $newfile)
{
warn "can't rename $file to $newfile: $newfile exists\n";
}
else
{
rename $file, $newfile)
}
}
Shaf_1
Advisor

Re: altering file name

Solution was provided.