LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   renaming directories from upper case to lower case, help!! (https://www.linuxquestions.org/questions/linux-newbie-8/renaming-directories-from-upper-case-to-lower-case-help-626343/)

linux_teller 03-07-2008 01:54 AM

renaming directories from upper case to lower case, help!!
 
Hello Linux Gurus,
I am pretty new to linux but have lot of passion to learn, I stumbledupon a script from a website and I couldn't possibly understand the hilighlighted peice of code. I really appreciate if you could help me understand this syntax.

having trouble with the following code
g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`

The complete code

for f in *; do
g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
mv "$f" "$g"
done

This code was found in the following url
webxadmin.free.fr/article/shell-rename-all-files-in-subdirectories-to-lowe-135.php

ghostdog74 03-07-2008 03:03 AM

Code:

# a=string
# echo $a | awk '{print toupper($0)}'
STRING


linux_teller 03-07-2008 03:15 AM

Thanks but I want to understand the higlighted code.
 
Quote:

Originally Posted by ghostdog74 (Post 3080857)
Code:

# a=string
# echo $a | awk '{print toupper($0)}'
STRING


Hi, I really appreciate your awk command reference, but I want to understand the command highlighted in red from my original post.

g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`


I appreciate gurus help here.

Thanks
teller

indeliblestamp 03-07-2008 05:15 AM

Actually I'm not getting any difference whether or not I put the 'xxx' part is there in the expr section.
I made these 3 files: F ILE1 FIL -E2 fIlE3
Now I run the script given in that link (last line is replaced with echo instead of mv):
Code:

bash-2.05b ~/scrap$ for f in *; do
> g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
> echo $g
> done
f ile1
fil -e2
file3

And now if I remove the 3 x's in line 2:
Code:

bash-2.05b ~/scrap$  for f in *; do
> g=`expr "$f" : '\(.*\)' | tr '[A-Z]' '[a-z]'`
> echo $g
> done
f ile1
fil -e2
file3

Its the same! This link gives the same example and seems to imply that this usage of expr can handle control characters and whitespace better than simpler scripts. I'm guessing that's what the xxx is for. Apart from that the explanation is fairly simple I guess.
Code:

g=`expr "$f" : '\(.*\)' | tr '[A-Z]' '[a-z]'`
expr "$f" : '\(.*\)' This part saves the entire value of $f. Usual regex stuff here.
| tr '[A-Z]' '[a-z]'... and passes it to the tr commands which changes the case.
So maybe someone else here knows how prefixing 'xxx' in the regex helps.


All times are GMT -5. The time now is 11:10 AM.