Skip to content

Instantly share code, notes, and snippets.

@coleturner
Last active July 8, 2016 04:39
Show Gist options
  • Save coleturner/3139934d7ee40209e0494081365ca28b to your computer and use it in GitHub Desktop.
Save coleturner/3139934d7ee40209e0494081365ca28b to your computer and use it in GitHub Desktop.
Extract all *.rar files in a directory
#!/bin/bash
# @author Cole Turner
# @website http://coleturner.me/
# @readme This script will extract all .rar files in a directory
# while preserving the original files. If "unrar.lock" is
# present in the directory, the archive will be skipped.
# If the rar was previously extracted and "unrar.lock" is
# not present, it will be extracted again.
#
# @license ¯\_(ツ)_/¯
#### PARAMETERS ####
WORKING_DIR=/tmp
NOW=$(date +%d.%m.%Y\ %H:%M:%S)
LOGFILE=/var/log/unrar.log
UNRAR=$(which unrar)
find "${WORKING_DIR}" -iname "*.rar" | while read archive;
do
BASENAME=$(dirname "${archive}")
if [ -d "${BASENAME}" ]
then
if [ ! -f "${BASENAME}/unrar.lock" ]
then
tee >(logger) <<< "Found archive to extract ${archive}"
${UNRAR} x -y "${archive}" ${BASENAME}
touch ${BASENAME}/unrar.lock
tee >(logger) <<< "${NOW}: Extracted {$archive}" >> $LOGFILE
fi
fi
done
@coleturner
Copy link
Author

I have a raspberry pi running with a samba server. Extracting RAR files over SMB is painfully slow and I found myself needing to frequently ssh into do extraction. This script is useful on an hourly CRON to auto-extract the archives from the working directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment