Skip to content

Compress and Un-compress

Zip

The -r option is used to recursively zip files. This option will compress all the files present within a folder. An example of such command is as shown below:

Bash
zip –r sampleZipFile.zip MyDirectory

Exclude Files in a Zip (Here ExampleFile.txt will not be added to the sampleZipFile.zip.)

Bash
zip -x sampleZipFile.zip ExampleFile.txt

Zip with Password

Bash
zip -re OUTPUT_FILE.zip FOLDER

Unzip to a Different Directory

Bash
unzip sampleZipFile.zip -d /usr/sampleZip/ExampleDir

Use Linux Unzip with Multiple Zip Files

Bash
unzip ‘*.zip’

Suppress Output When Using Unzip in Linux (In case you want to suppress these messages, then you can use the -q option. The command would be as shown below:)

Bash
unzip -q sampleZipFile.zip

Exclude Files Using Unzip in Linux

Bash
unzip sampleZipFile.zip -x excludedFile.txt
Bash
unzip sampleZipFile.zip -x "*.png/*"

Using Unzip in Linux with Password Protected Files

Bash
unzip -P Password sampleZipFile.zip

Overriding Zip Files

[y]es, [n]o, [A]ll, [N]one, [r]ename

You can override these files by using the -o options

Bash
unzip -o sampleZipFile.zip

Using Linux Unzip Without Overwriting Files

Bash
unzip -n sampleZipFile.zip

How to List the Content of a Zip in Linux

Bash
unzip -l sampleZipFile.zip

Tar

Options

Options:

-c : Creates Archive

-x : Extract the archive

-f : creates archive with given filename

-t : displays or lists files in archived file

-u : archives and adds to an existing archive file

-v : Displays Verbose Information

-A : Concatenates the archive files

-z : zip, tells tar command that creates tar file using gzip

-j : filter archive tar file using tbzip

-W : Verify a archive file

-r : update or add file or directory in already existed .tar file

This command creates a tar file called file.tar which is the Archive of all .c files in current directory.

Bash
tar cvf file.tar *.c

gzip compression on the tar Archive, using option -z

Bash
tar cvzf file.tar.gz *.c

Extracting files from Archive using option -xvf : This command extracts files from Archives.

Bash
tar xvf file.tar

Extracting a gzip tar Archive *.tar.gz using option -xvzf

Bash
tar xvzf file.tar.gz

Creating compressed tar archive file in Linux using option -j : This command compresses and creates archive file less than the size of the gzip. Both compress and decompress takes more time then gzip.

Bash
tar cvfj file.tar.tbz example.cpp

Untar single tar file or specified directory in Linux

Bash
tar xvfj file.tar -C path of file in directory 

Untar multiple .tar, .tar.gz, .tar.tbz file in Linux : This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the above command will extract “fileA” “fileB” from the archive files.

Bash
tar xvf file.tar "fileA" "fileB" 

or

Bash
tar zxvf file1.tar.gz "fileA" "fileB"

or

Bash
tar jxvf file2.tar.tbz "fileA" "fileB"

Check size of existing tar, tar.gz, tar.tbz file in Linux : The above command will display the size of archive file in Kilobytes(KB).

Bash
tar czf file.tar | wc -c

Update existing tar file in Linux

Bash
tar rvf file.tar *.c

list the contents and specify the tarfile using option -tf

Bash
tar tf file.tar

Applying pipe to through ‘grep command’ to find what we are looking for

Bash
tar tvf file.tar | grep "text to find" 

Viewing the Archive using option -tvf

Bash
tar tvf file.tar

To search for an image in .png format

Bash
tar tvf file.tar --wildcards '*.png' 

Zst

Bash
sudo apt install zstd

The extention .zst means that the archive is compressed by zstd.

Bash
tar --use-compress-program=unzstd -xvf archive.tar.zst
Bash
tar -I zstd -xvf archive.tar.zst
Bash
unzstd yourfilename.zst

Simple

Bash
zstd file.txt 

Decompress

Bash
zstd -d file.txt.zst
Bash
unzstd file.txt.zst

If you want to compress a directory, or combine multiple files into a single archive, you’ll need to use tar to create an archive and then compress it with zstd. You’ll need to add the --zstd option, along with any other flags you choose.

Bash
tar --zstd -cf archive.tar.zst /home/linuxnightly

Alternatively, we could use the a option with the tar command, which will choose the correct compression method based on the file extension (zst in this case) specified.

Bash
tar acf archive.tar.zst /home/linuxnightly

Use the following tar command to open a Zstandard tarball.

Bash
tar --zstd -xf archive.tar.zst

Alternatively, the a option can save us a few keystrokes again.

Bash
tar axf archive.tar.zst

More here

Gunzip Gzip

To Decompress A File Using The "gunzip" Command:

Bash
gunzip myfilename.gz

Force A File To Decompress:

Bash
gunzip -f myfilename.gz

To keep both the compressed and decompressed file:

Bash
gunzip -k myfile.gz

To display compressed output:

Bash
gunzip -l myfile.gz

Decompressing Lots Of Files Recursively:

Bash
gunzip -r /tmp/

Compression Size Time Elapsed Command

gzip 955 MB 1:45 tar cfz files.tar.gz files/

xz 856 MB 16:06 tar cfJ files.tar.xz files/

bzip2 943 MB 5:36 tar cfj files.tar.bz2 files/

7zip 851 MB 10:59 7z a files.7z files/

zip 956 MB 1:41 zip -r files.zip files/

rar 877 MB 6:37 rar a files.rar files/*

zstd 934 MB 0:43 tar --zstd -cf files.tar.zst files/

Back to top