Tar is an archiving software utility for the computer for compressing multiple files and directories into one archive file. It’s an acronym for Tape Archive that is broadly used in Linux/Unix. Tar archiving is mostly used for the purpose of data backup or distribution of source files or packages. Tar command is used for archiving, compressing, and extracting as it has a variety of its types, the most popular ones are tar, tar.gz, and tar.bz2.
$ tar -c [option] [archive name] [file] | [directory]
Note: The first option must be a mode specifier.
- -c: Create Archive.
- -r: Add files to the end of an archive.
- -t: Display list of all files inside archive.
- -x: Extract Archive.
1) Create Tar Archive
$ tar -cf ngix.tar /var/log/nginx
As tar command supports various archive file formats but its default archive format is .tar. In order to create a .tar archive, you need to add the hyphen c (-c) option which denotes creating an archive and the hyphen f(-f) option which denotes archive name. Then, determine the archive name with extension (.tar). Lastly, provide filenames or directories you want to archive. You can also pass the -v option to view verbose of process operation.
Note: During command execution, the -f option must be always ahead of the archive name just like the example given below.
$ tar -cvf ngix.tar /var/log/nginx
2) Listing Files Inside Tar Archive
$ tar -tf nginx.tar
Creating a tar archive was easy but what if you want to view content inside the tar file without extracting the file. This feature is provided by tar with hyphen t (-t), passing the -t option to the command just like in the above example do the work, and -f determine the archive name.
$ tar -tvf nginx.tar
In this example the -v option is to view files or directories details like the owner, created date, and full path inside the archive respectively.
3) Extract Tar Archive
$ tar -xvf nginx.tar
Just like other commands extracting archive it only varies in operator. In the above example, the -x operator tells the command to extract the tar file, the -v operator shows verbose of all files inside the archive, and -f specifies the archive name.
4) Add File to Existing Archive
$ tar -rvf nginx.tar /var/log/nginx/newlog.log
Let’s consider you have already an archive file and you want to add more files to that archive, to do so you need to first extract it then re-archive it including new files. Don’t you think it’s kind of a dull process? We can minimize this complexity with the -r operator. This operator tells the command to add new files to the end of an archive and -vf is as usual v for verbose and f for archive name. In the above example, we have a newlog.log file that has been added to the existing nginx.tar archive.
5) Compressing to tar.gz Archive
$ tar -cvzf nginx.tar.gz /var/log/nginx
The gzip is the most popular and widely used archiver that is used for compress and decompresses files and directories. To use this archiver we need to add the -z option to the command that tells the command to compress in gzip file format. In the above example, we have compressed the Nginx log directory into gzip file.
6) Compressing to tar.bz2 Archive
$ tar -cvjf nginx.tar.gz /var/log/nginx
The bz2 is the same as gzip but it highly compresses files which results in smaller file size as compared to gzip. It consumes more time than gzip to compress the same files. In the above example, we have compressed Nginx logs dir into bz2 archive by adding -j to the command which results in nginx.tar.gz.
7) Decompressing gzip Archive
$ tar -xvzf nginx.tar.gz
While decompressing gzip archive files we simply need to use -xvzf where x denotes extraction, v denotes verbose, z denotes gzip archive. Simply execute the command like the above example just replace the filename with yours.
8) Decompressing bz2 Archive
$ tar -xvjf nginx.tar.bz2
While decompressing bz2 archive files we need to pass -j operators denote bzip2, Just like the above example execute the command with -xvjf options and filename having tar.bz2 extension.
9) Extracting Specific File from Tar Archive
$ tar -xvf nginx.tar nginx/access.log
If you wonder how to single file from archive files we can do so by specifying the exact filename along with the exact directory name just like the above example. In the above example -xvf denotes extraction, verbose, and archive name respectively. We can also do the same for multiple files. For example,
$ tar -xvf nginx.tar nginx/access.log nginx/error.log
10) Extracting tar.gz File from stdout
$ wget -c https://downloads.apache.org/tomcat/tomcat-8/v8.5.64/bin/apache-tomcat-8.5.64.tar.gz -O - | tar -xz
In this example, while wget downloading the gzip file the command will write the file directly to stdout overriding the file writing process and pipe it to tar. After piping the files to tar, it will begin to decompress the file on the fly during download and result in extraction of gzip file. The example -c in wget resumes any interrupted downloads and -O – will tell wget to write directly to stdout.
11) Extracting Multiple File Using Wildcards Pattern
$ tar -xvf nginx.tar --wildcards 'nginx/*.log'
The wildcard is a set of metacharacters that helps in pattern searching set directories and files. In the above example, we extract all the files that end with dot log extension using wildcard pattern and tar.
12) Extracting Tar Archive in Different Directory
$ tar -xvf nginx.tar -C /home/demouser/
By default, the tar extracts the file in the current working directory but we can override its path by simply adding -C option that denotes directory. In the above example, we extract the file in the demouser home directory rather than its default directory which leads us to move extracted file according to our preferences.
13) Removing Files From Tar Archive
$ tar --delete -f nginx.tar nginx/error.log
In some cases, you have to delete unnecessary files from the archive to reduce the archive size or for other purposes. We can delete the file inside the archive by using the –delete operation simply needed to use the delete operation and provide a filename like in the above command. In the example, we have removed error.log from the nginx.tar archive file.
14) Excluding Files During Compression
$ tar -czvf nginx.tar.gz /var/log/nginx --exclude=/var/log/nginx/access.log
In most of the scenarios, we didn’t want to include some files in our compressed files or directories. In order to do that we have to keep that file separately to compress the required file or directory only. We can exclude the unnecessary files and directories using –exclude operation exactly like in the above example.
15) Check Size of Existing Archive File
$ tar -tvf nginx.tar /var/log/nginx | wc -c $ tar -tvzf nginx.tar.gz /var/log/nginx | wc -c $ tar -tvjf nginx.tar.bz2 /var/log/nginx | wc -c
In this example, we use the above commands to view archive file size while viewing files inside the archive file. Piping tar command with wc command with the -c option displays file size in KiloBytes.
16) Listing File from gzip and bz2 Archive
$ tar -tvf nginx.tar.gz | grep -i access.log $ tar -tvf nginx.tar.bz2 | grep -i error.log
To view the content inside the compressed files we can pass the -tvf option for both gzip and bz2. In the above example, I have added the grep command to filter out the output along with the shown tar commands as it has the same pattern of viewing files in the archive file, the only difference is its extension.
These are some of the useful tar commands used in compressing, decompressing archive files for the distribution of source files, or keeping backup for future use. In today’s context in most Linux/Unix systems, gzip or tar.gz archive is used for software or package distribution. Lastly, While using the tar command -c | -x | -t operators after tar command and -f before archive name is compulsory for the command to work
Also Read : 15 Quick Wget Command Examples in Linux