TAR is short form of tape archive. They are used to sequentially read or write data. Compiling huge file list or deep directory structure into single file (tar file) is what we are seeing in this post. This resulting single file sometimes termed as tarball as well.

Creating tape archives of files or directories is extremely important when you are planning to FTP huge pile of them to another server. Since each file opens new FTP session and closes it after transfer finishes, if number of files are huge then FTP takes forever to complete data transfer (even if size of each file is small). Even if you have ample amount of bandwidth, due to session open and close operations FTP slows down to knees. In such cases, archiving all files in single tarball makes sense. This single archive can transfer at maximum speed and making transfer quick. After transfer you can again expand archive and get all files in place same as source.

Normally its good practice to first zip files or directories and then create tape archive of it. Zipping reduces their size and tar packs these reduced size files in single tarball! In Linux or Unix, command tar is used to create tar file. Lets see different operations which can be handled by tar command.

How to create tar file :

To create tar file -c option is used with tar command. It should be followed with filename of archive, normally it should have *.tar extension. It for users to identify its a tar archive, because Linux/Unix really dont care about extension.

In above output, we have used -v (verbose mode) for -c option. file.tar is archive name specified and end of the command is stuffed with list of files to be added in archive. Output shows filename on terminal which is currently being added to archive. Once all files are processed, you can see new archive in the specified path.

To add directories in tar file, you need to add directory path at end of same command above.

If you use full absolute path in command then tar will remove leading / so making members path relative. When archive will be deflated then all members will have path starting with current working directory/specified directory rather than leading /

In above output, you can see mentioned parameters in same sequence displayed. Relative file path means when this tar file extracts it will create files of directories in PWD or path specified in command line. For example in above output, if you extract this file in /home/user4/data directory then it will create tmp directory under /home/user4/data and extracts all files in it i.e. under /home/user4/data/tmp.

How to extract tar file :

-x option extracts files from tar file. As explained above, it will extract files and directories within tar into relative path.

In above outputs, step by step you can see tmp and dir4 structure created by tar command and extracted all files within it.

There are also few more options supported by tar like appending files in existing archive, zipping, updating new files in existing archive etc. We will see them in other post later. Leave us comments if you have any queries or suggestions.