bash-folders/bash-folders-decompress.sh

123 lines
2.7 KiB
Bash
Raw Normal View History

2023-04-19 19:33:47 +02:00
#!/usr/bin/env bash
2023-03-22 00:33:13 +01:00
2023-04-19 19:33:47 +02:00
# --
2023-04-19 22:38:52 +02:00
# Description: Script that decompresses all the files that are left in the folder.
# --nnnnnnn
# Requirements: Install inotify-tools, gzip bzip2 xz-utils unzip p7zip-full and unrar
# Example Debian: $sudo apt install inotify-tools gzip bzip2 xz-utils unzip p7zip-full unrar
2023-04-19 19:33:47 +02:00
# --
2023-04-19 22:38:52 +02:00
# Cron: @reboot bash-folders-descompress.sh >/dev/null 2>&1 &
2023-04-19 19:33:47 +02:00
# --
# START
set -e
# EXPORTS
# Fix: notify-send command doesn't launch the notification through systemd service
export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/${UID}/bus}"
# VARIABLES
PROGNAME=$(basename "$0")
FOLDER_ORIGIN="$2"
2023-04-19 22:38:52 +02:00
EXTENSIONS_TO_WATCH=("gzip" "bzip2" "xz" "zip" "7z" "rar")
2023-04-19 19:33:47 +02:00
# FUNCTIONS
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
cat << EOF
Usage: $PROGNAME [OPTION]
2023-04-19 22:38:52 +02:00
Watches new compress files and decompresses all the files.
2023-04-19 19:33:47 +02:00
Options:
2023-04-19 22:38:52 +02:00
--folder [path] Folder path where will be monitored.
2023-04-19 19:33:47 +02:00
--help Display this usage message and exit
EOF
exit 1
}
send-notification() {
if command -v notify-send >/dev/null 2>&1; then
# Send a native notification
notify-send "$1"
else
# If the above command is not available, print by console
echo "$1"
fi
}
start() {
2023-04-19 22:44:25 +02:00
# Output
cd "$FOLDER_ORIGIN"
2023-04-19 19:33:47 +02:00
# Monitors the selected folder
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
while read -r filename; do
# Gets the file extension
extension="${filename##*.}"
2023-04-19 22:38:52 +02:00
filepath=$(readlink -f "$FOLDER_ORIGIN/$filename")
2023-04-19 19:33:47 +02:00
# Checks if the extension is in the extension list
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
if [[ "$ext" = "$extension" ]]; then
2023-04-19 22:38:52 +02:00
# Notifies that the conversion is to be started
send-notification "Descompressing '$filename', please wait."
# Decompresses the file
filetype=$(file -b "$filepath" | awk '{print $1}')
case "$filetype" in
"gzip")
gzip -d "$filepath"
;;
"bzip2")
bzip2 -d "$filepath"
;;
"XZ")
xz -d "$filepath"
;;
"Zip")
unzip "$filepath"
;;
"7-zip")
7z x "$filepath"
;;
"RAR")
unrar x "$filepath"
;;
*)
send-notification "Error: Unknown file type $filetype"
;;
esac
# Notifies that it has been terminated
send-notification "Descompressing $filename finished."
2023-04-19 19:33:47 +02:00
fi
done
done
}
# CONTROLE ARGUMENTS
isArg=""
while [ $# -gt 0 ] ; do
case "$1" in
--help)
usage
;;
--folder)
isArg="1"
if [ $# -eq 2 ]; then
start
else
usage "You need to specify the path of the folder to watch."
fi
;;
*)
esac
shift
done
if [ -z $isArg ] ; then
usage "Not enough arguments"
fi