-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.zsh
134 lines (118 loc) · 3.3 KB
/
functions.zsh
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
## A collection of functions I use
extract () {
# Try to determine the type of an archive and extract it
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z e $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
dls() {
# directory ls
ls -l | grep "^d" | awk '{ print $9 }' | tr -d "/"
}
dgrep() {
# A recursive, case-insensitive grep that excludes binary files
grep -iR "$@" * | grep -v "Binary"
}
dfgrep() {
# A recursive, case-insensitive grep that excludes binary files
# and returns only unique filenames
grep -iR "$@" * | grep -v "Binary" | sed 's/:/ /g' | awk '{ print $1 }' | sort | uniq
}
psgrep() {
if [ ! -z $1 ] ; then
echo "Grepping for processes matching $1..."
ps aux | grep $1 | grep -v grep
else
echo "!! Need name to grep for"
fi
}
killit() {
# Kills any process that matches a regexp passed to it
ps aux | grep -v "grep" | grep "$@" | awk '{print $2}' | xargs sudo kill
}
# If I don't have a native version of 'tree' then make a simple one with find and sed
if [ -z "\${which tree}" ]; then
tree () {
find $@ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
}
fi
mcd () {
# make a dir and cd into it
mkdir -p "$@" && cd "$@"
}
# ip/ifconfig functions
exip() {
# fetch my external ip from ifconfig.io
curl ifconfig.io
}
ips() {
# determine local ip address
ip addr | grep 'inet' | awk '{ print $2 }'
}
dot_progress() {
# Fancy progress function from Landley's Aboriginal Linux
# Useful for long rm, tar, etc.
# Usage:
# rm -rfv /foo | dot_progress
local i='0'
local line=''
while read line; do
i="$((i+1))"
if [ "${i}" = '25' ]; then
printf '.'
i='0'
fi
done
printf '\n'
}
escape() {
# Uber useful when you need to translate weird as fuck path into single-argument string.
local escape_string_input
echo -n "String to escape: "
read escape_string_input
printf '%q\n' "$escape_string_input"
}
confirm() {
local answer
echo -ne "zsh: sure you want to run '${YELLOW}$*${NC}' [yN]? "
read -q answer
echo
if [[ "${answer}" =~ ^[Yy]$ ]]; then
command "${@}"
else
return 1
fi
}
confirm_wrapper() {
if [ "$1" = '--root' ]; then
local as_root='true'
shift
fi
local prefix=''
if [ "${as_root}" = 'true' ] && [ "${USER}" != 'root' ]; then
prefix="sudo"
fi
confirm ${prefix} "$@"
}
poweroff() { confirm_wrapper --root $0 "$@"; }
reboot() { confirm_wrapper --root $0 "$@"; }
hibernate() { confirm_wrapper --root $0 "$@"; }
# GREP the onboard wordlist
dict() {
grep "$@" /usr/share/dict/words
}