Need script for changing DIRECTORY names only to upper case
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Need script for changing DIRECTORY names only to upper case
EDIT: Good start - I meant to say in the title 'change to LOWER case' not upper duhh.
Hello all
I have just installed Ubuntu and am moving a lot of files from my Windows machine.
Now, as I have many, many directories with upper case characters in them, I'd like to change them so they are only lower case. However, I only want the DIRECTORIES changed this way, NOT the filenames (got my reasons).
I've made a script (or I got one off the net I should say ), here:
Code:
#!/bin/bash
ls | while read file
do
new=`echo $file | tr [:upper:] [:lower:]`
mv -v "$file" "$new"
done
This is fine, but does both filenames and directories. Now, if I do ls -F I get the directory list output with directory names appended with a '/' . So perhaps I could check the name string and only echo those that end with a '/'. Except, of course, I have no clue how to do this, and need showing (I've looked, but no joy) .
Or perhaps my whole approach is wrong and it can be done a different way?
EDIT: Good start - I meant to say in the title 'change to LOWER case' not upper duhh.
I have just installed Ubuntu and am moving a lot of files from my Windows machine.
Now, as I have many, many directories with upper case characters in them, I'd like to change them so they are only lower case. However, I only want the DIRECTORIES changed this way, NOT the filenames (got my reasons).
I've made a script (or I got one off the net I should say ), here:
Code:
#!/bin/bash
ls | while read file
do
new=`echo $file | tr [:upper:] [:lower:]`
mv -v "$file" "$new"
done
This is fine, but does both filenames and directories. Now, if I do ls -F I get the directory list output with directory names appended with a '/' . So perhaps I could check the name string and only echo those that end with a '/'. Except, of course, I have no clue how to do this, and need showing (I've looked, but no joy) .
Or perhaps my whole approach is wrong and it can be done a different way?
Code:
for dir in */
do
new=`printf "%s\n" "${dir%/}" | tr [:upper:] [:lower:]`
mv -v "$dir" "$new"
done
acid_kewpie's code didn't work - much appreciated tho m8, as I just spent a couple of hours analysing what you did and finding out more for myself and got it done .
cfaj - maybe tomorrow if I get time I'll see about working yours in just for the learning experience . Thx folks.
Here's mine:
Code:
#!/bin/bash
echo
for i in *; do
if [ -d "$i" ]
then
echo "$i is a directory"
new=`echo $i | tr [:upper:] [:lower:]`
if [[ "$i" == "$new" ]]
then
echo "...but is already lower case, so will not be changed"
echo
else
echo "...changing to lower case."
mv -v "$i" "$new"
echo
fi
else
echo "$i is not a directory"
echo
fi
done
oh yeah, i didn't replace all the "$file"'s with "$i"'s. my code is *much* simpler though... there's no point using intermediate variables like you are, just a waste of space.
oh yeah, i didn't replace all the "$file"'s with "$i"'s. my code is *much* simpler though... there's no point using intermediate variables like you are, just a waste of space.
Intermediate variables serve various useful purposes. They can:
Hehe code wars . Guess it's always like this around here eh? . All good for educating myself though, even 'Mr Sledgehammer' . FWIW, I wanted to put the error checking in my version to help me see what was going on and for practice (not written any code in about 5 years).
One final question - can somebody give me an example of a negative IF conditional line? In C you'd just use "!=". Doesn't seem to work here and I can't find a single example of it (every tutorial I've found just does an '=='). Thx again folks .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.