I have a folder all_folders which contains 5 folders:


Can I compress all these folders using a single command at once? I want to be in all_folders directory and then execute command so that individual folders in this directory get compressed.

The expected output is

when I do


in all_folders, then I should be having 10 directories. 5 uncompressed ones and 5 compressed ones.


cd into all_folders, then run

for i in folder1 folder2 folder3 folder4 folder5; do tar czf $i.tar.gz $i; done

It's somewhat unclear what you're trying to accomplish. I'm running off the use-case where only sub directories shall be compressed here.

First, go to the directory that contains the collection of directories, and then:

find -maxdepth 1 -type d | {
    while read dir; do 
        if [ ! "x${dir#*/}" = "x." ]; then
            tar cJf ${dir#*/}.tar.xz ${dir#*/};

This works well with zsh. To test things, put a echo infront of tar, to see if the command checks out, etc.

However making a collection of archives like this, is usually not desirable. Usually you want either compress the whole collection, use transparent compression of the drive these files reside on, or use transport compression (X-DEFLATE with HTTP, for instance).

Please be aware you will not end up with a "compressed directory" but with a "compressed archive", these things are not interchangeable, as archives are files themselves, while directories are mere inode datagrams.

tar -zcvf archive.tar.gz directory/ 

This will tell tar to c (create) an archive from the files in directory (tar is recursive by default), compress it using the z (gzip) algorithm, store the output as a f (file) named archive.tar.gz, and v (verbosely) list all the files it adds to the archive.

To decompress and unpack the archive into the current directory you would use

tar -zxvf archive.tar.gz 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.