"tar -xzf" for many "tar.gz" files in a directory

11,755

Solution 1

Using find mojo:

find . -maxdepth 1 -type f -iname '*.tar.gz' -exec tar xzf '{}' \;

Solution 2

It's expected not to work as tar -xzf *.tar.gz will expand in your case to tar -xzf one.tar.gz juk.tar.gz cv.tar.gz ... Which means gunzip one.tar.gz and extract from it the files named juk.tar.gz, cv.tar.gz, etc. This is clearly not what you want.

You have to do a loop on all tar.gz files to extract them.

For instance:

for f in *.tar.gz ; do tar -xzf $f ; done

Solution 3

ls *.tar.gz | xargs -n 1 tar -xzf

Solution 4

for i in *.tar.gz
do tar -xzf $i
done

Will work fine

Solution 5

One way's to use a for loop.

for i *.tar.gz; do tar -xvz $i; done
Share:
11,755
Admin
Author by

Admin

Updated on September 17, 2022

Comments

  • Admin
    Admin over 1 year

    I have many files in a directory. every one of them have very different name:

    one.tar.gz
    juk.tar.gz
    cv.tar.gz
    erf.tar.gz
    vcx.tar.gz
    cxcd.tar.gz
    vc.tar.gz
    jkjk.tar.gz
    zxc.tar.gz
    asd.tar.gz
    ghj.tar.gz
    kll.tar.gz
    qwe.tar.gz
    

    Extracting one by one is very time consuming. So I want to extract all of that files using one command. I've tried tar -xzf * but not works

  • skazhy
    skazhy over 13 years
    very handy one-liner! :)
  • phogg
    phogg over 13 years
    -1 for piping the output of ls
  • phogg
    phogg over 13 years
    I would want to include -maxdepth 1 to avoid accidental expansion of tarballs in subdirectories.
  • Rich Homolka
    Rich Homolka over 13 years
    I think this was downmodded because if a filename has a space in it, xargs will pass it as two (or more) chunks to tar. But most of the for loops so far on this page don't deal with this problem either. Meh.
  • phogg
    phogg over 13 years
    Other for loops on this page break on spaces and special characters, so I did not vote them up. Their form was correct with simply a small omission. This one breaks that way, too, but does it in a more egregious manner. The output of ls is for human consumption only unless you use -1 or similar, but even then I would avoid it. See ParsingLS.
  • user1686
    user1686 over 13 years
    Don't #2 "do not use leading dash for portability" and #3 "compression auto-detected by modern tar versions?" sort of contradict each other?
  • phogg
    phogg over 13 years
    I did not omit the z in my example for this reason. There's another reason not to use a leading switch which feeds in to the reason you might omit the z: One less character to type.