Learn tar command with examples in Linux or Unix

This tutorial shows you about tar command, extract, and compress with example commands.

Tar command in UNIX:-

Tar command is abbreviated as tape archive.tar is one of the most popular zipped/achieve commands in Unix like zipping in windows to compress the files and store all the files into a single file tar.gz.

tar and gzip are more popular zipped archive commands in Linux or Unix like zipping, WinRAR, etc in windows.

I have also blogged about Top 10 gzip examples in Linux,unix for gzip usage.

Most of the open-source frameworks like Maven, Tomcat, JBoss, etc..are shipped in tar.gz format to install in Unix environments. blogging all these for reference.

tar command examples

  • How to create an archive tar.gz for a folder or tar the files
tar –cvzf newfilename.tar.gz existing-folder-name

This compress and create the tar version/zipped as new-file-name.tar.gz from the existing folder. The structure of files, directories, and subdirectories in existing-folder are the same way created for new-file-name.tar.gz

  • c- create a new gz file

  • z - compressed the files

  • v- print the list of the files shown in stdout

  • f- with this option, the archive name is specified within the command

  • Extract/unzip the tar.gz file into a folder or untar

tar –xvzf filename.tar.gz

X option means unzip the contents of this archive The remaining options are the same as in #1

This will extract and copy all the files to the current directory with the folder as the filename where this command is running.

  • Show all the files of the tar file
tar tvf filename.tar.gz

This will show all the files in filename.tar.gz to the stdout console. Option t – list all the contents of the archive

The advantages of tar are it will take a backup of your files The disadvantage is a single file cannot be extracted from the zipped or archived file

Extract or unzip the archive with gunzip, tar command:-

With this both commands, we can be used to unzip the tar.gz or .gz files.’

To unzip the tar.gz file, there are two steps involved. First, unzip .tar.gz into the tar file in the following command, and then use the tar command

gunzip filename.tar.gz
tar –xzf filename.tar

filename.tar is created with the first command, this is passed to the tar command to extract the files in the tar.

Hope you got the basic start for tar, and gunzip with examples.

Please feel free to comment and share your thoughts.