forked from Prosper-ador/shell_customization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_env.sh
321 lines (277 loc) · 8.33 KB
/
shell_env.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/bin/bash
# Create .bashrc or .zshrc
cat << 'EOF' > ~/.bashrc
# Custom Aliases
alias ll='ls -al'
alias ls='ls -l'
alias where='echo $PWD'
alias c=clear
alias s=source
alias e=exit
alias ..='cd ..'
alias ...='cd ../..'
alias rp=realpath
alias h=history
alias me=whoami
alias pd=passwd
alias l=less
alias install1='sudo apt update && sudo apt install'
alias install2='sudo apt update && sudo apt-get install'
alias install3='sudo apt update && sudo snap install'
alias ua=useradd
alias um=usermod
alias gi='git init'
alias gs='git status'
alias ga='git commit'
alias gcm='git commit -m'
alias gco='git checkout'
alias fig='figlet'
# Custom Functions
ca() { cd "$1" && ls -al; }
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Function to prompt before deleting a file or directory
del() {
# Check if argument is passed (file or directory path)
if [ -z "$1" ]; then
echo "Error: No file or directory specified."
return 1
fi
local target="$1"
# Check if the target is a file or directory
if [ -f "$target" ]; then
# It's a file
read -p "Are you sure you want to delete the file '$target'? (y/n) " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
rm "$target" && echo "File '$target' deleted."
else
echo "File '$target' not deleted."
fi
elif [ -d "$target" ]; then
# It's a directory
read -p "Are you sure you want to delete the directory '$target' and its contents? (y/n) " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
rm -r "$target" && echo "Directory '$target' deleted."
else
echo "Directory '$target' not deleted."
fi
else
echo "Error: '$target' is not a valid file or directory."
return 1
fi
}
# Function to find files and directories in the current directory
finddir() {
local type="$1" # The type argument (file, dir, all)
local name="$2" # The pattern to match (optional)
# Default to search in the current directory if no directory is specified
local search_dir="."
# If no type is specified, default to search for both files and directories
if [ -z "$type" ]; then
type="all"
fi
# Case for searching all files and directories
if [ "$type" == "all" ]; then
if [ -z "$name" ]; then
# If no name is given, list everything in the current directory
ls -alF "$search_dir"
else
# Search for files or directories matching the name pattern
find "$search_dir" -name "$name"
fi
# Case for searching only directories
elif [ "$type" == "dir" ]; then
if [ -z "$name" ]; then
# List all directories in the current directory
find "$search_dir" -type d
else
# Search for directories matching the name pattern
find "$search_dir" -type d -name "$name"
fi
# Case for searching only files
elif [ "$type" == "file" ]; then
if [ -z "$name" ]; then
# List all files in the current directory
find "$search_dir" -type f
else
# Search for files matching the name pattern
find "$search_dir" -type f -name "$name"
fi
else
echo "Usage: finddir [file|dir|all] [name_pattern]"
echo " file - Search for files"
echo " dir - Search for directories"
echo " all - Search for both files and directories"
fi
}
function extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.zip) unzip "$1" ;;
*) echo "Unsupported file type";;
esac
else
echo "'$1' is not a valid file."
fi
}
# Personalized PS1 Prompt
PS1='[e[36m]u@h [e[33m]w [e[32m]$(git branch 2>/dev/null | grep "^*" | colrm 1 2)[e[0m] $ '
# Autocomplete and History Enhancements
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
bind '"e[B": history-search-forward'
bind '"e[A": history-search-backward'
# Enable case-insensitive globbing
shopt -s nocaseglob
EOF
# Create .bash_profile
cat << 'EOF' > ~/.bash_profile
# Source the .bashrc file
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# Display a welcome message
echo "Welcome back, $(whoami)! Today is $(date +'%A, %B %d, %Y')."
EOF
# Create .zshrc (if using Zsh)
cat << 'EOF' > ~/.zshrc
# Custom Aliases
alias ll='ls -al'
alias ls='ls -l'
alias where='echo $PWD'
alias c=clear
alias s=source
alias e=exit
alias ..='cd ..'
alias ...='cd ../..'
alias rp=realpath
alias h=history
alias me=whoami
alias pd=passwd
alias l=less
alias install1='sudo apt update && sudo apt install'
alias install2='sudo apt update && sudo apt-get install'
alias install3='sudo apt update && sudo snap install'
alias ua=useradd
alias um=usermod
alias gi='git init'
alias gs='git status'
alias ga='git commit'
alias gcm='git commit -m'
alias gco='git checkout'
alias fig='figlet'
# Custom Functions
ca() { cd "$1" && ls -al; }
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Function to prompt before deleting a file or directory
del() {
# Check if argument is passed (file or directory path)
if [ -z "$1" ]; then
echo "Error: No file or directory specified."
return 1
fi
local target="$1"
# Check if the target is a file or directory
if [ -f "$target" ]; then
# It's a file
read -p "Are you sure you want to delete the file '$target'? (y/n) " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
rm "$target" && echo "File '$target' deleted."
else
echo "File '$target' not deleted."
fi
elif [ -d "$target" ]; then
# It's a directory
read -p "Are you sure you want to delete the directory '$target' and its contents? (y/n) " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
rm -r "$target" && echo "Directory '$target' deleted."
else
echo "Directory '$target' not deleted."
fi
else
echo "Error: '$target' is not a valid file or directory."
return 1
fi
}
# Function to find files and directories in the current directory
finddir() {
local type="$1" # The type argument (file, dir, all)
local name="$2" # The pattern to match (optional)
# Default to search in the current directory if no directory is specified
local search_dir="."
# If no type is specified, default to search for both files and directories
if [ -z "$type" ]; then
type="all"
fi
# Case for searching all files and directories
if [ "$type" == "all" ]; then
if [ -z "$name" ]; then
# If no name is given, list everything in the current directory
ls -alF "$search_dir"
else
# Search for files or directories matching the name pattern
find "$search_dir" -name "$name"
fi
# Case for searching only directories
elif [ "$type" == "dir" ]; then
if [ -z "$name" ]; then
# List all directories in the current directory
find "$search_dir" -type d
else
# Search for directories matching the name pattern
find "$search_dir" -type d -name "$name"
fi
# Case for searching only files
elif [ "$type" == "file" ]; then
if [ -z "$name" ]; then
# List all files in the current directory
find "$search_dir" -type f
else
# Search for files matching the name pattern
find "$search_dir" -type f -name "$name"
fi
else
echo "Usage: finddir [file|dir|all] [name_pattern]"
echo " file - Search for files"
echo " dir - Search for directories"
echo " all - Search for both files and directories"
fi
}
function extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.zip) unzip "$1" ;;
*) echo "Unsupported file type";;
esac
else
echo "'$1' is not a valid file."
fi
}
# Personalized PS1 Prompt
PROMPT='%F{cyan}%n@%m %F{yellow}%~ %F{green}$(git branch 2>/dev/null | grep "^*" | colrm 1 2)%f %# '
# Autocomplete and History Enhancements
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
bind '"e[B": history-search-forward'
bind '"e[A": history-search-backward'
# Enable case-insensitive globbing
setopt nocaseglob
EOF
echo "Shell environment setup completed. Please restart your terminal or run 'source ~/.bashrc' or 'source ~/.zshrc'."