-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
187 lines (166 loc) · 4.96 KB
/
.functions
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
# Make a directory and `cd` into it
mkd() {
mkdir -p "$@" && cd "$_"
}
# `cd` into a directory and `ls`
cdl() {
cd "$1" && ls
}
# Jump up `n` directories
up() {
local ups=""
for i in $(seq 1 ${1:-1}); do
ups=$ups"../"
done
cd $ups
}
# `man` that supports shell builtins
# Source: https://unix.stackexchange.com/a/18088
man() {
case "$(type -t "$1"):$1" in
# built-in
builtin:*) help "$1" | "${PAGER:-less}";;
# pattern
*[[?*]*) help "$1" | "${PAGER:-less}";;
# something else, presumed to be an external command or options for the man
# command or a section number
*) command -p man "$@";;
esac
}
# Get the weather
weather() {
# If no $1, use the zip code from our IP
# If no $2, use "q0n" as URL params
curl -s -H "Accept-Language: ${LANG%_*}" http://wttr.in/${1:-$(curl -s http://ipinfo.io/postal)}?${2:-q0n}
}
# `tre` is a shorthand for `tree` with hidden files and color enabled, ignoring
# the `.git` directory, listing directories first. The output gets piped into
# `less` with options to preserve color and line numbers, unless the output is
# small enough for one screen.
# Source: https://github.com/mathiasbynens/dotfiles
tre() {
tree -aC -I '.git|node_modules|bower_components' --dirsfirst "$@" | less -FRNX;
}
# Select a range of a file by line numbers
# Usage: selrg 5 10 file.txt
selrg() {
sed -n "${1},${2}p" "${3}"
}
# Kill all processes on a specified port
# Source: https://stackoverflow.com/a/5043907
killport() {
if [ -z "${1}" ]; then
echo "Usage: \`killport <port>\`"
return 1
fi
lsof -i tcp:${1} | awk 'NR!=1 {print $2}' | xargs kill
}
# Determine size of a file or total size of a directory
# Source: https://github.com/mathiasbynens/dotfiles
fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh;
else
local arg=-sh;
fi
if [[ -n "$@" ]]; then
du $arg -- "$@";
else
du $arg .[^.]* ./*;
fi;
}
# Compare original and gzipped file size
# Source: https://github.com/necolas/dotfiles
gz() {
local origsize=$(wc -c < "$1")
local gzipsize=$(gzip -c "$1" | wc -c)
local ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l)
printf "orig: %d bytes\n" "$origsize"
printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio"
}
# General "extract" function
# Source: https://github.com/xvoland/Extract
extract() {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
echo " extract <path/file_name_1.ext> [path/file_name_2.ext] [path/file_name_3.ext]"
return 1
else
for n in $@
do
if [ -f "$n" ] ; then
case "${n%,}" in
*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
tar xvf "$n" ;;
*.lzma) unlzma ./"$n" ;;
*.bz2) bunzip2 ./"$n" ;;
*.rar) unrar x -ad ./"$n" ;;
*.gz) gunzip ./"$n" ;;
*.zip) unzip ./"$n" ;;
*.z) uncompress ./"$n" ;;
*.7z|*.arj|*.cab|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.rpm|*.udf|*.wim|*.xar)
7z x ./"$n" ;;
*.xz) unxz ./"$n" ;;
*.exe) cabextract ./"$n" ;;
*)
echo "extract: '$n' - unknown archive method"
return 1
;;
esac
else
echo "'$n' - file does not exist"
return 1
fi
done
fi
}
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression
# Source: https://github.com/mathiasbynens/dotfiles
targz() {
local tmpFile="${@%/}.tar";
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || return 1;
size=$(
stat -f"%z" "${tmpFile}" 2> /dev/null; # macOS `stat`
stat -c"%s" "${tmpFile}" 2> /dev/null; # GNU `stat`
);
local cmd="";
if (( size < 52428800 )) && hash zopfli 2> /dev/null; then
# the .tar file is smaller than 50 MB and Zopfli is available; use it
cmd="zopfli";
else
if hash pigz 2> /dev/null; then
cmd="pigz";
else
cmd="gzip";
fi;
fi;
echo "Compressing .tar ($((size / 1000)) kB) using \`${cmd}\`…";
"${cmd}" -v "${tmpFile}" || return 1;
[ -f "${tmpFile}" ] && rm "${tmpFile}";
zippedSize=$(
stat -f"%z" "${tmpFile}.gz" 2> /dev/null; # macOS `stat`
stat -c"%s" "${tmpFile}.gz" 2> /dev/null; # GNU `stat`
);
echo "${tmpFile}.gz ($((zippedSize / 1000)) kB) created successfully.";
}
# Upload a file to a hastebin
# Modified from: https://github.com/seejohnrun/haste-client#lightweight-alternative
# Used like: cat file.txt | haste or haste < file.txt
haste() {
a=$(cat);
b=$(curl -X POST -s -d "$a" "${HASTE_SERVER:-https://hastebin.com}"/documents | awk -F '"' '{print $4}');
echo "${HASTE_SERVER:-https://hastebin.com}"/"$b";
}
# Cache $DISPLAY if we're not in tmux, otherwise update from the cache
# Based on: http://alexteichman.com/octo/blog/2014/01/01/x11-forwarding-and-terminal-multiplexers/
update-x11-forwarding() {
if [ ! -f ~/.display ]; then
touch ~/.display
fi
if [ -z "$TMUX" ]; then
echo $DISPLAY > ~/.display
else
export DISPLAY=`cat ~/.display`
fi
}