-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.sh
executable file
·166 lines (146 loc) · 3.2 KB
/
functions.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
# shellcheck shell=bash
# update the dotfiles repo
function dotfiles-update() (
# shellcheck disable=SC2164
cd "${DOTFILES__ROOT}/.dotfiles"
git pull origin master
./bootstrap.sh
)
# Count the number of files in a directory
function cf() {
find "${1-.}" -type f | wc -l
}
# find files with case-insensetive matching in current directory
function findhere() {
find . -iname "*$1*"
}
# do a case-insensetive grep on all the files in a directory
function grip() {
grep -ir "$1" .
}
# xargs wrapper for running PROC_CORES parallel processes
function parallel-xargs() {
local cmd="$*"
if [[ ! "$cmd" =~ "{}" ]]; then
cmd="$cmd {}"
fi
xargs -r -I {} -P "${PROC_CORES:-1}" sh -c "${cmd}"
}
# Extract archives automatically
function extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2)
tar xjf "$@"
;;
*.tar.gz)
tar xzf "$@"
;;
*.bz2)
bunzip2 "$@"
;;
*.rar)
rar x "$@"
;;
*.gz)
gunzip "$@"
;;
*.tar)
tar xf "$@"
;;
*.tbz2)
tar xjf "$@"
;;
*.tgz)
tar xzf "$@"
;;
*.zip)
unzip "$@"
;;
*.Z)
uncompress "$@"
;;
*.7z)
7z x "$@"
;;
*)
echo "'$1' cannot be extracted via extract()"
;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Get gzipped file size
function gz-size() {
echo -n "original (bytes): "
wc -c <"${1}"
echo -n "gzipped (bytes): "
gzip -c "${1}" | wc -c
}
# Create a new directory and enter it
function md() {
mkdir -p "$@" && cd "$@" || return 1
}
# Use Git's colored diff when available
if command -v git &>/dev/null; then
function diff() {
git diff --no-index --color "$@"
}
fi
# Create a data URL from an image (works for other file types too, if you tweak the Content-Type afterwards)
function dataurl() {
echo "data:image/${1##*.};base64,$(openssl base64 -in "$1")" | tr -d '\n'
}
# Gzip-enabled `curl`
function curl-gz() {
curl -sH "Accept-Encoding: gzip" "$@" | gunzip
}
# Escape UTF-8 characters into their 3-byte format
function escape() {
# shellcheck disable=SC2046,SC2059
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
echo # newline
}
# Decode \x{ABCD}-style Unicode escape sequences
function unidecode() {
perl -e "binmode(STDOUT, ':utf8'); print \"$*\""
echo # newline
}
# Get a character's Unicode code point
function codepoint() {
perl -e "use utf8; print sprintf('U+%04X', ord(\"$*\"))"
echo # newline
}
# Convert a unix timestamp to a date string
function unix2date() {
if [[ -n "$1" ]]; then
echo "$1" | awk '{print strftime("%c", $1)}'
return
fi
date
}
# Convert a date string to a unix timestamp
function date2unix() {
if [[ -n "$1" ]]; then
date --date "$*" +%s
return
fi
date +%s
}
# Convert to lowercase.
function lc() {
tr '[:upper:]' '[:lower:]'
}
# Convert to uppercase.
function uc() {
tr '[:lower:]' '[:upper:]'
}
# regex match and replace from: https://gist.github.com/opsb/4409156
function regex() {
gawk "match(\$0, /${1}/, ary) { print ary[${2:-0}] }"
}
# binary diff
function binarydiff() {
vimdiff <(xxd "${1}") <(xxd "${2}")
}