-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrename.sh
More file actions
executable file
·52 lines (38 loc) · 1.18 KB
/
rename.sh
File metadata and controls
executable file
·52 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# Directory to process
DIRECTORY="$1"
# Check if the directory exists
if [ ! -d "$DIRECTORY" ]; then
echo "Error: Directory '$DIRECTORY' does not exist."
exit 1
fi
# Function to convert CamelCase to kebab-case
camel_to_kebab() {
echo "$1" | sed -E 's/([a-z])([A-Z])/\1-\2/g' | tr '[:upper:]' '[:lower:]'
}
# Loop through each file in the directory
for filename in "$DIRECTORY"/*; do
# Skip if it's not a file
if [ ! -f "$filename" ]; then
continue
fi
# Get the filename without the directory path
basefilename=$(basename "$filename")
# Separate name and extension
name="${basefilename%.*}"
extension="${basefilename##*.}"
# Convert the name to kebab-case
new_name=$(camel_to_kebab "$name")
# Construct the new filename
new_filename="${new_name}.${extension}"
# Check if the file needs renaming (avoid rename to the same name)
if [ "$basefilename" != "$new_filename" ]; then
# Rename the file
mv "$filename" "$DIRECTORY/$new_filename"
# Print a message
echo "Renamed '$basefilename' to '$new_filename'"
else
echo "Skipping '$basefilename': Already in kebab-case"
fi
done
echo "Finished processing files."