Change filenames to lowercase in Ubuntu in all sub

2019-02-01 21:31发布

问题:

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!

回答1:

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.



回答2:

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.