×

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.


Related Topics

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

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...

4 minutes read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

3 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.