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
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?
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'
Summary: As I understand it, you’re only concerned with the 3 numbers and not the unknown number of words before or after the numbers and skip any file that is already named correctly. So we only need to loo...
Summary: I really need some major help with this Lab Exercise, I need to write a shell script to accept a file name and a directory name from the user. The script should check whether both the file and the dir...
Summary: Hi, I have a newbie question. I have a list of files named like (filename).out in a directory, and I would like to rename all of them into just (filename). How can I write a shell script to do that?...