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?

Don Smythe
  • 111
  • 4
  • What's the problem with the second one? `-type f` lists regular files only. Directories don't get listed. –  Dec 13 '14 at 08:20
  • 1
    you can also add the `-type f` to the first `find` command which to me looks the simplest, clearest and more easy to remember way to do it. – The Peach Dec 13 '14 at 08:45

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]

αғsнιη
  • 712
  • 2
  • 5
  • 22
  • This still renames the last directory in tree as it iterates though. But can be fixed if change to: find . -name "*.type" -exec rename 's:([^/]*$):lc($1):e' {} + –  Dec 13 '14 at 10:36
  • @DonSmythe Uh, correct I missed `-type f`. fixed that. thank you. – αғsнιη Dec 13 '14 at 10:45