-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_directory.sh
More file actions
executable file
·41 lines (33 loc) · 1.26 KB
/
add_directory.sh
File metadata and controls
executable file
·41 lines (33 loc) · 1.26 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
#!/bin/bash
# Prompt the user for inputs
read -p "Enter the prefix name for the files/directories: " prefix
read -p "Enter the number of items to create: " number
read -p "Enter the file extension (e.g., .txt, .csv) [Leave blank if creating directories]: " extension
read -p "Enter the directory where items should be created: " directory
read -p "Enter the command you want to execute (mkdir | touch): " cmd
# Validate the command
if [[ "$cmd" != "mkdir" && "$cmd" != "touch" ]]; then
echo "Invalid command. Please use 'mkdir' or 'touch'."
exit 1
fi
# Check if the directory exists; if not, create it
if [ ! -d "$directory" ]; then
echo "Directory does not exist. Creating it..."
mkdir -p "$directory"
fi
# Calculate zero padding based on the number of items
padding_length=${#number}
# Create the files or directories
for ((i=1; i<=number; i++))
do
# Generate zero-padded numbers
padded_number=$(printf "%0${padding_length}d" "$i")
item_name="${prefix}${padded_number}${extension}"
if [[ "$cmd" == "mkdir" ]]; then
# Remove the extension for directories
item_name="${prefix}${padded_number}"
fi
$cmd "${directory}/${item_name}"
echo "Created: ${directory}/${item_name}"
done
echo "All items have been created successfully."