-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract
executable file
·65 lines (50 loc) · 1.83 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
#!/usr/bin/env bash
# DESTRUCTIVE: Extracts every archive in the current directory and subdirectories.
# Deletes the original archive only when the files have been extracted successfully.
#
# Can be given a path as an argument.
EXTS=(tar gz 7z zip rar) # Archive filetypes to search for
dir="$1"
if [[ ! -d "$dir" ]]; then dir="$PWD"; fi
# Check for missing executables
if ! type 7z > /dev/null 2>&1; then
echo "executable '7z' not found, exiting."
exit 1
fi
if ! type unrar > /dev/null 2>&1; then
echo "executable 'unrar' not found, exiting."
exit 1
fi
find_archives () {
# Searches for archives in the directory
# Create a list of arguments from the 'EXTS' variable to be used with 'find'
argument_list="$(printf " -name \*.%s -or" "${EXTS[@]}" | sed 's, -or$,,g')"
# Find the archives, following symlinks
eval "find -L \"$dir\" -type f $argument_list"
}
extract_archives () {
# Read the archives from stdin
while read file; do
# If the filetype is a rar-archive, uses unrar, otherwise uses 7z
# Removes the archive if the files have been extracted successfully
if [[ "$file" == *.rar ]]; then
unrar x "$file" "${file%.*}/" > /dev/null 2>&1 \
&& rm -v "$file"
else
7z x "$file" -o"${file%.*}/" > /dev/null 2>&1 \
&& rm -v "$file"
fi
done
}
archives="$(find_archives)"
if [[ -z "$archives" ]]; then
echo "No archives in the directory $dir, exiting."
exit
fi
# Count the number of archives found and ask the user whether or not to continue
echo "$archives" | wc -l | tr -d '\n'
read -p " archive files will be extracted and deleted, continue (y/N)? " answer
# Continue only when the user has answered with 'y' or 'Y'
if [[ $answer == y || $answer == Y ]]; then
echo "$archives" | extract_archives
fi