Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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 `'
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...
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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.
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
if you have Python, here's a script you can use
eg usage:
Code:
# ls -1
A-file.txt
C-FILE.TXT
b-FILE.txt
# filerenamer.py -p ".*" -e "[a-z]" -l "*"
==>>>> [ /home/C-FILE.TXT ]==>[ /home/c-file.txt ]
==>>>> [ /home/b-FILE.txt ]==>[ /home/b-file.txt ]
==>>>> [ /home/A-file.txt ]==>[ /home/a-file.txt ]
# filerenamer.py -p ".*" -e "[a-z]" "*" #remove -l to commit
/home/C-FILE.TXT is renamed to /home/c-file.txt
/home/b-FILE.txt is renamed to /home/b-file.txt
/home/A-file.txt is renamed to /home/a-file.txt
# ls -1
a-file.txt
b-file.txt
c-file.txt
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.