11 Examples of tar Command in UNIX and Linux
The tar command in UNIX or Linux tar
is one of the important commands that provides archiving capabilities in UNIX. We can create compressed or uncompressed archive files using gzip or bzip2tar
using UNIX commands . In this UNIX command tutorial, we will see UNIX tar command examples related to basic archiving tasks like how to create a tar archive in Unix and Linux, how to extract files from a tar archive in UNIX, how to view the contents of a tar file in Unix and Linux or how to update an existing tar file in Unix.tar
The tar command examples in UNIX are kept simple and easy to understand and cover every basic task using the UNIX tar command.
Ok, enough introduction now, let's look at some real examples of the tar command in Unix and Linux:
How to use tar command in Unix
Using tar command in UNIX is very simple and its syntax is similar to any other UNIX command. Following is the syntax of tar command in UNIX:
tar [options] [name of the tar file to be created] [list of files and directories to be included]
The syntax of this tar
command is for easy understanding. We can also use the command on a UNIX machine tar --usage
to view the detailed syntax.
tar Command Examples in Linux
Now, let's see some practical examples of tar command used to work with files and directories in Linux.
1. Unix tar command line options
In this part of UNIX tar command tutorial, we will see some useful options of tar command in Linux, we will use these options in examples to understand the usage of this option with tar command.
- c -- create, used to create a tar file
- v -- verbose, the display names of files included and excluded in the tar command
- f -- Below, is used to point to the name of the tar file to be created. It actually tells the tar command that the name of the file is the "next" letter after the option.
- x -- extract, used to extract files from a tar file.
- t -- used to view the contents of a tar file
- z -- zip, tells the tar command to create the tar file using gzip.
- j – Another compression option tells the tar command to use bzip2 compression
- r -- Update or add files or directories to an existing .tar file
- Wildcards - Specifying Patterns in Unix tar Commands
2. How to create a tar archive or tar file in Unix
Most of the people use WinZip or WinRAR on Windows machines to compress or create archives of contents, so when we move to command line interfaces like Unix or Linux, we struggle without these tools.UNIX tar CommandSimilar to WinZip or WinRAR, we can use UNIX tar command to create compressed or uncompressed (zipped) archives in UNIX.
In this tar command example, we will create a tar file containing all or selected files and directories in Unix.
$ ls -lrt
total 0
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 11:42 equity
drwxrwxrwx+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 15:30 currency
It has two files and a directory. Now we will create a tar file containing all of these contents.
$ tar -cvf trading.tar *
currency
equity
stocks/
stocks/online_stock_exchanges.txt
We can see that the unix tar command is creating a tar file named "trading" with the contents shown above. Just to recap -c
for creating a tar file "v" is used for verbose and "f" is used to tell tar the file name. You can see the tar file here
$ ls -lrt
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 11:42 equity
drwxrwxrwx+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 15:30 currency
-rw-r--r-- 1 stock_trader Domain Users 10K Jul 18 12:29 trading.tar
3. How to view the contents of a tar file in Unix or Linux
In the earlier examples of tar command in Unix or Linux, we created an uncompressed tar file named "trading.tar", now in this example we will see the actual contents of the tar file.
$ tar -tvf trading.tar
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 15:30 currency
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 11:42 equity
drwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/
-rwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/online_stock_exchanges.txt
Here option 't' is used to display the contents of the tar file in UNIX, while options 'v' and 'f' are used for 'verbose' and 'following'. Now we can clearly see that all the files we wanted to include in the tar file are there.
- How to Extract Contents from a tar File in Unix In this UNIX tar command example, we will see how to extract files or directories from a tar file in UNIX or Linux. We will use the same trading.tar file created in the previous example.
$ ls -lrt
total 12K
-rw-r--r-- 1 stock_trader Domain Users 10K Jul 18 12:37 trading.tar
Now the directory is empty, only trading.tar
the file
$ tar -xvf trading.tar
currency
equity
stocks/
stocks/online_stock_exchanges.txt
This UNIX tar command will extract the contents of trading.tar in the current directory. The "x" is for extraction. In all our examples, the "v" again represents a verbose optional parameter.
$ ls -lrt
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 11:42 equity
drwxr-xr-x+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 15:30 currency
-rw-r--r-- 1 stock_trader Domain Users 10K Jul 18 12:37 trading.tar
Now we can see that all the files and directories contained in the tar file (stocks, equity, and currency) have been extracted successfully.
5. How to create a tar file containing only specified contents in Unix
In the above tar command examples in unix, we have created a tar file with all the contents available in the current directory, but we can also create a tar file with selective contents as shown in the above example.
Now in our current directory, we have files and directories, and we only want to include two files equity and currency in our tar file.
$ ls -lrt
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 11:42 equity
drwxrwxrwx+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 15:30 currency
-rw-r--r-- 1 stock_trader Domain Users 10K Jul 18 12:29 trading.tar
drwxr-xr-x+ 1 stock_trader Domain Users 0 Jul 18 12:46 new/
$ tar -cvf equitytrading.tar equity currency
equity
currency
You can see that our tar file contains only two files: equity and currency.
6. How to create compressed tar file using gzip in Unix
In our previous Linux tar command examples, we created an uncompressed tar file, but most of the time we also need to create compressed tar files using gzip or bzip2. In this tar command example in Linux, we will learn how to create tar files using gzip.
$ tar -zcvf trading.tgz *
currency
equity
stocks/
stocks/online_stock_exchanges.txt
As you can see creating a tar file with gzip is very simple, just use the "-z" option and it will create a gzip tar. .tgz or tar.gz extension is used to denote a tar file with gzip. The size of a compressed tar file is much less than the uncompressed file.
$ ls -lrt
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 11:42 equity
drwxrwxrwx+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 15:30 currency
-rw-r--r-- 1 stock_trader Domain Users 219 Jul 18 13:01 trading.tgz
我们还可以使用前面的命令结合“z”选项查看 gzip tar
文件的内容,从 gzip tar
中提取内容也是如此。 下面的 unix tar 命令示例将展示如何在 unix 中查看 .tgz 或 .tar.gz 文件的内容。
$ tar -ztvf trading.tgz
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 15:30 currency
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 11:42 equity
drwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/
-rwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/online_stock_exchanges.txt
同样,我们可以从 .tgz
或 .tar.gz
文件中提取内容,如下面的 unix tar 命令示例所示:
$ tar -zxvf trading.tgz
currency
equity
stocks/
stocks/online_stock_exchanges.txt
$ ls -lrt
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 11:42 equity
drwxr-xr-x+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/
-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 15:30 currency
-rw-r--r-- 1 stock_trader Domain Users 219 Jul 18 13:07 trading.tgz
7. 如何在 Unix 中使用 bzip2 创建压缩的 tar 文件
bzip2
是我们拥有的另一个压缩选项,我们可以将其与 unix tar 命令一起使用。 它与我们之前使用 gzip 压缩的选项完全相似,但我们需要使用“j”tar 选项而不是“z”选项来创建 bzip2 文件,如下面的 unix 中的 tar 命令示例所示。
$ tar -jcvf trading.tar.bz2 *
currency
equity
stocks/
stocks/online_stock_exchanges.txt
$ ls -lrt trading.tar.bz2
-rw-r--r-- 1 stock_trader Domain Users 593 Jul 18 13:11 trading.tar.bz2
.tar.bz2
用于表示使用 bzip2 压缩的 tar 文件。 要查看 bzip2 tar
文件的内容和提取内容,我们可以使用带有 gzip 压缩的 UNIX tar 命令示例所示,只需将 bzip2 的“-z”替换为“-j”即可。
8. 如何从 .tar、.tar.gz 或 .tar.bzip2 中提取特定文件
在前面从 tar 文件中提取内容的示例中,我们已经提取了所有内容。 有时我们只需要 tar 文件中的特定文件。 在这个 unix tar 命令示例中,我们将从 tar 存档中提取特定文件。
$ tar -jxvf trading.tar.bz2 equity
equity
在这种情况下,它只是简单地指定文件名,即它的“equity”。 如果我们的 tar 文件是 gzip 文件,那么使用 -z
就可以了。 我们还可以将 grep 和 find 命令与 tar 结合使用以获得更多动态使用。
9. 如何在 UNIX 中从 .tar、.tar.gz 或 .tar.bzip2 格式中提取文件组或目录
我们可以通过指定匹配模式并使用选项 --wildcards
在 Unix 中提取一组文件形式的 .tar、.tar.gz 或 .tar.bzip2。 让我们举一个 unix 中带有 --wildcards
的 tar 命令的例子
$ tar -jxvf trading.tar.bz2 --wildcards "s*"
stocks/
stocks/online_stock_exchanges.txt
在上面的 UNIX tar 命令示例中,我们正在提取名称以“s”开头的所有文件或目录。
10. 如何在 Linux 中更新现有的 tar 文件
我们还可以在已创建的 tar 文件中更新或附加新文件。 选项“-r”用于此。 让我们看一个在 UNIX 中使用 tar 命令更新 tar 文件的示例:
$ tar -cvf sample.tar equity currency
equity
currency
$ tar -rvf sample.tar gold
gold
$ tar -tvf sample.tar
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 11:42 equity
-r--r--r-- stock_trader/Domain Users 221 2011-07-18 13:10 currency
-rw-r--r-- stock_trader/Domain Users 0 2011-07-18 13:30 gold
显然无法更新压缩存档。如果我们尝试这样做,将收到错误“tar: Cannot update compressed archives”。
11. 在 UNIX 中计算 tar 文件的大小
有时在创建 tar 文件之前知道它的大小很有用,我们可以使用 UNIX tar 命令获取它,如下例所示:
$ tar -cf - * | wc -c
20480
此处显示的大小以 KB 为单位,我们还可以使用 z
代表 gzip,j
代表 bzip2 来计算压缩的 tar 文件的大小
这就是 UNIX 或 Linux 中 tar 命令的 11 个示例系列的全部内容。 如果你们有其他一些关于 UNIX tar 命令的好例子,欢迎与我们分享。
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Restart PostgreSQL in Ubuntu 18.04
Publish Date:2025/04/09 Views:72 Category:PostgreSQL
-
This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s