1

I am trying to recursively rename all files within a directory and subdirectories to lower-case but leaving the directory names unchanged.

All examples I have found rename both files and directories to lower-case eg:

find ./ -depth -exec rename -n 'y/[A-Z]/[a-z]/' {} ";"

and

find . -type f|while read f; do mv "$f" "$(echo $f|tr '[:upper:]' '[:lower:]')"; done

How can I keep directories unchanged but rename all files?

1 Answers1

2

You get rename on directory names because find command return full path of file names, then rename command done the rename also base on directory name. So if you have a file in directory DIR1/FILE, it will be rename to dir1/file while you don't want to rename the directory.

Here is the command for renaming only files name:

find . -type f -exec rename -n 's:([^/]*$):lc($1):e' {} +

In above command, ([^/]*$) matches only the last part of the path that doesn't contain a / inside a pair of parentheses(...) which it makes the matched part as group of matches. Then translate matched part($1 is index of first matched group) to lowercase with lc() function.

At the you need to ride the -n option to renaming on actual files.

-exec ... {} + is for commands that can take more than one file at a time (eg cat, stat, ls). The files found by find are chained together like an xargs command. This means less forking out and for small operations, can mean a substantial speedup. [answer by @Oli]