This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
alias.maclin
398 lines (384 loc) · 16.1 KB
/
alias.maclin
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 foldmethod=marker filetype=bash
# ----------------------------------------------------------------------------
# Author: Mubarak Alrashidi (DeaDSouL)
# Package: dsBash
# Info: Setting up your bash environment
# License: GNU/GPL v3
# URL: https://gitlab.com/DeaDSouL/dsBash
# ----------------------------------------------------------------------------
# ------------------------------------------------------------------------------
ds.alias_maclin() { #{{{
echo ""
echo -e "${BRED}Linux & Mac :${NC}"
echo -e "${BRED}-------------${NC}"
echo -e "${BRED}FILE AND FOLDER MANAGEMENT"
echo -e " ${BBLUE}ds.findtext${NC} ${FG}: Find files containing text. ${NC}${YELLOW}(ex: ds.findtext 'keyword')${NC}"
echo -e " ${BBLUE}ds.ptar${NC} ${FG}: Parallel compression. ${NC}${YELLOW}(ex: ds.ptar DIR_NAME)${NC} ${PURPLE}[requires: 'lbzip2']${NC}"
echo -e " ${BBLUE}ds.extract${NC} ${FG}: Extract most known archives.${NC}"
echo -e " ${BBLUE}ds.secure-tar${NC} ${FG}: Secure compressing file(s)/directories with a password using 'OpenSSL'.${NC}"
echo -e " ${BBLUE}ds.mcd${NC} ${FG}: Make and cd into directory.${NC}"
echo -e " ${BBLUE}ds.lsr${NC} ${FG}: Recursive directory listing.${NC}"
echo -e " ${BBLUE}ds.ls${NC} ${FG}: ${PURPLE}'ls'${NC} ${FG}with extra info.${NC}"
echo -e " ${BBLUE}ds.less${NC} ${FG}: Preferred${NC} ${PURPLE}'less'${NC} ${FG}implementation.${NC}"
echo -e " ${BBLUE}ds.cp_p${NC} ${FG}: ${PURPLE}'cp${NC}' ${FG}with progress bar.${NC}"
echo -e " ${BBLUE}ds.cp_stat${NC} ${FG}: ${PURPLE}'cp${NC}' ${FG}with statistics.${NC}"
echo ""
echo -e "${BRED}SEARCHING"
echo -e " ${BBLUE}ds.find${NC} ${FG}: Quickly search for file.${NC}"
echo -e " ${BBLUE}ds.finds${NC} ${FG}: Find file whose name starts with a given string.${NC}"
echo -e " ${BBLUE}ds.finde${NC} ${FG}: Find file whose name ends with a given string.${NC}"
echo -e " ${BBLUE}ds.which${NC} ${FG}: Find executables.${NC}"
echo -e "${BBLUE}ds.findbadimages${NC} ${FG}: Find the corrupted images.${NC}"
echo ""
echo -e "${BRED}PROCESS MANAGEMENT"
echo -e " ${BBLUE}ds.findps1${NC} ${FG}: Search for process.${NC} ${PURPLE}(ps -ef)${NC}"
echo -e " ${BBLUE}ds.findps2${NC} ${FG}: Search for process.${NC} ${PURPLE}(ps aux)${NC}"
echo -e " ${BBLUE}ds.findpid${NC} ${FG}: Find out the pid of a specified process.${NC}"
echo -e " ${BBLUE}ds.findmyps${NC} ${FG}: List processes owned by my user.${NC}"
echo -e " ${BBLUE}ds.topmem${NC} ${FG}: Find memory hogs.${NC}"
echo -e " ${BBLUE}ds.topcpu${NC} ${FG}: Find CPU hogs.${NC}"
echo -e " ${BBLUE}ds.k9${NC} ${FG}: Kill 9 process by process-id.${NC}"
echo -e " ${BBLUE}ds.k9n${NC} ${FG}: Kill 9 process by process-name.${NC}"
echo -e " ${BBLUE}ds.k9user${NC} ${FG}: Kill 9 user by user-name.${NC}"
echo -e " ${BBLUE}ds.getppid${NC} ${FG}: Get parent-process-id of current process-id.${NC}"
echo ""
echo -e "${BRED}NETWORKING"
echo -e " ${BBLUE}ds.myip${NC} ${FG}: Public facing IP Address.${NC}"
echo -e " ${BBLUE}ds.myip2${NC} ${FG}: Get external IP.${NC}"
echo -e " ${BBLUE}ds.oports${NC} ${FG}: Show all listening connections. The open ports.${NC}"
echo -e " ${BBLUE}ds.netcons${NC} ${FG}: Show all open TCP/IP sockets.${NC}"
echo -e " ${BBLUE}ds.osocks${NC} ${FG}: Display open sockets.${NC}"
echo -e " ${BBLUE}ds.osocku${NC} ${FG}: Display only open UDP sockets.${NC}"
echo -e " ${BBLUE}ds.osockt${NC} ${FG}: Display only open TCP sockets.${NC}"
echo -e " ${BBLUE}ds.httdebug${NC} ${FG}: Download a web page and show info on what took time.${NC}"
echo -e " ${BBLUE}ds.wgetlist${NC} ${FG}: Parallel wget urls list in a file.${NC}"
echo -e " ${BBLUE}ds.yt2mp3${NC} ${FG}: Youtube video to mp3 file.${NC}"
echo ""
echo -e "${BRED}SYSTEMS OPERATIONS & INFORMATION"
echo -e " ${BBLUE}ds.cleands${NC} ${FG}: Recursively delete${NC} ${PURPLE}.DS_Store${NC} ${FG}files.${NC}"
echo ""
echo -e "${BRED}MISC."
echo -e " ${BBLUE}ds.colors${NC} ${FG}: Prints the terminal's colors.${NC}"
echo -e " ${BBLUE}ds.tophis${NC} ${FG}: Shows which commands you use the most (based on .bash_history entries).${NC}"
echo -e " ${BBLUE}ds.path${NC} ${FG}: Echo all executable Paths.${NC}"
echo -e " ${BBLUE}ds.path_add${NC} ${FG}: Temporary add location to \$PATH (e.g: ds.path_add $HOME/bin2).${NC}"
echo ""
} #}}}
# ------------------------------------------------------------------------------
# Files & Folders #{{{
# ds.findtext: Find files containing text. #{{{
ds.findtext() {
[[ "$*" ]] && grep -lir "$*" * || echo "[ERROR]: Missing keyword";
} #}}}
# ds.ptar: Parallel compression #{{{
ds.ptar() {
zPath="$1"
zDir=$(basename "$zPath")
zFile="${zDir}.tbz2"
if [[ ! -e "$zPath" ]]; then
echo "[ERROR]: '${zPath}': Doesn't exist."
elif [[ -e "${zFile}" ]]; then
echo "[ERROR]: '${zFile}': Already exists."
else
tar cvf "${zFile}" --use-compress-program lbzip2 "${zPath}"
fi
} #}}}
# ds.extract: Extract most know archives. #{{{
ds.extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*.xz) xz -d $1 ;;
*) echo "'$1' cannot be extracted via ds.extract()" ;;
esac
else
echo "[ERROR]: '$1' is not a valid file"
fi
} #}}}
# ds.secure-tar: Secure tar #{{{
ds.secure-tar () {
showHelp() {
echo "USING: ds.secure-tar"
echo " options:"
echo " c,compress to compress files/dirs."
echo " e,extract to extract a compressed file."
echo " examples:"
echo " ds.secure-tar c dirOrFiles_to_compress"
echo " ds.secure-tar e compressed_file"
}
if [ $# -lt 2 ]; then
showHelp
else
args=("$@")
case $1 in
-c|c|compress|--compress)
# Removing first argument
unset args[0]
# Removing the slash at the end
defOutName="${2%*/}.tar.gz"
# get an input from the user
read -p "Output filename? [${defOutName}] : " userOutName
# decide which name should we use for the output
if [ -z "$userOutName" ]; then
# if it is empty, then used the default one.
outputName="$defOutName"
else
outputName="${userOutName%*/}"
fi
# finally, do the magic
tar -zcf - "${args[@]}" | openssl enc -e -aes256 -out "${outputName}"
;;
-e|e|extract|--extract)
if [ -f "$2" ]; then
openssl enc -d -aes256 -in "$2" | tar -xz
else
showHelp
fi
;;
*) showHelp ;;
esac
fi
} #}}}
# ds.mcd: Make and cd into directory. #{{{
ds.mcd() {
if [[ "$1" ]]; then
mkdir -p "$1" && cd "$1";
else
echo "[ERROR]: Missing directory name."
fi
} #}}}
# ds.lsr: Recursive directory listing. #{{{
alias ds.lsr='ls -R | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\'''
#}}}
# ds.ls: 'ls' with extra info. #{{{
alias ds.ls='ls -FGlAhp'
#}}}
# ds.less: Preferred 'less' implementation. #{{{
alias ds.less='less -FSRXc'
#}}}
# ds.cp_p: #{{{
ds.cp_p() {
# URL: https://chris-lamb.co.uk/posts/can-you-get-cp-to-give-a-progress-bar-like-wget
strace -q -ewrite cp -- "${1}" "${2}" 2>&1 \
| awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d%% [", percent
for (i=0;i<=percent;i++)
printf "="
printf ">"
for (i=percent;i<100;i++)
printf " "
printf "]\r"
}
}
END { print "" }' total_size=$(stat -c '%s' "${1}") count=0
} #}}}
# ds.cp_stat: #{{{
ds.cp_stat () {
# URL: http://unix.stackexchange.com/questions/65077/is-it-possible-to-see-cp-speed-and-percent-copied
# based on `pv`
# aptitude install pv urpmi pv dnf install pv
# yum install pv zypper in pv
tar -cf - "${@: 1: $#-1}" |
pv -s "$( du -cs -BK --apparent-size "${@: 1: $#-1}" |
tail -n 1 |
cut -d "$(echo -e "\t")" -f 1)" |
( cd "${@: $#}"; tar -xf - )
} #}}}
#}}}
# Searching #{{{
# ds.find: Quickly search for file. #{{{
ds.find() { /usr/bin/find . -iname '*'"$@"'*' ; }
#}}}
# ds.finds: Find file whose name starts with a given string. #{{{
ds.finds() { /usr/bin/find . -iname "$@"'*' ; }
#}}}
# ds.finde: Find file whose name ends with a given string. #{{{
ds.finde() { /usr/bin/find . -iname '*'"$@" ; }
#}}}
# ds.which: Find executables. #{{{
alias ds.which='type -all'
#}}}
# ds.findbadimages: #{{{
ds.findbadimages() {
oput='corrupted_'`date +%s%N`'.txt'
# while read f; do
# identify "$f" > /dev/null || echo "$f" >> "${oput}"
# done < <(find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' \))
# < <( ) is a Process Substitution, $() is a command substitution and <<< is a here-string.
# read: http://unix.stackexchange.com/a/52027
# ----------------------------
find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' \) -print0 | while read -d '' -r f; do
identify "$f" > /dev/null || echo "$f" >> "${oput}"
done
# ----------------------------
# for f in $(find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' \)); do
# identify "$f" > /dev/null || echo "$f" >> "${oput}"
# done
# the fastest... althoug not secure
# read: http://askubuntu.com/a/343753
echo "Created: ${oput}"
} #}}}
#}}}
# Process management #{{{
# ds.findps1: Search for process (ef). #{{{
ds.findps1() {
[[ "$1" ]] && ps -ef | grep $1 || echo "[ERROR]: Missing keyword."
} #}}}
# ds.findps2: Search for process (aux). #{{{
ds.findps2() {
[[ "$1" ]] && ps aux | grep $1 || echo "[ERROR]: Missing keyword."
} #}}}
# ds.findpid: Find out the pid of a specified process. #{{{
# -------------------------------------------------------
# Note that the command name can be specified via a regex
# E.g. ds.findpid '/d$/' finds pids of all processes with names ending in
# 'd' Without the 'sudo' it will only find processes of the current user.
# -------------------------------------------------------
ds.findpid () { lsof -t -c "$@" ; }
#}}}
# ds.findmyps: List processes owned by my user. #{{{
alias ds.findmyps="ps $@ -u $USER -o pid,%cpu,%mem,start,time,bsdtime,command ;"
#}}}
# ds.topmem: Find memory hogs. #{{{
alias ds.topmem='ps wwaxm -o pid,stat,vsize,rss,time,command | head -10'
#}}}
# ds.topcpu: Find CPU hogs. #{{{
alias ds.topcpu='ps wwaxr -o pid,stat,%cpu,time,command | head -10'
#}}}
# ds.k9: Kill 9 process by process-id. #{{{
ds.k9() {
if [[ "$1" ]]; then
sudo kill -9 "$1"
else
echo "[ERROR]: Missing PID."
fi
} #}}}
# ds.k9n: Kill 9 process by process-name. #{{{
ds.k9n() {
if [[ "$1" ]]; then
sudo kill -9 $(pgrep "$1")
else
echo "[ERROR]: Missing Process-name."
fi
} #}}}
# ds.k9user: Kill 9 all process by user (logs him out) #{{{
ds.k9user() {
#su -c "pkill -KILL -u $1"
sudo pkill -KILL -u "$1"
# alt: fuser -k /home/USERNAME
} #}}}
# ds.getppid: Get parent-process-id of current process-id. #{{{
ds.getppid() {
[[ "$1" ]] && ps -p ${1:-$$} -o ppid= || echo "[ERROR]: Missing PID."
} #}}}
#}}}
# Networking #{{{
# ds.myip: Get external IP. #{{{
alias ds.myip='curl ip.appspot.com; echo ""'
#}}}
# ds.myip2: Get external IP. #{{{
alias ds.myip2='curl ifconfig.me; echo ""'
#}}}
# ds.oports: Show all listening connections (Open ports). #{{{
alias ds.oports='lsof -i -P | grep LISTEN'
#}}}
# ds.netcons: Show all open TCP/IP sockets. #{{{
alias ds.netcons='lsof -i'
#}}}
# ds.osocks: Display all open sockets. #{{{
alias ds.osocks='sudo /usr/sbin/lsof -i -P'
#}}}
# ds.osocku: Display only open UDP sockets. #{{{
alias ds.osocku='sudo /usr/sbin/lsof -nP | grep UDP'
#}}}
# ds.osockt: Display only open TCP sockets. #{{{
alias ds.osockt='sudo /usr/sbin/lsof -nP | grep TCP'
#}}}
# ds.httdebug: Download a web page and show info on what took time #{{{
ds.httdebug() { /usr/bin/curl $@ -o /dev/null -w "dns: %{time_namelookup} connect: %{time_connect} pretransfer: %{time_pretransfer} starttransfer: %{time_starttransfer} total: %{time_total}\n" ; }
#}}}
# ds.wgetlist: Parallel wget urls list in a file #{{{
ds.wgetlist() {
case "$1" in
-h|--help|h|help)
echo 'USAGE: ds.wgetlist URLS_FILE'
;;
*)
if [[ -z "$1" ]]; then
echo 'Missing: URLs file!'
elif [[ ! -f "$1" ]]; then
echo 'URLs file does not exist!'
else
cat "$1" | parallel -t -v -j 4 --gnu "wget -c {}"
fi
;;
esac
} #}}}
# ds.yt2mp3: Youtube video to mp3 file. #{{{
ds.yt2mp3() {
youtube-dl "$1" --extract-audio --title --audio-format mp3
} #}}}
#}}}
# System Operations & Information #{{{
# ds.cleands: Recursively delete .DS_Store files. #{{{
ds.cleands() {
# Works on linux & mac
find . -type f -name '*.DS_Store' -ls -delete
# find . -type f \( -iname '*DS_Store' -o -name '.AppleDouble' -o -iname '.AppleDB' -o -iname '.AppleDesktop' \) -exec rm -fv {} \;
# find . \( -iname '*DS_Store' -o -name '.AppleDouble' -o -iname '.AppleDB' -o -iname '.AppleDesktop' \) -exec rm -rfv {} \;
# Source: http://osxdaily.com/2012/07/05/delete-all-ds-store-files-from-mac-os-x/
# find . -name ".DS_Store" -depth -exec rm {} \;
} #}}}
#}}}
# Misc. #{{{
# ds.colors: Prints the terminal's colors #{{{
ds.colors() {
local fgc bgc vals seq0
printf "Color escapes are %s\n" '\e[${value};...;${value}m'
printf "Values 30..37 are \e[33mforeground colors\e[m\n"
printf "Values 40..47 are \e[43mbackground colors\e[m\n"
printf "Value 1 gives a \e[1mbold-faced look\e[m\n\n"
# foreground colors
for fgc in {30..37}; do
# background colors
for bgc in {40..47}; do
fgc=${fgc#37} # white
bgc=${bgc#40} # black
vals="${fgc:+$fgc;}${bgc}"
vals=${vals%%;}
seq0="${vals:+\e[${vals}m}"
printf " %-9s" "${seq0:-(default)}"
printf " ${seq0}TEXT\e[m"
printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
done
echo; echo
done
} #}}}
# ds.tophis: Show which commands you use the most. #{{{
alias ds.tophis='cut -f1 -d" " ~/.bash_history | sort | uniq -c | sort -nr | head -n 30'
#}}}
# ds.path: Echo all executable Paths. #{{{
alias ds.path='echo -e ${PATH//:/\\n}'
#}}}
# ds.path_add: Temporary add location to $PATH (e.g: ds.path_add $HOME/bin2) #{{{
ds.path_add() {
if ! [[ "$PATH" =~ "${1}:" ]]; then
[[ -d "${1}" ]] && PATH="${1}:${PATH}"
fi
export PATH
} #}}}
#}}}
# ------------------------------------------------------------------------------