Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change filenames to lowercase in Ubuntu in all subdirectories [closed]

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!

like image 947
Naterade Avatar asked Oct 24 '12 14:10

Naterade


2 Answers

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.

like image 170
Steve Avatar answered Sep 21 '22 03:09

Steve


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.

like image 39
Andy Ross Avatar answered Sep 19 '22 03:09

Andy Ross