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

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.

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

1 Comment