Learn how to create tar file in Linux or Unix operating system. Understand how to create, list, extract tar files with preserving file permissions.
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.
# tar -cvf file.tar file2 file3 file2 file3 # ll total 32 -rw-r--r-- 1 root users 40 Jan 3 00:46 file2 -rw-r--r-- 1 root users 114 Jan 3 00:46 file3 -rw-r--r-- 1 root users 10240 Jan 9 10:22 file.tar
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.
# tar -cvf dir.tar dir4/ dir4/ dir4/file.tar dir4/file1 dir4/file3 dir4/file2
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 /
# tar -cvf dir2.tar /tmp/dir4 tar: Removing leading `/' from member names /tmp/dir4/ /tmp/dir4/file.tar /tmp/dir4/file1 /tmp/dir4/file3 /tmp/dir4/file2
You can see / being removed warning in output above.
How to view content of tar file :
Tar file content can be viewed without deflating it. -t i.e. table option does this task. It shows file permissions, ownerships, size, date timestamp and relative file path.
# tar -tvf dir4.tar drwxr-xr-x root/users 0 2017-01-09 10:28 tmp/dir4/ -rw-r--r-- root/users 10240 2017-01-09 10:22 tmp/dir4/file.tar -rw-r--r-- root/users 10240 2017-01-09 10:21 tmp/dir4/file1 -rw-r--r-- root/users 114 2017-01-03 00:46 tmp/dir4/file3 -rw-r--r-- root/users 40 2017-01-03 00:46 tmp/dir4/file2
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.
# tar -xvf dir4.tar tmp/dir4/ tmp/dir4/file.tar tmp/dir4/file1 tmp/dir4/file3 tmp/dir4/file2 # ll total 36 -rw-r--r-- 1 root users 30720 Jan 9 10:28 dir4.tar drwxr-xr-x 3 root users 4096 Jan 9 10:46 tmp # ll tmp total 4 drwxr-xr-x 2 root users 4096 Jan 9 10:28 dir4 # ll tmp/dir4 total 32 -rw-r--r-- 1 root users 10240 Jan 9 10:21 file1 -rw-r--r-- 1 root users 40 Jan 3 00:46 file2 -rw-r--r-- 1 root users 114 Jan 3 00:46 file3 -rw-r--r-- 1 root users 10240 Jan 9 10:22 file.tar
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.