-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_rename.sh
42 lines (34 loc) · 939 Bytes
/
file_rename.sh
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
#!/bin/bash
# Set the base directory to the current directory
base_dir=$(pwd)
# Define a function to rename the files in a given directory
rename_files() {
# Iterate through all the files in the directory
for file in *
do
# Convert the file name to lowercase and replace spaces with underscores
new_name=$(echo "$file" | tr '[:upper:]' '[:lower:]' | tr ' ' '_')
# Only rename the file if the new name is different from the old name
if [ "$file" != "$new_name" ]
then
mv "$file" "$new_name"
fi
done
}
# Define a function to process a directory
process_dir() {
# Change to the directory
cd "$1"
# Rename the files in the current directory
rename_files
# Iterate through all the subdirectories
for dir in */
do
# Process the subdirectory
process_dir "$dir"
done
# Change back to the base directory
cd "$base_dir"
}
# Process the base directory
process_dir "$base_dir"