Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

Unzip or Unrar Many Files at Once in Linux Unzip eller Unrar många filer på en gång i 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. Om du har en katalog med dussintals zippade eller rar'd filer kan du köra ett enda kommando för att packa upp dem alla i ett steg, tack vare kraften i 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. För denna uppgift, kommer vi att använda bash's for-loop ledningsstruktur. Ersätt <var> med varierande namn och <list> med antingen ett kommando som matar ut en lista eller en uttrycklig uppräkning.

for <var> in <list> för <var> i <list>
do göra
command $<var>; kommando $ <var>;
done gjord

You can run it on a single line with this syntax instead: Du kan köra det på en enda rad med denna syntax istället:

for <var> in <list>;do command $<var>;done för <var> i <list>, do kommando $ <var>; gjort

So if you want to unrar a list of files, you could use this command. Så om du vill riva en lista över filer, kan du använda detta kommando. You don't necessarily need the quotes, but it helps when the filenames have spaces or something like that in them. Du behöver inte nödvändigtvis citattecken, men det hjälper när filnamnen har mellanslag eller något liknande i dem.

for f in *.rar;do unrar e “$f”;done för f i *. rar, do unrar e "$ f"; gjort

If you wanted to use 7zip to extract a list of files: Om du vill använda 7zip att extrahera en lista över filer:

for f in *.001;do 7z e “$f”;done för f i *. 001; göra 7z e "$ f"; gjort

Or if you wanted to unzip a list of files: Eller om du vill packa upp en lista med filer:

for f in *.zip;do unzip “$f”;done för f i *. zip; göra unzip "$ f"; gjort

You could even chain commands together if you wanted to. Du kan även kedjan kommandon tillsammans om du ville. 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: Om exempelvis alla zip-filer som finns. Txt-filer och du ville packa upp dem och sedan flytta den uppackade filerna till en annan katalog:

for f in *.zip;do unzip “$f”;done; for f in *.txt;do mv “$f” /myfolder/;done för f i *. zip; göra unzip "$ f"; gjort, för f i *. txt; do mv "$ f" / myfolder /; gjort

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. Den bash shell är bara så otroligt mäktig ... detta inte ens peka på makt, men det bör ge dig en god uppfattning om vad som är möjligt.

This article was originally written on 05/28/07 Tagged with: Denna artikel skrevs ursprungligen på 05/28/07 Märkta med: Ubuntu Ubuntu

Daily Email Updates Dagligt Updates

You can get our how-to articles in your inbox each day for free. Du kan få våra instruktioner artiklar i din inkorg varje dag gratis. Just enter your name and email below: Skriv bara in ditt namn och e-postadress nedan:


Name: Namn:
Email: Email:

