Shell Script : Find All Zip / Rar Files Then Unzip / Unrar

March 20, 2013 | By
| Reply More

Script that find all rar files in all sub-directories and unrar them to same destination. Shell script will shows how to find all rar files and assign to a variable. Commands are run on each file on the list.

more shell scripts

Shell Script

#!/bin/bash

list=`find /home/linoxide/ -type f -name "*.rar"`

for line in $list; do

DEST=${line%/*}

unrar x $line $DEST

done

Shell Script Output

Server1:~$ ./rar.sh

UNRAR 3.93 freeware Copyright (c) 1993-2010 Alexander Roshal

Extracting from /home/linoxide/Dropbox/Yevhen/test.rar

Extracting /home/linoxide/Dropbox/Yevhen/wget.sh OK
All OK

UNRAR 3.93 freeware Copyright (c) 1993-2010 Alexander Roshal

Extracting from /home/linoxide/Pictures/test.rar

Extracting /home/linoxide/Pictures/wget.sh OK
All OK

Learning above shell script

#this line must be in every bash script, just ensure that you use correct path

list=`find /home/linoxide/ -type f -name "*.rar"` # get list of file and write this list to variable with name list, find command used to find all files (-type f) where name match *.rar (-name key)

for line in $list; do #this line take every line from list to line variable

DEST=${line%/*} # remove from line filename, so just destination will be in DEST variable.

unrar x $line $DEST # unrar file from line variable to DEST dir

done # finish of for loop.

Filed Under : SHELL SCRIPTS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.