Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
One liner to lowercase any uppercase file names
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
a23d56
Tux's lil' helper
Tux's lil' helper


Joined: 29 Aug 2003
Posts: 109
Location: Charlotte, NC, USA

PostPosted: Wed Jan 28, 2004 7:51 pm    Post subject: One liner to lowercase any uppercase file names Reply with quote

Code:
find ./ -type f -name \*[[:upper:]]\* | sed -e 's#\(.*/\)\(.*\)$#\"\1\2\" \"\1\L\2\E\"#' | xargs -n 2 -r mv -i -v 2>/dev/null


:idea:

To be safe, first try it without the pipe to xargs, to see what it finds.

If that looks OK, then run it with the pipe to xargs. It will report every name changed.

Then once more, remove the pipe to xargs, run it again, and it will report any files that could not be renamed because there was already a file having the same lowercase name.


Last edited by a23d56 on Sun Feb 01, 2004 3:45 am; edited 3 times in total
Back to top
View user's profile Send private message
meowsqueak
Veteran
Veteran


Joined: 26 Aug 2003
Posts: 1549
Location: New Zealand

PostPosted: Wed Jan 28, 2004 8:40 pm    Post subject: Reply with quote

Nice one-liner. Can I suggest adding '-i' to the mv command, to ensure you never accidentally overwrite an existing file?
Back to top
View user's profile Send private message
a23d56
Tux's lil' helper
Tux's lil' helper


Joined: 29 Aug 2003
Posts: 109
Location: Charlotte, NC, USA

PostPosted: Wed Jan 28, 2004 8:59 pm    Post subject: Reply with quote

meowsqueak wrote:
Nice one-liner. Can I suggest adding '-i' to the mv command, to ensure you never accidentally overwrite an existing file?


Good idea.
Back to top
View user's profile Send private message
snakattak3
Guru
Guru


Joined: 11 Dec 2002
Posts: 468
Location: Seattle

PostPosted: Wed Jan 28, 2004 11:28 pm    Post subject: Reply with quote

You can use this
Code:
for i in *.*; do mv "$i" `echo $i | tr ' ' '_'`; done
to remove spaces from a filename first. Then use a23d56's line or this one
Code:
for i in *.*; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done
. I love how there are 50 different ways to do something in linux :) Mine only works on filenames in the current directory, and not on directory names, only file names.
_________________
Ban Reality TV!
Adopt an Unanswered Post
Back to top
View user's profile Send private message
jesterspet
Apprentice
Apprentice


Joined: 05 Feb 2003
Posts: 215
Location: Atlanta

PostPosted: Thu Jan 29, 2004 2:50 am    Post subject: Reply with quote

These may not be one liners (just like in the previous example can be converted into one line), but they serve a similar purpose:

Unix Tip 2047 - August 9, 2003

To convert the file names from upper case to lower in all subdirectories a simple script can be used:
Code:
for i in $*
do
  mv $i `echo $i | tr A-Z a-z`
done


Unix Tip 2038 - July 31, 2003

This script can be used to convert the file names from upper case to lower case and vice versa.
Code:
typeset -u Lcase
for Ucase in `ls`
do
Lcase=$Ucase
mv  $Ucase $Lcase
done

And to convert from lower case to upper case, just change (typeset -u ) to be ( typeset -l ).


Unix Tip 1986 - June 9, 2003

If you have occasion where you need to translate upper case to lower and vice versa, and happen to run a ksh, enter the following functions into your .profile:
Code:
function trans
{
tr '[:upper:]' '[:lower:]' <$1  > $2
}
function TRANS
{
tr '[:lower:]' '[:upper:]' <$1 > $2
}

The first function will translate all uppercase letters in the first file (argument) to lowercase in the second file.

The second function TRANS does the exact opposite.


Unix Tip 1956 - May 10, 2003

A shorter method for renaming all files in a unix directory from upper to lower case.
Code:
for file in *
do
  mv $file `echo $file | tr [:upper:] [:lower:]`  2>/dev/null
done


Armed with these tid bits & the previous posting, readers should not have many more troubles with upper & lower case files/directories/text. :D
_________________
(X) Yes! I am a brain damaged lemur on crack, and would like to buy your software package for $499.95


Last edited by jesterspet on Thu Jan 29, 2004 2:56 am; edited 1 time in total
Back to top
View user's profile Send private message
a23d56
Tux's lil' helper
Tux's lil' helper


Joined: 29 Aug 2003
Posts: 109
Location: Charlotte, NC, USA

PostPosted: Thu Jan 29, 2004 2:54 am    Post subject: Reply with quote

snakattak3 wrote:
You can use this
Code:
for i in *.*; do mv "$i" `echo $i | tr ' ' '_'`; done
to remove spaces from a filename first


FWIW, my find/sed solution copes with spaces embedded in a filename, and leaves them intact.

Using find also lets you customize the search to suit your needs, with all the power of find.

My first version did not work with subdirectories where a directory name had uppercase characters, because it inadvertently tried to lower case the directory name, not just the file name, and that caused the mv command to fail. But that's fixed now, in the current revision above.
Back to top
View user's profile Send private message
soroh6
Apprentice
Apprentice


Joined: 07 Nov 2002
Posts: 232

PostPosted: Tue May 11, 2004 8:06 am    Post subject: Reply with quote

Code:
emerge mmv
mmv \* \#l1 (I have this aliased to 'lc')


Yes, I know this thread is getting old.

-edit- Oh, yes.. Forgot, lowercase to uppercase.
Code:
mmv \* \#u1

_________________
:: soroh -*~
Back to top
View user's profile Send private message
monsieur
n00b
n00b


Joined: 01 Jul 2005
Posts: 16

PostPosted: Sat May 13, 2006 5:31 pm    Post subject: Reply with quote

hello,
yes i know the topic is old, but none of the solutions posted can deal with *all* filenames.
The correct solution is to use find's -print0 option along with either xargs -0 or read -d $'\0' . This way, you can also deal with newlines in the filenames.
Also, use mv -- to deal with filenames beginning with -.
Here it is all explained : http://wooledge.org/mywiki/BashFaq#faq20
Also, USE MORE QUOTES!!! http://www.grymoire.com/Unix/Quote.html
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum