Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a huge file that contains zips and rars with zip and rar files located within on a linux box.

I basically need a script or one-liner that will recursively hunt through the directories and unzip and unrar any rar or zip it finds.

share|improve this question

migrated from stackoverflow.com Oct 23 '13 at 4:18

This question came from our site for professional and enthusiast programmers.

add comment

4 Answers 4

Here is someones solution to this with a shell script.

http://www.dbforums.com/unix-shell-scripts/1619154-how-unzip-files-recursively.html

It would seem that Linux does not have a recursive unzip option :(

share|improve this answer
1  
It is inappropriate to simply link your source, you should also provide an answer –  xenoterracide Oct 10 '13 at 21:53
add comment

The only way I can think of doing this is to loop multiple times with find and execute a script that removes the files after they've been correctly unpacked. It'll have to go over subdirectories multiple times so it's not exactly efficient. (expected filenames in bold on top)

recursive_unpack

#!/bin/bash
if [ -d "$1" ]; then
   STARTDIR="$1"
else
   echo "starting dir not found: $1"
   exit
fi


COUNTER=1
while [ $COUNTER -gt 0 ]; do
   COUNTER=`find "$STARTDIR" -type f \( -iname '*.zip' -o -iname '*.rar' \) -exec ./unpacker \{\} \; | wc -l`
done

unpacker

#!/bin/bash
BASENAME=`basename "$1"`
BASEDIR=`dirname "$1"`

cd "$BASEDIR"
EXT=`echo "$BASENAME" | awk -F . '{print $NF}'`

if [ "$EXT" = "zip" ]; then
  unzip -qq "$BASENAME"
  if [ $? -eq 0 ]; then
     echo "zip unpacked: $1"
     rm "$BASENAME"
  fi
fi

if [ "$EXT" = "rar" ]; then
  unrar e -y -c- -inul "$BASENAME"
  if [ $? -eq 0 ]; then
     echo "rar unpacked: $1"
     rm "$BASENAME"
  fi
fi

do

chmod u+x recursive_unpack
chmod u+x unpacker

call it with ./recursive_unpack "/my/directory/containing/my/files"

  • recursive_unpack expects unpacker in the same directory
  • unrar and unzip need to be installed
  • if it can't find the directory it'll exit with an error
  • do not remove the echo messages in unpacker, they're used to check for the presence of files to unpack to terminate the loop
share|improve this answer
add comment

A quick one-liner, doesn't check if an archive has already been extracted:

$ find . -name '*.rar' -execdir unrar e '{}' + && find . -name '*.zip' -execdir unzip -tq '{}' +
share|improve this answer
    
I wonder why unrar requires execdir, after reading the man page I think I'll stop using exec. –  xenoterracide Oct 10 '13 at 21:52
    
note, edited to add unzip, that's untested –  xenoterracide Oct 10 '13 at 21:56
add comment

Maybe dtrx (http://brettcsmith.org/2007/dtrx/) will do, though I'm not sure whether it handles rar type.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.