Linux menu

Wednesday, September 17, 2014

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

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.

No comments: