Computing.Net > Forums > Linux > shell script- rename file name

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

shell script- rename file name

Reply to Message Icon

Name: cekcool
Date: September 2, 2003 at 22:49:09 Pacific
OS: RedHat Adv Server 2.1
CPU/Ram: 2.4G/1G
Comment:

Can someone show me on to write a script to rename all the file within the same directory to uppercase file name?

e.g :
abc.tex --> ABC.tex
test_one --> TEST_ONE.tex

Thank.



Sponsored Link
Ads by Google

Response Number 1
Name: 3Dave
Date: September 3, 2003 at 04:38:48 Pacific
Reply:

You could do it in perl with the "uc"
command which converts strings to upper
case. Something like:

foreach $file(`ls`){
$newfile = uc ($file);
print `mv $file $newfile`;
}

If you wanted to change just the name
without converting the extension, split the
string at the "." first.


0

Response Number 2
Name: cekcool
Date: September 3, 2003 at 21:11:44 Pacific
Reply:

I am not familiar with perl. Could i save it as re-name.sh then execute it with Linux Shell Command prompt as Shell script?

Also, can you show me on how to split the file name with it?

Thanks


0

Response Number 3
Name: 3Dave
Date: September 4, 2003 at 02:50:29 Pacific
Reply:

You certainly can. Just make sure the
shebang points to the right place and that
you have perl installed on your system
(which you probably do). To find the
location:
$ which perl
On my system it returns "/usr/bin/perl" so
the first line must be "#!/usr/bin/perl"
(as you would do for #!/bin/bash).

FYI a lot of perl scripts end with .pl
instead of .sh, although it doesn't really
matter.

Well, this script works for me:

#!/usr/bin/perl
foreach $file(`ls`){
$file =~ /(.*)\.(.*)/;
($name,$extension) = ($1,$2);
$newname = uc ($name);
chomp ($file);
print `mv $file $newname.$extension`;
}


0

Response Number 4
Name: Iuri Cernov
Date: September 5, 2003 at 21:12:08 Pacific
Reply:

You can use this after command to convert to uppercase:
|tr 'a-z' 'A-Z'

Example:

echo "Iuri" | tr 'a-z' 'A-Z' # IURI
ls -l | tr 'a-z' 'A-Z'


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: shell script- rename file name

Shell Script to rename files www.computing.net/answers/linux/shell-script-to-rename-files/28394.html

Shell script 2 accept file/dir name www.computing.net/answers/linux/shell-script-2-accept-filedir-name/22197.html

Linux shell script www.computing.net/answers/linux/linux-shell-script/11120.html