Subscribe to How-To Geek

Unzip or Unrar Many Files at Once in Linux

If you’ve got a directory with dozens of zipped or rar’d files, you can run a single command to unzip them all in one step, thanks to the power of the bash shell.

For this task, we’ll use bash’s for loop command structure. Replace <var> with a variable name, and <list> with either a command that outputs a list or an explicit list.

for <var> in <list>
do
   command $<var>;
done

You can run it on a single line with this syntax instead:

for <var> in <list>;do command $<var>;done

So if you want to unrar a list of files, you could use this command. You don’t necessarily need the quotes, but it helps when the filenames have spaces or something like that in them.

for f in *.rar;do unrar e “$f”;done

If you wanted to use 7zip to extract a list of files:

for f in *.001;do 7z e “$f”;done

Or if you wanted to unzip a list of files:

for f in *.zip;do unzip “$f”;done

You could even chain commands together if you wanted to. For instance, if all your zip files contained .txt files and you wanted to unzip them and then move the unzipped files to another directory:

for f in *.zip;do unzip “$f”;done; for f in *.txt;do mv “$f” /myfolder/;done

The bash shell is just so incredibly powerful… this doesn’t even tap the power, but it should give you a good idea of what is possible.

The Geek is the founder of How-To Geek and a geek enthusiast. This article was written on 05/28/07 and tagged with: Ubuntu

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:
Similar Articles Featured Wiki Articles
Latest Software Reviews Quick Linux Tips
Geek Arcade Popular Forum Threads

Comments (13)

  1. Mr Linux

    Actually, I’m pretty sure with both these commands you can just do this:

    unzip *.zip
    unzip *.rar

    Seems a bit more straightforward. :-)

  2. The Geek

    That may work sometimes, but didn’t work for me. (just tried it again to make sure)

    The article is mostly illustrating how to use the for / do in bash =)

  3. BookJunkie

    How would I go about ‘rarring’ :) or zipping a bunch of folders?

  4. jose

    Is there a decompressor that extracts almost all file types, if so which?

  5. hoberion

    find -name *.zip -exec unzip {} \;

  6. Ranga

    Try unzip \*.zip – It works in RED HAT flavours, I have not tried it on any other linux.

  7. Alp

    Why didn’t i find this 3 days ago? The only matter is that the files aren’t extracted in subdirectories. I made a similar script myself. Take a look: http://ubuntu.alperortac.de/20.....ubfolders/

  8. Davide Cavestro

    Maybe Alp wants something like this:

    for f in *.zip;do unzip “$f” -d “$f.inflated”;done

    It works on my Debian.

    PS: I’ve not seen Alp link cause it seems broken.

  9. Alp

    Sorry for the broken link, and thanks for pointing that out. It’s fixed now.

  10. Ax3

    Very useful tutorial! Thanks!

    I modified mine a bit to account for archives which ask for confirmation. If you ‘man unzip’ you’ll find a -o switch which: “-o overwrite files WITHOUT prompting”

    So my .sh script amounted to:

    for f in *.zip;
    do unzip -o -d “$f”;
    done

    Hope this helps someone! Regards,
    Ax3

  11. Ax3

    Looks like my previous code post didn’t go thru completely. I meant on the second line of code:

    do unzip -o -d foldername/ “$f”;

    Ax3

  12. 1jackjack

    I know this page is old, but I just found it when looking for a good unrar script, so thought I would post my final solution. The situation: you’re in a directory with a bunch of folders, each one containing a compressed file. I used to use this script, which found and extracted all the *.rar files:

    find -type f -name ‘*.rar’ -exec unrar x {} \;

    But then i noticed some of my compressed files were distributed over many ‘part’ files, and occasionally, all of these would be *.rar files, and so this script would try to do the whole decompression for part file, which was annoying, time consuming and more importantly not the elegant solution! So after a bit of reading, I came up with this:

    for directory in `ls -d */`; do
    rarFile=`ls $directory | grep -i .rar –max-count 1`;
    unrar e $directory$rarFile;
    done

    This iterates through each directory at the current level, and for each one finds just the first *.rar file, and extracts it to the current directory.

  13. Sergio

    thx a lot!!! :)


Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Sponsored Links
Getting Started
About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
Cmd.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.