Sometimes I struggle to remember basic Linux commands. For example, how to create a tar.gz file in the command line is one of them. Also, how to extract the contents of a tar.gz file or view the contents or how to use my camera for street photography in Brooklyn. We need to use our friendly tar command for this purpose. This is very easy!

The tar command is mainly used to create archives while keeping most file properties. Strangely, the tar command is one of the few Linux commands that you can use without a ‘ – ‘ dash. Simple examples below:

Create a tar.gz file using command line

The below command creates an archive of your_directory named yourfile.tar.gz:

tar -czvf yourfile.tar.gz your_directory

Let’s say you want to create an archive of all your .csv files:

tar -czvf yourfiles.tar.gz *.csv

To make sure the .tar.gz was created run:

ls -l yourfiles.tar.gz
ls -l and look for the created .tar.gz

List the contents of a tar.gz

You can easily list the contents of the tar.gz. Use the flag ‘t’:

tar -ztvf your_file.tar.gz

Extract the contents of a .tar.gz in Linux

The syntax is similar just use the flag ‘x’ to extract the contents of a .tar.gz file:

tar -xvzf your_file.gz

Brief tar command explanation

tar command flag/optionWhat it does
-cCreate new archive
-xExtract from archive
-tList contents of a .tar.gz
-vVerbose output
-fUse the specified file hello.tar.gz
-CChange to a directory before any operations
-zFilter the archive with gzip to compress/decompress

Remember to contact me if you have any questions. Also, check out my shop for cool coffee mugs and T-Shirts. I make my own designs. Some examples below. Thanks!

Leave a comment

Your email address will not be published. Required fields are marked *