Comments (13) Kommentarer (13)

  1. Mr Linux Mr Linux

    Actually, I'm pretty sure with both these commands you can just do this: Faktiskt, jag är ganska säker med båda dessa kommandon kan du bara göra detta:

    unzip *.zip unzip *. zip
    unzip *.rar unzip *. rar

    Seems a bit more straightforward. Verkar lite enklare. :-)

  2. The Geek The Geek

    That may work sometimes, but didn't work for me. Det kan fungera ibland, men fungerade inte för mig. (just tried it again to make sure) (bara försökt en gång till för att försäkra)

    The article is mostly illustrating how to use the for / do in bash =) Artikeln är främst visar hur man använder för / gör i bash =)

  3. BookJunkie BookJunkie

    How would I go about 'rarring' Hur skulle jag gå om "rarring" :) or zipping a bunch of folders? eller zippa en massa mappar?

  4. jose jose

    Is there a decompressor that extracts almost all file types, if so which? Finns det en expanderare som utvinner nästan alla filtyper, så fall vilka?

  5. hoberion hoberion

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

  6. Ranga Ranga

    Try unzip \*.zip – It works in RED HAT flavours, I have not tried it on any other linux. Prova packa \ *. zip - Det fungerar i RED HAT smaker, har jag inte provat det på någon annan linux.

  7. Alp Alp

    Why didn'ti find this 3 days ago? Varför didn'ti hitta detta 3 dagar sedan? The only matter is that the files aren't extracted in subdirectories. Den enda frågan är att filerna inte utvinns i underkataloger. I made a similar script myself. Jag gjorde ett liknande skript själv. Take a look: Ta en titt: http://ubuntu.alperortac.de/20.....ubfolders/ http://ubuntu.alperortac.de/20.....ubfolders/

  8. Davide Cavestro Davide Cavestro

    Maybe Alp wants something like this: Kanske Alp vill ha ungefär så här:

    for f in *.zip;do unzip “$f” -d “$f.inflated”;done för f i *. zip; göra unzip "$ f"-d "$ f.inflated"; gjort

    It works on my Debian. Det fungerar på min Debian.

    PS: I've not seen Alp link cause it seems broken. PS: Jag har inte sett Alp länk orsaka det verkar bruten.

  9. Alp Alp

    Sorry for the broken link, and thanks for pointing that out. Ledsen för den brutna länken, och tack för upplysningen. It's fixed now. Det är åtgärdat nu.

  10. Ax3 Ax3

    Very useful tutorial! Mycket användbar tutorial! Thanks! Tack!

    I modified mine a bit to account for archives which ask for confirmation. Jag ändrade mig lite att redovisa arkiv som be om bekräftelse. If you 'man unzip' you'll find a -o switch which: “-o overwrite files WITHOUT prompting” Om du är man packa "hittar du en-o switch som:"-o skriva över filer utan att fråga "

    So my .sh script amounted to: Så min. Sh script uppgick till:

    for f in *.zip; för f i *. zip;
    do unzip -o -d “$f”; do unzip-o-d "$ f";
    done gjord

    Hope this helps someone! Hoppas att detta hjälper någon! Regards, Hälsningar,
    Ax3 Ax3

  11. Ax3 Ax3

    Looks like my previous code post didn't go thru completely. Ser ut som min tidigare koden post gick inte igenom helt. I meant on the second line of code: Jag menade på andra raden i registret:

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

    Ax3 Ax3

  12. 1jackjack 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. Jag vet att denna sida är gammal, men jag fann det när vi letar efter ett bra unrar manus, så tänkte jag skulle skicka min slutliga lösning. The situation: you're in a directory with a bunch of folders, each one containing a compressed file. Situationen: du är i en katalog med en massa mappar, var och en innehåller en komprimerad fil. I used to use this script, which found and extracted all the *.rar files: Jag brukade använda detta skript, som hittats och extraherat alla *. rar filer:

    find -type f -name '*.rar' -exec unrar x {} \; 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! Men då jag upptäckte några av mina komprimerade filer spreds över många "del" filer, och ibland, alla dessa skulle vara *. rar-filer, och så skriptet skulle försöka göra det hela tryckfall för en del filer, vilket var irriterande, tidskrävande och framför allt inte den eleganta lösningen! So after a bit of reading, I came up with this: Så efter lite läsning, kom jag fram till detta:

    for directory in `ls -d */`; do för katalog i `ls-d * /`; do
    rarFile=`ls $directory | grep -i .rar –max-count 1`; rarFile = `ls $ katalog | grep-i. rar-max-count 1";
    unrar e $directory$rarFile; unrar e $ katalogen $ rarFile;
    done gjord

    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. Detta itererar igenom varje katalog på nuvarande nivå, och för var och en finner bara den första *. rar filen och extraherar den till den aktuella katalogen.

  13. Sergio Sergio

    thx a lot!!! thx a lot! :)


Our Friends Our Friends
Getting Started Komma igång


About How-To Geek Om How-To Geek
What Is That Process? Hur skall den processen?
svchost.exe svchost.exe
jusched.exe SynTPEnh.exe
dwm.exe dwm.exe
ctfmon.exe CTFMON.EXE
wmpnetwk.exe wmpnetwk.exe
wmpnscfg.exe wmpnscfg.exe
rundll32.exe rundll32.exe
wfcrun32.exe wfcrun32.exe
Ipoint.exe Ipoint.exe
Itype.exe Itype.exe
Wfica32.exe Wfica32.exe
Mobsync.exe Mobsync.exe
conhost.exe conhost.exe
Dpupdchk.exe Dpupdchk.exe Adobe_Updater.exe Adobe_Updater.exe

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