-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions
More file actions
260 lines (237 loc) · 6.03 KB
/
Copy pathfunctions
File metadata and controls
260 lines (237 loc) · 6.03 KB
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
# FUNCTIONS {{{
# top des commandes de l'historique {{{
top10() { history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}' | sort -rn | head; }
#}}}
# UP {{{
# on revient en arrière
up() {
local d=""
limit=$1
for ((i=1 ; i <= limit ; i++)); do
d=$d/..
done
d=$(echo $d | sed 's/^\///')
if [[ -z "$d" ]]; then
d=..
fi
cd $d
}
#}}}
# on extract n'importe quel type d'archive {{{
extract() {
clrstart="\033[1;34m" #color codes
clrend="\033[0m"
if [[ "$#" -lt 1 ]]; then
echo -e "${clrstart}Pass a filename. Optionally a destination folder. You can also append a v for verbose output.${clrend}"
exit 1 #not enough args
fi
if [[ ! -e "$1" ]]; then
echo -e "${clrstart}File does not exist!${clrend}"
exit 2 #file not found
fi
if [[ -z "$2" ]]; then
DESTDIR="." #set destdir to current dir
elif [[ ! -d "$2" ]]; then
echo -e -n "${clrstart}Destination folder doesn't exist or isnt a directory. Create? (y/n): ${clrend}"
read response
#echo -e "\n"
if [[ $response == y || $response == Y ]]; then
mkdir -p "$2"
if [ $? -eq 0 ]; then
DESTDIR="$2"
else
exit 6 #Write perms error
fi
else
echo -e "${clrstart}Closing.${clrend}"; exit 3 # n/wrong response
fi
else
DESTDIR="$2"
fi
if [[ ! -z "$3" ]]; then
if [[ "$3" != "v" ]]; then
echo -e "${clrstart}Wrong argument $3 !${clrend}"
exit 4 #wrong arg 3
fi
fi
filename=`basename "$1"`
#echo "${filename##*.}" debug
case "${filename##*.}" in
tar)
echo -e "${clrstart}Extracting $1 to $DESTDIR: (uncompressed tar)${clrend}"
tar x${3}f "$1" -C "$DESTDIR"
;;
gz)
echo -e "${clrstart}Extracting $1 to $DESTDIR: (gip compressed tar)${clrend}"
tar x${3}fz "$1" -C "$DESTDIR"
;;
tgz)
echo -e "${clrstart}Extracting $1 to $DESTDIR: (gip compressed tar)${clrend}"
tar x${3}fz "$1" -C "$DESTDIR"
;;
xz)
echo -e "${clrstart}Extracting $1 to $DESTDIR: (gip compressed tar)${clrend}"
tar x${3}f -J "$1" -C "$DESTDIR"
;;
bz2)
echo -e "${clrstart}Extracting $1 to $DESTDIR: (bzip compressed tar)${clrend}"
tar x${3}fj "$1" -C "$DESTDIR"
;;
zip)
echo -e "${clrstart}Extracting $1 to $DESTDIR: (zipp compressed file)${clrend}"
unzip "$1" -d "$DESTDIR"
;;
rar)
echo -e "${clrstart}Extracting $1 to $DESTDIR: (rar compressed file)${clrend}"
unrar x "$1" "$DESTDIR"
;;
7z)
echo -e "${clrstart}Extracting $1 to $DESTDIR: (7zip compressed file)${clrend}"
7za e "$1" -o"$DESTDIR"
;;
*)
echo -e "${clrstart}Unknown archieve format!"
exit 5
;;
esac
}
#}}}
# on compress n'importe quelle archive {{{
compress() {
if [[ -n "$1" ]]; then
FILE=$1
case $FILE in
*.tar ) shift && tar cf $FILE $* ;;
*.tar.bz2 ) shift && tar cjf $FILE $* ;;
*.tar.gz ) shift && tar czf $FILE $* ;;
*.tgz ) shift && tar czf $FILE $* ;;
*.zip ) shift && zip $FILE $* ;;
*.rar ) shift && rar $FILE $* ;;
esac
else
echo "usage: compress <foo.tar.gz> ./foo ./bar"
fi
}
#}}}
# on convertit en iso {{{
to_iso () {
if [[ $# == 0 || $1 == "--help" || $1 == "-h" ]]; then
echo -e "Converts raw, bin, cue, ccd, img, mdf, nrg cd/dvd image files to ISO image file.\nUsage: to_iso file1 file2..."
fi
for i in $*; do
if [[ ! -f "$i" ]]; then
echo "'$i' is not a valid file; jumping it"
else
echo -n "converting $i..."
OUT=`echo $i | cut -d '.' -f 1`
case $i in
*.raw ) bchunk -v $i $OUT.iso;; #raw=bin #*.cue #*.bin
*.bin|*.cue ) bin2iso $i $OUT.iso;;
*.ccd|*.img ) ccd2iso $i $OUT.iso;; #Clone CD images
*.mdf ) mdf2iso $i $OUT.iso;; #Alcohol images
*.nrg ) nrg2iso $i $OUT.iso;; #nero images
* ) echo "to_iso don't know de extension of '$i'";;
esac
if [[ $? != 0 ]]; then
echo -e "${R}ERROR!${W}"
else
echo -e "${G}done!${W}"
fi
fi
done
}
#}}}
# petite calclulette #{{{
# usage: calc <equation>
calc() {
if which bc &>/dev/null; then
echo "scale=3; $*" | bc -l
else
awk "BEGIN { print $* }"
fi
}
#}}}
## cherche fichier avec pattern dans le nom {{{
ff() { find . -type f -iname '*'$*'*' -ls ; }
#}}}
## cherche fichier avec pattern dans le nom $1 et on execute $2 dessus {{{
fe() { find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \; ; }
#}}}
## on passe en minuscule {{{
lowercase() {
for file ; do
filename=${file##*/}
case "$filename" in
*/* ) dirname==${file%/*} ;;
* ) dirname=.;;
esac
nf=$(echo $filename | tr A-Z a-z)
newname="${dirname}/${nf}"
if [[ "$nf" != "$filename" ]]; then
mv "$file" "$newname"
echo "lowercase: $file --> $newname"
else
echo "lowercase: $file not changed."
fi
done
}
#}}}
## on change 2 nom de fichiers {{{
swap() {
local TMPFILE=tmp.$$
[[ $# -ne 2 ]] && echo "swap: 2 arguments needed" && return 1
[[ ! -e $1 ]] && echo "swap: $1 does not exist" && return 1
[[ ! -e $2 ]] && echo "swap: $2 does not exist" && return 1
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
}
#}}}
## on cherche les tailles des repos et on les liste{{{
dirsize () {
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
egrep '^ *[0-9.]*M' /tmp/list
egrep '^ *[0-9.]*G' /tmp/list
rm -rf /tmp/list
}
#}}}
# SYSTEMD SUPPORT {{{
if which systemctl &>/dev/null; then
start() {
sudo systemctl start $1.service
}
restart() {
sudo systemctl restart $1.service
}
stop() {
sudo systemctl stop $1.service
}
enable() {
sudo systemctl enable $1.service
}
status() {
sudo systemctl status $1.service
}
disable() {
sudo systemctl disable $1.service
}
fi
#}}}
# on créé un rep et on y va{{{
mkcd() {
if [ $# != 1 ]; then
echo "Usage: mkcd <dir>"
else
mkdir -p $1 && cd $1
fi
}
#}}}
#}}}
vim()
{
# osx users, use stty -g
local STTYOPTS="$(stty --save)"
stty stop '' -ixoff
command vim "$@"
stty "$STTYOPTS"
}