Rename file names to lower case, but not dirs 

*Tags*: cmd:find

Newsgroups:  comp.unix.shell
Date:        Tue, 28 Nov 2006 07:16:07 +0000
> I'm trying to rename names of all files under the current dir to lower
> case, but keep dir names as-is.
>
> Is there any way to tweak the following to suit the above goal?
>
>  find . -print0 | xargs -t0 rename 'y/A-Z/a-z/'

I'd do

zmv -Qv '(**/)(*)(.D)' '$1${(L)2}'

(using zsh and its autoloadable zmv function).

Or, as you seem to have GNU find and the perl based rename command:

find . -type f -name '*[[:upper:]]' -print0 -exec rename -v '
 s,(.*/)(.*),$1\L$2,' {} +

If you don't want to recurse into subdirectories, you can use the non-standard -maxdepth or do:

find . ! -name . -prune -type f -name '*[[:upper:]]*' -exec \
 rename '$_=lc' {} +

Stephane CHAZELAS

Rename file names to lower case, but not dirs 

find . -type f ...

Bill Marcum

Solution: Rename file names to lower case, but not dirs 

>> I'm trying to rename names of all files under the current dir to lower
>> case, but keep dir names as-is.
>>
> find . -type f -name '*[[:upper:]]' -print0 -exec rename -v '
>   s,(.*/)(.*),$1\L$2,' {} +

Thanks, that works, although didn't work at my first trial — maybe because missing the ending '*' after [[:upper:]].

Anyway, this is what I used:

find . -type f -exec rename -v 's/[_ ]//g; s,(.*/)(.*),$1\L$2,' {} \;

BTW, Just for the information, both Bill and Adam's suggestion won't work because rename will try to lower case the dir names as well.

T

Solution: Rename file names to lower case, but not dirs 

>  find . -type f -exec rename -v 's/[_ ]//g; s,(.*/)(.*),$1\L$2,' {} \;
>
> BTW, Just for the information, both Bill and Adam's suggestion won't work
> because rename will try to lower case the dir names as well. [...]

And in your solution above it won't work if the dirs have spaces or underscores in their name for the same reason. You could do:

find . -type f -exec rename -v '
   s,[^/]*$,$_=lc$&;s/[_ ]//g;$_,e' {} +

Note the "+" instead of ";" to avoid having to call rename for every file. It should work with any POSIX or Unix conformant find.

Stephane CHAZELAS

Solution: Rename file names to lower case, but not dirs 

>>> Note the "+" instead of ";" to avoid having to call rename for
>>> every file. It should work with any POSIX or Unix conferment find.
>>
>> thx, learned another trick here as well. Now I don't need to do
>>
>>  find . -print0 | xargs -t0 ...
>>
>> to avoid exec invocation on every file any more...
>
> If the found result is extremely long, longer than a command can take as
> parameter, can find -exec + do the same thing as xarg so as to break down
> the parameters in chunks so that the command can handle?

Yes, that's what -exec … {} + is meant to do.

Stephane CHAZELAS