What command do I need to extract or create a .tar.gz file?

How to extract?

This is one of the common struggles if you are not an experienced Linux user. You downloaded .tgz or .tar.gz file and you are not sure what to do with it? The simple answer is:

tar -xvzf my_package.tar.gz

For some time the tar tool is also updated to handle auto-decompress so you can also try:

tar -xf my_package.tar.gz

The above commands will extract contents of your package in the current directory. If you want to place them somewhere else, at the end of the command you should add -C /name/of/destination/directory for example:

tar -xvzf my_package.tar.gz -C /home/username/my_package_contents/

How to create an archive?

If you want to create tar package, using compression provided by gzip you should use:

tar -cvzf my_package.tar.gz /name/of/directory/to/compress

You can use the .tar.gz extension as well as .tgz – they are used interchangeably and this does not affect contents of the package. If you are curious what the parameters used for tar command are doing, here is a short explanation:

-x - extract files from archive
-c - create a new archive
-v - verbosely list files processed
-z - use gzip
-f [file_name] - use file of name [file_name]

By the way, you may like the tool which is very helpful for anybody who wants to check what given command will do. I’m always using it to check commands I found on the internet – just in case. The page is named ExplainShell and it simply does that – explains shell commands. You can try it yourself.