-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract
100 lines (91 loc) · 1.94 KB
/
extract
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
#!/bin/bash
# Description: This script extracts various archive file formats based on their extension.
# Dependency: zip, tar, 7z, xz, gzip
# Print help message
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo "This script extracts various archive file formats based on their extension."
echo "Usage: "
echo " extract [filename] "
echo "Example: "
echo " extract documents.zip"
exit 0
fi
# Check if required commands are installed
check_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Error: '$1' is not installed. Please install it using your package manager."
exit 1
fi
}
check_command "tar"
check_command "7z"
check_command "xz"
# Check if a file is provided
if [ -z "$1" ]; then
echo "Error: Please provide a file to extract."
exit 1
fi
# Check if the file exists
if [ ! -f "$1" ]; then
echo "File does not exist."
exit 1
fi
# Extract the file based on its extension
extract_archive() {
local filename="$1"
case "$filename" in
*.zip)
unzip "$filename"
;;
*.tar)
tar xvf "$filename"
;;
*.tar.gz | *.tgz)
tar xzvf "$filename"
;;
*.tar.bz2)
tar xjvf "$filename"
;;
*.tar.xz | *.txz)
tar Jxvf "$filename"
;;
*.tar.lzma | *.taz)
tar --lzma -xvf "$filename"
;;
*.tar.Z)
tar Zxvf "$filename"
;;
*.tar.lzo)
tar --lzop -xvf "$filename"
;;
*.tar.lz)
tar --lzip -xvf "$filename"
;;
*.tar.lrz)
tar --lrzip -xvf "$filename"
;;
*.7z)
7z x "$filename"
;;
*.gz)
gzip -dv "$filename"
;;
*.xz)
xz -dv "$filename"
;;
*)
echo "Error: Unsupported file extension."
exit 1
;;
esac
}
# Extract the archive
extract_archive "$1"
# Check if the extraction completed without any errors
if [[ $? -eq 0 ]]; then
echo "Extraction completed!"
exit 0
else
echo "Extraction failed!"
exit 1
fi