-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract
More file actions
executable file
·78 lines (77 loc) · 1.74 KB
/
extract
File metadata and controls
executable file
·78 lines (77 loc) · 1.74 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
#!/bin/bash
# extract FILE
#
while getopts "h" opt; do
case $opt in
h)
echo "Supported extensions:"
echo "* xz"
echo "* tgz"
echo "* tar"
echo "* gz"
echo "* tar.gz"
echo "* zip"
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if [[ "$#" -ne 1 ]]; then
echo "Illegal number of parameters" >&2
exit 1
fi
# Process file path
file=$1
if [[ "$file" =~ ^(.+)\.([^.]+)$ ]]; then
filename="${BASH_REMATCH[1]}"
file_ext="${BASH_REMATCH[2]}"
else
filename="$file"
fi
case "$file_ext" in
"xz" )
if [[ "$filename" =~ ^(.+)\.([^.]+)$ ]]; then
filename="${BASH_REMATCH[1]}"
file_ext2="${BASH_REMATCH[2]}"
if [[ "$file_ext2" = 'tar' ]]; then
echo "Extracting TAR XZ file..."
tar -xJf "$file"
exit 0
fi
fi
echo "Extracting XZ file..."
xz -d "$file"
;;
"tgz" )
echo "Extracting TGZ file..."
tar -xzvf "$file"
exit 0
;;
"gz" )
if [[ "$filename" =~ ^(.+)\.([^.]+)$ ]]; then
filename="${BASH_REMATCH[1]}"
file_ext2="${BASH_REMATCH[2]}"
if [[ "$file_ext2" = 'tar' ]]; then
echo "Extracting TAR GZ file..."
tar -xzvf "$file"
exit 0
fi
fi
echo "Extracting GZ file..."
gunzip "$file"
;;
"tar" )
echo "Extracting TAR file..."
tar -xf "$file"
;;
"zip" )
echo "Extracting ZIP file..."
unzip "$file"
;;
* )
echo "No valid format specified by file extension or flag"
exit 1
;;
esac