Fork me on GitHub

Project Notes

#336 Tidy Folder Names

A little script that can standardise the naming of a set of folders to kebab-case, snake_case, or CamelCase

Notes

Ever want to standardise the way a set of folders are named?

The tidy-folders.sh script does this, handling most cases. It currently only renames folders, and ignores normal files. Perhaps files, folders, or both should be a selection?

$ ./tidy-folders.sh . help
Usage: ./tidy-folders.sh [directory] [kebab-case|snake_case|CamelCase]

It uses a combination of awk, sed, and tr to get the job done.

The core functions are implemented like this:

# Function to convert to kebab-case
to_kebab_case() {
  echo "$1" | sed -E 's/([a-z])([A-Z0-9])/\1-\2/g' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr '_' '-'
}

# Function to convert to snake_case
to_snake_case() {
  echo "$1" | sed -E 's/([a-z])([A-Z0-9])/\1-\2/g' | tr '[:upper:]' '[:lower:]' | tr ' ' '_' | tr '-' '_'
}

# Function to convert to CamelCase
to_camel_case() {
  echo "$1" | awk -F'[-_ .]' '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) substr($i,2)}1' OFS=''
}

Testing

The test.sh runs a series of tests:

$ ./test.sh
# Given folders:
Test Folder with spaces e.g. 1
Test Folder with spaces e.g. 2
test_snake_case_folder_1
test_snake_Case_folder_2
test-kebab-case-folder-1
test-kebab-Case-folder-2
TestCamelCaseFolder1
TestCamelCaseFolder2
## All folders renamed successfully to kebab-case:
test-camel-case-folder-1
test-camel-case-folder-2
test-folder-with-spaces-e.g.-1
test-folder-with-spaces-e.g.-2
test-kebab-case-folder-1
test-kebab-case-folder-2
test-snake-case-folder-1
test-snake-case-folder-2
## All folders renamed successfully to snake_case:
test_camel_case_folder_1
test_camel_case_folder_2
test_folder_with_spaces_e.g._1
test_folder_with_spaces_e.g._2
test_kebab_case_folder_1
test_kebab_case_folder_2
test_snake_case_folder_1
test_snake_case_folder_2
## All folders renamed successfully to CamelCase:
TestCamelCaseFolder1
TestCamelCaseFolder2
TestFolderWithSpacesEG1
TestFolderWithSpacesEG2
TestKebabCaseFolder1
TestKebabCaseFolder2
TestSnakeCaseFolder1
TestSnakeCaseFolder2

Credits and References

About LCK#336 bash

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader