-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkfile.sh
More file actions
executable file
·88 lines (71 loc) · 1.89 KB
/
Copy pathmkfile.sh
File metadata and controls
executable file
·88 lines (71 loc) · 1.89 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bash
# mkfile - Create nested directories and file in one command
# Version: 1.0.0
# Author: fuyalasmit
# License: MIT
VERSION="1.0.0"
# Display help message
show_help() {
cat << EOF
mkfile - Create nested directories and file in one command
Usage: mkfile [OPTIONS] <path/to/file>
Options:
-h, --help Show this help message
-v, --version Show version information
Examples:
mkfile a/b/c/file.txt
mkfile src/components/Header.jsx
mkfile ~/projects/new/config.json
Description:
Creates all parent directories if they don't exist, then creates
an empty file at the specified path. Will not overwrite existing files.
EOF
}
# Display version
show_version() {
echo "mkfile version $VERSION"
}
# Main function
mkfile() {
# Handle options
case "${1:-}" in
-h|--help)
show_help
return 0
;;
-v|--version)
show_version
return 0
;;
"")
echo "Error: No file path provided" >&2
echo "Usage: mkfile <path/to/file>" >&2
echo "Run 'mkfile --help' for more information" >&2
return 1
;;
esac
local filepath="$1"
# Check if file already exists
if [[ -e "$filepath" ]]; then
echo "Error: '$filepath' already exists" >&2
return 1
fi
# Get directory path
local dirpath
dirpath="$(dirname "$filepath")"
# Create directories
if ! mkdir -p "$dirpath" 2>/dev/null; then
echo "Error: Failed to create directory '$dirpath'" >&2
return 1
fi
# Create file
if ! touch "$filepath" 2>/dev/null; then
echo "Error: Failed to create file '$filepath'" >&2
return 1
fi
echo "✓ Created: $filepath"
}
# If script is executed directly (not sourced), run mkfile
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
mkfile "$@"
fi