-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
132 lines (122 loc) · 3.51 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
#
# Extract an archive of any type
#
extract(){
if [ $# -lt 1 ]
then
echo Usage: extract file
return 1
fi
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 ;;
*.war|*.jar) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
#
# Swap two files
#
swap(){
if [ $# -ne 2 ]
then
echo Usage: swap file1 file2
return 1
fi
local TMPFILE=tmp.$$
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
}
#
# fast find, using globstar
#
ff(){
ls -ltr **/$@
}
# Trivial command line calculator using awk's bult-in floating-point arithmetic expressions:
function calc
{
awk "BEGIN {print \"The answer is: \" $* }";
}
# Make directory and change to it at the same time
mdir () { mkdir -p "$@" && cd "$@"; }
# wgetall
function wgetall () {
wget -r -l3 -nd -Nc -A.$@ $@
}
# cs to cd and then ls
function cs () {
clear
# only change directory if a directory is specified
[ -n "${1}" ] && cd $1
# filesystem stats
echo "`df -hT .`"
echo ""
echo -n "[`pwd`:"
# count files
echo -n " <`find . -maxdepth 1 -mindepth 1 -type f | wc -l | tr -d '[:space:]'` files>"
# count sub-directories
echo -n " <`find . -maxdepth 1 -mindepth 1 -type d | wc -l | tr -d '[:space:]'` dirs/>"
# count links
echo -n " <`find . -maxdepth 1 -mindepth 1 -type l | wc -l | tr -d '[:space:]'` links@>"
# total disk space used by this directory and all subdirectories
echo " <~`du -sh . 2> /dev/null | cut -f1`>]"
ROWS=`stty size | cut -d' ' -f1`
FILES=`find . -maxdepth 1 -mindepth 1 |
wc -l | tr -d '[:space:]'`
# if the terminal has enough lines, do a long listing
if [ `expr "${ROWS}" - 6` -lt "${FILES}" ]; then
ls -ACF
else
ls -hlAF
fi
}
# do an ls after every successful cd
function cd {
builtin cd "$@" && ls
}
# recursive mkdir and cd if successful
function mkcd {
mkdir -p "$@" && builtin cd "$@"
}
# Start an HTTP server from a directory, optionally specifying the port
function server() {
local port="${1:-8000}"
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"
}
# cp progress bar
cp_p()
{
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
}