Search code examples
zsh

zmv - rename directories recursively from kebab case to pascel case


I am using the following function to rename files from camel Or pascal case to kebab case given we will ignore consecutive uppercase letters.

function CamelOrPascalToKebab() {

    zmv -Q '(**/)(*[A-Z]*)(/)' '$1${2//(#b)([a-z])([A-Z])/$match[1]-$match[2]}'
    zmv -Q '(**/)(*[A-Z][a-z]*)(/)' '$1${2//(#m)[A-Z][a-z]/${(L)MATCH}}'

}

So, For the following directory structure

% tree
.
├── DDDDDD
├── EmptyFile
├── EmptyFile.adoc
├── FirstDirToRename
│   ├── DDDDDD
│   ├── EmptyFile
│   ├── EmptyFile.adoc
│   ├── fourthDirToRename
│   ├── SecondDirToRename
│   └── ThisIsDDDDD
├── fourthDirToRename
├── SecondDirToRename
└── ThisIsDDDDD

the resulting directory structure after the fuction is run is

% tree
.
├── DDDDDD
├── EmptyFile
├── EmptyFile.adoc
├── first-dir-to-rename
│   ├── DDDDDD
│   ├── EmptyFile
│   ├── EmptyFile.adoc
│   ├── fourth-dir-to-rename
│   ├── second-dir-to-rename
│   └── this-is-DDDDD
├── fourth-dir-to-rename
├── second-dir-to-rename
└── this-is-DDDDD

Now I need a function to go back to the previous structure.

I think the process should be simple

  1. Capitalize First Character
  2. Capitalize Every Character After -
  3. Remove all -

I am trying the following to capitalize the first character of filename

function KebabToPascal () {
    zmv -n -Q '(**/)(*-*)(/)' '$1${(U)2:0:1}${2#?}'
}

working.

What might the complete KebabToPascal function look like?


Solution

  • The function to rename files from kebab case to pascel case

    function kebab-to-pascal () {
        zmv -Q '(**/)(*-*)(/)' '$1${(U)2:0:1}${2#?}'
        zmv -Q '(**/)(*-[a-z]*)(/)' '$1${2//(#m)-[a-z]/${(U)MATCH}}'
        zmv -Q '(**/)(*-*)(/)' '$1${2//-##/}'
    }