Decompress all rar files and delete them if ran successfully

By xngo on March 4, 2019

#!/bin/bash
# Description: Decompress all *.rar files and delete them if ran successfully.
#              Errors are logged in err.log.
# Author: Xuan Ngo
# Usage: sh thisScript.sh
# Warning: It doesn't handle multiple part rar files.
 
# Change $IFS so that it handles filename with spaces.
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
 
 
ERROR_LOG=err.log
 
for filename in $(ls *.rar)
do
 
  # Extract and send error text in ${ERROR_LOG}
  rar x -y "${filename}" 2> ${ERROR_LOG}
  ERROR_LOG_SIZE=`ls -sh ${ERROR_LOG} | gawk '{print $1}'`
 
  # If there is no error.
  if [ "${ERROR_LOG_SIZE}" == "0" ]; then
    rm -f "${filename}"
  fi
 
done
 
# Change $IFS back to the original value.
IFS=$SAVEIFS





For unzip, use the following command:

unzip -oq

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.