tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine multiple files and directories into a single archive file, and also extract the files from the archive. The basic syntax for the 'tar' command is:

tar [options] [archive-file] [files-to-archive]

Where "options" are any optional flags or settings that you want to use, "archive-file" is the file name of the archive that you want to create or extract, and "files-to-archive" are the files and directories that you want to add to the archive or extract from the archive.

One of the most basic and common uses of the 'tar' command is to create an archive file. For example, if you want to create an archive file named 'archive.tar' that contains the files 'file1.txt' and 'file2.txt' and the directory 'folder1', you would use the command:

tar -cvf archive.tar file1.txt file2.txt folder1

The above command creates an archive file named 'archive.tar' and adds the files 'file1.txt', 'file2.txt' and the directory 'folder1' to it. The '-c' option tells tar to create a new archive, the '-v' option tells tar to display the files as it adds them to the archive, and the '-f' option tells tar to use the file 'archive.tar' as the archive file.

Another common usage of the 'tar' command is to extract files from an archive. For example, if you want to extract the files from an archive named 'archive.tar', you would use the command:

tar -xvf archive.tar

The above command extracts the files from the archive 'archive.tar' and the '-x' option tells tar to extract the files from the archive, the '-v' option tells tar to display the files as it extracts them, and the '-f' option tells tar to use the file 'archive.tar' as the archive file.

You can also use the 'tar' command to extract specific files or directories from an archive. For example, if you want to extract only the file 'file1.txt' from the archive 'archive.tar', you would use the command:

tar -xf archive.tar file1.txt

The above command extracts only the file 'file1.txt' from the archive 'archive.tar'

The 'tar' command also provides options to add files to an existing archive, and to list the contents of an archive. The '-r' option can be used to add files to an existing archive and the '-t' option can be used to list the contents of an archive.

Additionally, 'tar' command also supports compression feature, you can use -z option to compress the archive using gzip and -j option to compress the archive using bzip2. For example, if you want to compress the archive file 'archive.tar' using gzip, you would use the command:

tar -zcvf archive.tar.gz file1.txt file2.txt folder1

It's worth noting that when you create a compressed archive file, the original files will not be deleted, they are still present in the system.

It's important to note that, when using the 'tar' command, it's important to be careful with the options you're using. A small typo or mistake in the options can cause unexpected results and even data loss. It's also important to test the command on a small set of data before applying it to a large set of data.

It is also worth mentioning that the 'tar' command can be used in conjunction with other Linux commands and tools to automate tasks such as backups and file transfer. For example, you can use the 'cron' job scheduler to schedule regular backups of your files using the 'tar' command, and use the 'scp' command to securely transfer the archives to a remote server. You can also use the 'pipe' command to pass the output of one command as the input to another command, for example, you can use the 'find' command to locate specific files and then use the 'tar' command to archive them.

Another useful feature of 'tar' command is the ability to create incremental backups. This can be done by using the '-g' option which creates a new archive and saves a copy of the archive's meta-data, and then using '-N' option to specify the time frame, it tells tar to only add files that have been modified since the date specified.

In summary, the 'tar' command is an essential tool for managing and archiving files in Linux. It provides the ability to create, maintain and extract files from archive files called tarballs. The command also supports compression feature using gzip and bzip2. It can be used to automate tasks such as backups and file transfer, as well as create incremental backups. It's an important command that should be used carefully and with appropriate permissions. With a good understanding of how the command works and its implications, you will be able to efficiently and effectively manage and archive files on your Linux systems.