-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.functions
283 lines (246 loc) · 8.4 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
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
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$@"
}
# Determine size of a file or total size of a directory
function 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
}
# Use Git.s colored diff when available
hash git &>/dev/null
if [ $? -eq 0 ]; then
function diff() {
git diff --no-index --color-words "$@"
}
fi
# Create a data URL from a file
function dataurl() {
local mimeType=$(file -b --mime-type "$1")
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8"
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
}
# Start an HTTP server from a directory, optionally specifying the port
function server() {
local port="${1:-8000}"
sleep 1 && open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn.t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
# Start a PHP server from a directory, optionally specifying the port
# (Requires PHP 5.4.0+.)
function phpserver() {
local port="${1:-4000}"
local ip=$(ipconfig getifaddr en1)
sleep 1 && open "http://${ip}:${port}/" &
php -S "${ip}:${port}"
}
# Get gzipped file size
function gz() {
echo "orig size (bytes): "
cat "$1" | wc -c
echo "gzipped size (bytes): "
gzip -c "$1" | wc -c
}
# Test if HTTP compression (RFC 2616 + SDCH) is enabled for a given URL.
# Send a fake UA string for sites that sniff it instead of using the Accept-Encoding header. (Looking at you, ajax.googleapis.com!)
function httpcompression() {
encoding="$(curl -LIs -H 'User-Agent: Mozilla/5 Gecko' -H 'Accept-Encoding: gzip,deflate,compress,sdch' "$1" | grep '^Content-Encoding:')" && echo "$1 is encoded using ${encoding#* }" || echo "$1 is not using any encoding"
}
# Syntax-highlight JSON strings or files
# Usage: `json '{"foo":42}'` or `echo '{"foo":42}' | json`
function json() {
if [ -t 0 ]; then # argument
python -mjson.tool <<< "$*" | pygmentize -l javascript
else # pipe
python -mjson.tool | pygmentize -l javascript
fi
}
# Escape UTF-8 characters into their 3-byte format
function escape() {
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
}
# shows network information for your system
netinfo() {
echo -e "${grey}### ${BLUE}Network Information${grey} ############${reset}"
ifconfig | awk /'inet addr/ {print $2}'
ifconfig | awk /'Bcast/ {print $3}'
#ifconfig | awk /'inet addr/ {print $4}'
ifconfig | awk /'HWaddr/ {print $4,$5}'
lynx -dump -hiddenlinks=ignore -nolist http://checkip.dyndns.org:8245/ | sed '/^$/d; s/^[ ]*//g; s/[ ]*$//g'
echo -e "${grey}####################################${reset}"
}
# Get current host related info.
function whereami() {
echo -e "\n${RED}You are logged onto:${reset} " ; hostname
echo -e "\n${RED}Additionnal information:${reset} " ; uname -a
echo -e "\n${RED}Users logged on:${reset} " ; w -h
echo -e "\n${RED}Current date:${reset} " ; date
echo -e "\n${RED}Machine stat:${reset} " ; uptime
echo -e "\n${RED}Disk space:${reset} " ; df -h
echo -e "\n${RED}Memory stats (in MB):${reset} " ; free -m
echo -e "\n${RED}Network:${reset} " ; ifconfig
}
# Displays Top History Commands
topCommands(){ history|awk '{a[$2]++}END{for(i in a){printf"%5d\t%s\n",a[i],i}}'|sort -nr|head; }
# cd directly to a dir and list contents
cdl() {
if [ "$1" ]
then builtin cd "$1" && ll
else builtin cd && ll
fi
}
# Find a file with a pattern in name
function ff() { find . -type f -iname '*'$*'*' -ls ; }
# Find a file with pattern $1 in name and Execute $2 on it
function fe() { find . -type f -iname '*'${1:-}'*' -exec ${2:-file} {} \; ; }
# Handy Extract Program.
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 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Handy Compress Programm.
function compress_() {
# Credit goes to: Daenyth
FILE=$1
shift
case $FILE in
*.tar.bz2) tar cjf $FILE $* ;;
*.tar.gz) tar czf $FILE $* ;;
*.tgz) tar czf $FILE $* ;;
*.zip) zip $FILE $* ;;
*.rar) rar $FILE $* ;;
*) echo "Filetype not recognized" ;;
esac
}
# record screen
function recordScreen() {
#ffmpeg -f x11grab -s 800x600 -r 24 -i :0.0 -sameq ~/Videos/ScreenCast/$1.mpg
ffmpeg -y -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 'xdpyinfo | grep 'dimensions:'|awk '{print $2}'' -i :0.0 -acodec pcm_s16le ~/Videos/ScreenCast/$1.wav -an -vcodec libx264 -vpre lossless_ultrafast -threads 0 ~/Videos/recorded_$1.mp4
}
# record Webcam
function recordWebcam() {
mplayer -cache 128 -tv driver=v4l2:width=176:height=177 -vo xv tv:// -noborder -geometry "95%:93%" -ontop | ffmpeg -y -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 'xdpyinfo | grep 'dimensions:'|awk '{print $2}'' -i :0.0 -acodec pcm_s16le output.wav -an -vcodec libx264 -vpre lossless_ultrafast -threads 0 webcam_output.mp4
}
# Download a web page and show info on what took time
function debug_http() {
/usr/bin/curl $@ -o /dev/null -w "dns: %{time_namelookup} connect: %{time_connect} pretransfer: %{time_pretransfer} starttransfer: %{time_starttransfer} total: %{time_total}\n";
}
# Check headers of the site
function http_headers() { /usr/bin/curl -I -L $@ ; }
# define cheese
function define() { curl dict://dict.org/d:"$@" ; }
# Size directories
function sizedir() {
echo "size of directories in MB"
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo "directory not specified, using pwd"
DIR=$(pwd)
find $DIR -maxdepth 1 -type d -exec du -sm \{\} \; | sort -nr
else
find $1 -maxdepth 1 -type d -exec du -sm \{\} \; | sort -nr
fi
}
# Show files and sizes in current dir
function sizefiles() {
du -sh * | awk '/[[:space:]]*[[:digit:]]+,*[[:digit:]]*G/' | sort -nr
du -sh * | awk '/[[:space:]]*[[:digit:]]+,*[[:digit:]]*M/' | sort -nr
}
# Stopwatch
function stopwatch() {
BEGIN=$(date +%s)
while true; do
NOW=$(date +%s)
DIFF=$(($NOW - $BEGIN))
MINS=$(($DIFF / 60))
SECS=$(($DIFF % 60))
echo -ne "Time elapsed: $MINS:'printf %02d $SECS'\r"
sleep .1
done
}
# Stream movies with vlc to port 8080. on other computer use vlc http://server:8080
#function stream() { vlc -I dummy $1 udp: --sout '#standard{access=http,mux=ts,dst=:8080}'; }
# METAR ex:metar MRPV
function metar() {
_ICAO=$1
if [ $# = 1 ];then
printf "$_ICAO\n" | egrep '^[a-Z][a-Z][a-Z][a-Z]$' >>/dev/null
if [ $? = 0 ];then
lynx -dump "http://aviationweather.gov/adds/metars/index.php?station_ids=$_ICAO" | sed -n '5,8p';
elif [ $? = 1 ];then
printf "Bad ICAO code!\n"
fi
elif [ $# != 1 ];then
printf "You need to supply a ICAO code!\n"
fi
}
# SVN display the revision number of the current repository
function svn_rev() {
svn info $@ | awk '/^Revision:/ {print $2}'
}
# SVN do a svn update and show the log messages since the last update
function svn_uplog() {
local old_revision='svn_rev $@'
local first_update=$((${old_revision} + 1))
svn up -q $@
if [ $(svn_rev $@) -gt ${old_revision} ]; then
svn log -v -rHEAD:${first_update} $@
else
echo "No Changes."
fi
}
# Get X-Server
function get_xserver()
{
case $TERM in
xterm )
XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )
# Ane-Pieter Wieringa suggests the following alternative:
# I_AM=$(who am i)
# SERVER=${I_AM#*(}
# SERVER=${SERVER%*)}
XSERVER=${XSERVER%%:*}
;;
aterm | rxvt)
# Find some code that works here. ...
;;
esac
}