×

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

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

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

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

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

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.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

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

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

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

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

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

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.