Unzipping all files in a directory using Bash

. 1 min read

I have a ton of audiobook downloads in a folder, that I wanted to unzip and delete the old files. Doing this manually is boring and repetitive.

To solve this, the following script worked pretty well! I run this manually in the bash terminal.

#!/bin/bash

# Modifying previous script at: https://www.santoshsrinivas.com/text-manipulation-on-the-command-line/

# Unzip all *.rar files in the temporary downloads directory

cd /Volumes/Seagate\ Slim/Books-Downloads

# Get only the filenames: https://unix.stackexchange.com/questions/136884/how-to-use-a-shell-command-to-only-show-the-first-column-and-last-column-in-a-te
# files=$(ls -l *.rar | awk '{print $9}')

# Moving to find. https://askubuntu.com/questions/217742/list-files-of-particular-extension
files=$(find . -name "*.rar")



# Credit: [shell - How to loop over the lines of a file? - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/7011/how-to-loop-over-the-lines-of-a-file)
IFS=$'\n'       # make newlines the only separator
set -f          # disable globbing

for f in $files; do
  echo "File is: $f"
done


# Installations required:
# brew install unrar
# sudo apt-get install unrar

# Unzip files
for f in $files; do
  echo "Unzipping file: $f"
  # Create directory name: https://askubuntu.com/a/879323
  dir=$(echo $f | awk '{gsub(/.rar/,"")}1') #remove .rar from from the filename for directory
  mkdir -p "$dir"
  unrar e -r $f "$dir/"
done


# Delete old files after unzipping all. Out of caution
# UNCOMMENT and run explicitly after everything works!
# for f in $files; do
#   echo "Deleting file: $f"
#   rm $f
# done


Tags

AWS Analytics Antifragile Apache Spark Apple Apps Arbitrage Automation Awesome BBMP Bangalore Black Swan Bonds Boonex Bootstrap Brokerage Charges CFPB CMS CSS Calypso Camelot Capital Markets Charts Chirp Choropleth Chrome Code Igniter Collections Containerization Converters Cycling DalalBeat Data Analytics Data Journalism Data Mining Data Munging Data Privacy Data Science Databases Debt Design Design Stock DesignStock Diet Django Docker Document Processing Dropbox Electron Electronics Entertainment Entrepreneurship Exercise FFMPEG Facebook Finance Fire TV Stick Fitness Forerunner From-The-News Fun Futures Gaana Garmin General Geocoding Getting Started Ghost Google APIs GraphicsMagick HHVM HTTPS Hackathon Hacks Hardware Health House Keeping Housing IBM BlueMix IBrokers IPinfo Ideas Image Conversion Image Processing Infographics Interactive Brokers Internet Investing IoT Legal Lets Encrypt Mac Machine Learning Mailbox app Mapbox Markdown Marketing Meteor Mockups MongoDB Mortgages Movies Music MyPad MySQL Nativefier Nature Netflix Newsletters Nginx Node.js Notes OCR OSX Open Source OpenCPU Options PDF PHP Pebble Peloton Personal Photography Picture Platforms Plex Portable Apps Productivity Property Tax PyroCMS Quantitative Finance R-Project Random Raspberry Pi Research Resources Reticulate Robot S3 SQLite STT Scraping Security Server Shell Shiny Side Project Sikuli Speech Recognition Spider Pro Spinning Spotify Startup Resources Startups Strategy Sublime Text System Monitoring Tax Terminal Tips To Do Tools Torrent Trading Trends Troubleshooting USElection2016 Ubuntu Unix Valuations Video Conversion Visualization Wagtail Wearables Weekend Fun WhatsApp Wordpress anaconda annie bash catalina cv2 daily coding dailycoding data.table debugging devtools downloader e-Signatures ggplot git go google maps hat-tip iOS iPad iPhone iTunes javascript jupyter logging macos numpy opencv optionality plotly python rstats videos youtube