Suppose we have three files in a directory.
$ ls
abc.txt mno.pyc xyz.cpp
A) Renaming the files to Titlecase
$ ls | while read file
> do
> mv $file `echo $file | sed 's/\<./\u&/'`
> done
They became:
$ ls
Abc.txt Mno.pyc Xyz.cpp
B) Renaming above files to UPPERCASE
$ ls | while read file
> do
> mv $file `echo $file | sed 's/.*/\U&/'`
> done
Just confirm,
$ ls
ABC.TXT MNO.PYC XYZ.CPP
C) Now reverting back to lowercase, renaming them to lowercase
$ ls | while read file
> do
> mv $file `echo $file | sed 's/.*/\L&/'`
> done
Confirmed!
$ ls
abc.txt mno.pyc xyz.cpp
The same sequence of operations in a different way
**A1) To Titlecase
$ ls | while read file
> do
> mv $file `echo $file| awk 'BEGIN{OFS=FS=""}{$1=toupper($1);print}'`
> done
$ ls
Abc.txt Mno.pyc Xyz.cpp
**B1) To UPPERCASE
$ ls | while read file
> do
> mv $file `echo $file|awk '{$1=toupper($1);print}'`
> done
$ ls
ABC.TXT MNO.PYC XYZ.CPP
C1) To lowercase
$ ls | while read file
> do
> mv $file `echo $file|awk '{$1=tolower($1);print}'`
> done
$ ls
abc.txt mno.pyc xyz.cpp
We can also use ‘tr’ command for renaming files to UPPERCASE and lowercase, as
$ echo abc.txt | tr 'a-z' 'A-Z'
ABC.TXT
$ echo "ABC.TXT" | tr 'A-Z' 'a-z'
abc.txt
** Reference for A1) and B1) above. Feel the difference
$ echo "UNIX" | awk '{print $1}'
UNIX
$ echo "UNIX" | awk 'BEGIN{OFS=FS=""} {print $1}'
U
9 comments:
Thanks mate !!
Could you please explain to me the awk 'BEGIN{OFS=FS=""} part. What are OFS and FS? I am a newbie to awk.
thanks for your comment.
OFS and FS are "output field separator" and "field separator" resp. By def. its space or tab.(if you don't mention in BEGIN section)
OFS=FS="" => means the separator is a single character (not space or tab)
Let me know if you need more clarification.
Thanks for explaining. I got it somewhat. Just to make it more clearer, can you please tell me how to achieve the following:
Input string: Srimay
Output: S r i m a y
OR
Input: S r i m a y
Output: S,r,i,m,a,y
Thanks for your help again...
$ echo Srimay | sed -e 's/[a-zA-Z0-9]/,&/g' -e 's/^,//'
S,r,i,m,a,y
$ echo Srimay | sed -e 's/[a-zA-Z0-9]/ &/g' -e 's/^ //'
S r i m a y
//Jadu
Using AWK
$ echo "Srimay" | awk 'BEGIN{FS=""; OFS=" "} {for (i=1;i<=NF;i++) {printf "%s ",$i}}'
S r i m a y
ls | while read file
> do
> mv $file `echo $file | sed 's/\<./\u&/'`
> done
How do you make this work for files with spaces?
roffle lmao lolcat.txt -> Roffle Lmao Lolcat.txt
" sed 's/\<./\u&/g'"
That is awesome, thank you very much!
Excellent! Is it possible to make all file in a directory uppercase but not there extension (ex: image.jpg becoming IMAGE.jpg)
Thanks.
@Vincent, thanks for your comment. Your query is answered here http://unstableme.blogspot.com/2009/09/rename-file-to-uppercase-except.html
Keep in touch. // Jadu
Post a Comment