Lance Pollard
Lance Pollard

Reputation: 79278

How to rename files to param-case in Bash

How to rename a bunch of files in a directory to be param-case with the hyphen?

Here's one that does it in JavaScript, but I'm not sure how to go about this in bash.

Upvotes: 0

Views: 121

Answers (1)

1218985
1218985

Reputation: 8022

Below is an option in bash:

for file in ./* ; do mv "$file" "$(echo $file | sed 's/\(.\)\([A-Z]\)/\1-\2/g' | tr '[:upper:]' '[:lower:]')" ; done

An alternative with perl:

for file in ./* ; do mv "$file" "$(echo $file | perl -ne 'print lc(join("-", split(/(?=[A-Z])/)))')" ; done

Upvotes: 1

Related Questions