luis's personal blog

how to use tar to compress files

Published on 18/02/2025

Post Views

command:

tar -czvf filename.tar.gz /path/to/dir1/*

or

tar -czvf filename.tar.gz /path/to/dir1 dir2 file1 file2

to list the content of a compressed file

tar -ztvf filename.tar.gz

to extract

tar -xvf file.tar.gz  

or

tar -xzvf file.tar.gz  
tar command optionDescription
-cCreate a new archive
-xExtract files from an archive
-tList the contents of an archive
-vVerbose output
-f file.tar.gzUse archive file
-C DIRChange to DIR before performing any operations
-zFilter the archive through gzip i.e. compress or decompress archive

How compression works

Basically all files have redundant data, when you compress a file, the software is actually replacing all the duplicate data and rearranging the bytes, reducing its size, when you use .zip .rar .tar.gz or other compression methods, you are specifying how it was compressed.

So when you unzip, the software knows exactly how to do the opposite logic, creating files/folders that can be opened