2

How can I convert all filenames and leading directories to titlecase (proper case) using zmv command.

One starting example I found to start with at :

autoload -U zmv
zmv '(*).(*)' '${(C)1}.$2'

The above would not work if files are within subdirectories.

or here

zmv '(**/)(*)~CVS~**/CVS' '${(C)1}${(L)2}'

To recursively lowercase files and directories where the name is not CVS. This one would try to copy lower-case filenames to title-case directories (which does not yet exist and hence would not work either.

The following works well if trying to convert to lowercase (from https://stackoverflow.com/a/152741/631775):

find my_root_dir -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;

But I would like to make proper case here.

4ae1e1
  • 7,228
  • 8
  • 44
  • 77
jethar
  • 2,223
  • 2
  • 22
  • 19

2 Answers2

1

This uses the Perl-script version of rename AKA prename.

find -depth -execdir rename 's/.*/\L$&/;s/[[:lower:]]+/\u$&/g' {} \;

It lowercases the whole name then uppercases the first letter of each word.

Since all characters are already lowercase after the first s command, [[:lower:]]+ could be simplified to .+.

Note that "word" is loosely defined, for example, "ABC6DEF7GHI" would become "Abc6Def7Ghi".

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
0

The following seemed to work for me :

zmv '(**/)(*)(**)' '$1${(C)2}'

Is there any issue in using this, some outlier case which is not covered. Also could someone please provide a rename version as well if possible.

4ae1e1
  • 7,228
  • 8
  • 44
  • 77
jethar
  • 2,223
  • 2
  • 22
  • 19