Shell script for changing filename case
I'm looking for a script that will take all files in either the current, or a named, directory, and convert them all to lowercase.
I did a search of LinuxQuestions and came up with what I think is a similar solution. However, could someone confirm this for me, as I am naturally cautious about running any script that carries out file commands without knowing exactly how far it's going to go! Could someone confirm that the following will rename all files in the current directory ONLY to lowercase? I can understand (thanks to the script's comments) what "tr" does, but what's the meaning of the first line? for oldname in `ls`; do # tr translates characters from the first set specified to the corresponding character in the second set #in this case uppercase to lowercase newname=`echo $oldname | tr [:upper:] [:lower:]` mv $oldname $newname Steve |
The 'ls' as used here will use any aliases if defined first, so you should 'unalias ls' first or supply full path or prefix with a backslash. Then spaces in filenames will be handled "better" using say "find -print0|xargs -0" and why not "mv "${oldname}" $(echo "${oldname}" | tr [:upper:] [:lower:])" directly?..
If unsure you could test things either by using some shell flag like Bash "-n" or prefix "mv" with "echo" to just print the result without modifying anything. |
Okay, thanks. And that's a handy tip about using "echo" to see the results of any command before doing it.
|
Actually, I just tried both the original script and your suggested one-line replacement, and neither works. On your one I get the error: mv: missing destination file operand after `'
Can you suggest what is wrong with the command? |
Put the '[[:upper:]]' and '[[:lower:]]' in single quotes.
|
Okay thanks for the prompt reply. But upon Googling for this problem further, I found that there is a Rename command in Linux - and to do this it is "rename 'y/A-Z/a-z/' *", which in my case has worked fine.
Hmm - I didn't know Linux had a rename command. I thought you just used mv to rename files. Dunno where I got that from... |
Rename is not a Linux command, it is a Perl script which happens to be part of most Linux distros. Your script was more "Linux pure". I discovered the existence of renema after writing my own script as well.
Do: less $(which rename) BTW, when scripting it is not uncommon to start a script file like this: $CP=/usr/bin/cp $MV=/usr/bin/mv and in you script use: $MV $oldname $newname In that way it is very easy to change the value of $CP for /bin/echo as to make all calls to cp echos. If you ever write a script involving 'rm' it is even very unwise not to do so. Furthermore by specifying the full path name you make your script independent from the environment. It is a very common problem that even carefully tested scripts won't run from cron -- unless you have all these full path names. jlinkels |
Quote:
eg usage: Code:
# ls -1 |
Okay thanks for the input - plenty of ideas there.
|
All times are GMT -5. The time now is 01:04 PM. |