22

I know it's been asked but what I've found has not worked out so far. The closet I came is this : rename -n 'y[A-Z]/[a-z]/' * which works for the current directory. I'm not too good at Linux terminal so what should I add to this command to apply it to all of the files in all the sub-directories from which I am in, thanks!

Naterade
  • 2,635
  • 8
  • 34
  • 54

2 Answers2

34

Here's one way using find and tr:

for i in $(find . -type f -name "*[A-Z]*"); do mv "$i" "$(echo $i | tr A-Z a-z)"; done

Edit; added: -name "*[A-Z]*"

This ensures that only files with capital letters are found. For example, if files with only lowercase letters are found and moved to the same file, mv will display the are the same file error.

Steve
  • 51,466
  • 13
  • 89
  • 103
  • I'm going to try this, but it was just brought to my attention that the require calls in the php files are upper and lowercase in a ton of files. Is there a way to turn off case sensitivity in Ubuntu? – Naterade Oct 24 '12 at 15:59
  • No. Unix filesystems are case sensitive, there is no setting to turn it off. I suppose you could run your app from a FAT32 filesystem as a workaround, but if your source code has case bugs they will need to be fixed manually. – Andy Ross Oct 24 '12 at 17:57
  • This does not work if the directory contains upper case characters; mv fails as the new destination does not exist – Yanick Rochon Sep 11 '15 at 02:18
  • You should also add a warning that this *deletes* some files if they have the same name (e.g., `Foo` vs `FOO`). Ideally, it would issue a warning in this case. – zachaysan Dec 11 '15 at 16:54
  • Also, in order for this to work with spaces, it should be `IFS=$'\n' && for i in $(find . -type f -name "*[A-Z]*"); do echo "$i" "$(echo "$i" | tr A-Z a-z)"; done ; unset IFS` – zachaysan Dec 11 '15 at 17:33
10

Perl has a locale-aware lc() function which might work better:

find . -type f | perl -n -e 'chomp; system("mv", $_, lc($_))'

Note that this script handles whitespace in filenames, but not newlines. And there's no protection against collisions, if you have "ASDF.txt" and "asdf.txt" one is going to get clobbered.

Andy Ross
  • 11,699
  • 1
  • 34
  • 31