Merge pull request #4455 from GHLandy/master

[完成翻译] 20160911 17 tar command practical examples in Linux
This commit is contained in:
VicYu 2016-09-21 00:07:43 -05:00 committed by GitHub
commit 0c303e917d
2 changed files with 308 additions and 310 deletions

View File

@ -1,310 +0,0 @@
GHLandy translating
17 tar command practical examples in Linux
=====
Tar (tape archive ) is the most widely used command in Unix like operating system for creating archive of multiple files and folders into a single archive file and that archive file can be further compressed using gzip and bzip2 techniques. In other words we can say that tar command is used to take backup by archiving multiple files and directory into a single tar or archive file and later on files & directories can be extracted from the tar compressed file.
In this article we will discuss 17 practical examples of tar command in Linux.
Syntax of tar command:
```
# tar <options> <files>
```
Some of the commonly used options in tar command are listed below :
![](http://www.linuxtechi.com/wp-content/uploads/2016/09/tar-command-options.jpg)
Note : hyphen ( ) in the tar command options are optional.
### Example: 1 Create a tar archive file
Lets create a tar file of /etc directory and /root/ anaconda-ks.cfg file.
```
[root@linuxtechi ~]# tar -cvf myarchive.tar /etc /root/anaconda-ks.cfg
```
Above command will create a tar file with the name “myarchive” in the current folder. Tar file contains all the files and directories of /etc folder and anaconda-ks.cfg file.
In the tar command -c option specify to create a tar file, -v is used for verbose output and -f option is used to specify the archive file name.
```
[root@linuxtechi ~]# ls -l myarchive.tar
-rw-r--r--. 1 root root 22947840 Sep 7 00:24 myarchive.tar
[root@linuxtechi ~]#
```
### Example:2 List the contents of tar archive file.
Using t option in tar command we can view the contents of tar files without extracting it.
```
[root@linuxtechi ~]# tar -tvf myarchive.tar
```
Listing a specific file or directory from tar file. In the below example i am trying to view whether anaconda-ks.cfg file is there in the tar file or not.
```
[root@linuxtechi ~]# tar -tvf myarchive.tar root/anaconda-ks.cfg
-rw------- root/root 953 2016-08-24 01:33 root/anaconda-ks.cfg
[root@linuxtechi ~]#
```
### Example:3 Append or add files to end of archive or tar file.
-r option in the tar command is used to append or add file to existing tar file. Lets add /etc/fstab file in data.tar
```
[root@linuxtechi ~]# tar -rvf data.tar /etc/fstab
```
Note: In the Compressed tar file we cant append file or directory.
### Example:4 Extracting files and directories from tar file.
-x option is used to extract the files and directories from the tar file. Lets extract the content of above created tar file.
```
[root@linuxtechi ~]# tar -xvf myarchive.tar
```
This command will extract all the files and directories of myarchive tar file in the current working directory.
### Example:5 Extracting tar file to a particular folder.
In case you want to extract tar file to a particular folder or directory then use -C option and after that specify the path of a folder.
```
[root@linuxtechi ~]# tar -xvf myarchive.tar -C /tmp/
```
### Example:6 Extracting particular file or directory from tar file.
Lets assume you want to extract only anaconda-ks.cfg file from the tar file under /tmp folder.
Syntax :
```
# tar xvf {tar-file } {file-to-be-extracted } -C {path-where-to-extract}
[root@linuxtechi tmp]# tar -xvf /root/myarchive.tar root/anaconda-ks.cfg -C /tmp/
root/anaconda-ks.cfg
[root@linuxtechi tmp]# ls -l /tmp/root/anaconda-ks.cfg
-rw-------. 1 root root 953 Aug 24 01:33 /tmp/root/anaconda-ks.cfg
[root@linuxtechi tmp]#
```
### Example:7 Creating and compressing tar file (tar.gz or .tgz )
Lets assume that we want to create a tar file of /etc and /opt folder and also want to compress it using gzip tool. This can be achieved using -z option in tar command. Extensions of such tar files will be either tar.gz or .tgz
```
[root@linuxtechi ~]# tar -zcpvf myarchive.tar.gz /etc/ /opt/
```
Or
```
[root@linuxtechi ~]# tar -zcpvf myarchive.tgz /etc/ /opt/
```
### Example:8 Creating and compressing tar file ( tar.bz2 or .tbz2 )
Lets assume that we want to create compressed (bzip2) tar file of /etc and /opt folder. This can be achieved by using the option ( -j) in the tar command.Extensions of such tar files will be either tar.bz2 or .tbz
```
[root@linuxtechi ~]# tar -jcpvf myarchive.tar.bz2 /etc/ /opt/
```
Or
```
[root@linuxtechi ~]# tar -jcpvf myarchive.tbz2 /etc/ /opt/
```
### Example:9 Excluding particular files or type while creating tar file.
Using “exclude” option in tar command we can exclude the particular file or file type while creating tar file. Lets assume we want to exclude the file type of html while creating the compressed tar file.
```
[root@linuxtechi ~]# tar -zcpvf myarchive.tgz /etc/ /opt/ --exclude=*.html
```
### Example:10 Listing the contents of tar.gz or .tgz file
Contents of tar file with the extensions tar.gz or .tgz is viewed by using the option -t. Example is shown below :
```
[root@linuxtechi ~]# tar -tvf myarchive.tgz | more
.............................................
drwxr-xr-x root/root 0 2016-09-07 08:41 etc/
-rw-r--r-- root/root 541 2016-08-24 01:23 etc/fstab
-rw------- root/root 0 2016-08-24 01:23 etc/crypttab
lrwxrwxrwx root/root 0 2016-08-24 01:23 etc/mtab -> /proc/self/mounts
-rw-r--r-- root/root 149 2016-09-07 08:41 etc/resolv.conf
drwxr-xr-x root/root 0 2016-09-06 03:55 etc/pki/
drwxr-xr-x root/root 0 2016-09-06 03:15 etc/pki/rpm-gpg/
-rw-r--r-- root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
-rw-r--r-- root/root 1004 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
-rw-r--r-- root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7
-rw-r--r-- root/root 3140 2015-09-15 06:53 etc/pki/rpm-gpg/RPM-GPG-KEY-foreman
..........................................................
```
### Example:11 Listing the contents of tar.bz2 or .tbz2 file.
Contents of tar file with the extensions tar.bz2 or .tbz2 is viewed by using the option -t. Example is shown below :
```
[root@linuxtechi ~]# tar -tvf myarchive.tbz2 | more
........................................................
rwxr-xr-x root/root 0 2016-08-24 01:25 etc/pki/java/
lrwxrwxrwx root/root 0 2016-08-24 01:25 etc/pki/java/cacerts -> /etc/pki/ca-trust/extracted/java/cacerts
drwxr-xr-x root/root 0 2016-09-06 02:54 etc/pki/nssdb/
-rw-r--r-- root/root 65536 2010-01-12 15:09 etc/pki/nssdb/cert8.db
-rw-r--r-- root/root 9216 2016-09-06 02:54 etc/pki/nssdb/cert9.db
-rw-r--r-- root/root 16384 2010-01-12 16:21 etc/pki/nssdb/key3.db
-rw-r--r-- root/root 11264 2016-09-06 02:54 etc/pki/nssdb/key4.db
-rw-r--r-- root/root 451 2015-10-21 09:42 etc/pki/nssdb/pkcs11.txt
-rw-r--r-- root/root 16384 2010-01-12 15:45 etc/pki/nssdb/secmod.db
drwxr-xr-x root/root 0 2016-08-24 01:26 etc/pki/CA/
drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/certs/
drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/crl/
drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/newcerts/
drwx------ root/root 0 2015-06-29 08:48 etc/pki/CA/private/
drwx------ root/root 0 2015-11-20 06:34 etc/pki/rsyslog/
drwxr-xr-x root/root 0 2016-09-06 03:44 etc/pki/pulp/
..............................................................
```
### Example:12 Extracting or unzip tar.gz or .tgz files.
tar files with extension tar.gz or .tgz is extracted or unzipped with option -x and -z. Example is shown below :
```
[root@linuxtechi ~]# tar -zxpvf myarchive.tgz -C /tmp/
```
Above command will extract tar file under /tmp folder.
Note : Now a days tar command will take care compression file types automatically while extracting, it means it is optional for us to specify compression type in tar command. Example is shown below :
```
[root@linuxtechi ~]# tar -xpvf myarchive.tgz -C /tmp/
```
### Example:13 Extracting or unzip tar.bz2 or .tbz2 files
tar files with the extension tar.bz2 or .tbz2 is extract with option -j and -x. Example is shown below:
```
[root@linuxtechi ~]# tar -jxpvf myarchive.tbz2 -C /tmp/
```
Or
```
[root@linuxtechi ~]# tar xpvf myarchive.tbz2 -C /tmp/
```
### Example:14 Scheduling backup with tar command
There are some real time scenarios where we have to create the tar files of particular files and directories for backup purpose on daily basis. Lets suppose we have to take backup of whole /opt folder on daily basis, this can be achieved by creating a cron job of tar command. Example is shown below :
```
[root@linuxtechi ~]# tar -zcvf optbackup-$(date +%Y-%m-%d).tgz /opt/
```
Create a cron job for above command.
### Example:15 Creating compressed archive or tar file with -T and -X option.
There are some real time scenarios where we want tar command to take input from a file and that file will consists of path of files & directory that are to be archived and compressed, there might some files that we would like to exclude in the archive which are mentioned in input file.
In the tar command input file is specified after -T option and file which consists of exclude list is specified after -X option.
Lets suppose we want to archive and compress the directories like /etc , /opt and /home and want to exclude the file /etc/sysconfig/kdump and /etc/sysconfig/foreman, Create a text file /root/tar-include and /root/tar-exclude and put the following contents in respective file.
```
[root@linuxtechi ~]# cat /root/tar-include
/etc
/opt
/home
[root@linuxtechi ~]#
[root@linuxtechi ~]# cat /root/tar-exclude
/etc/sysconfig/kdump
/etc/sysconfig/foreman
[root@linuxtechi ~]#
```
Now run the below command to create and compress archive file.
```
[root@linuxtechi ~]# tar zcpvf mybackup-$(date +%Y-%m-%d).tgz -T /root/tar-include -X /root/tar-exclude
```
### Example:16 View the size of .tar , .tgz and .tbz2 file.
Use the below commands to view the size of tar and compressed tar files.
```
[root@linuxtechi ~]# tar -czf - data.tar | wc -c
427
[root@linuxtechi ~]# tar -czf - mybackup-2016-09-09.tgz | wc -c
37956009
[root@linuxtechi ~]# tar -czf - myarchive.tbz2 | wc -c
30835317
[root@linuxtechi ~]#
```
### Example:17 Splitting big tar file into smaller files.
In Unix like operating big file is divided or split into smaller files using split command. Big tar file can also be divided into the smaller parts using split command.
Lets assume we want to split mybackup-2016-09-09.tgz file into smaller parts of each 6 MB.
```
Syntax : split -b <Size-in-MB> <tar-file-name>.<extension> “prefix-name”
```
```
[root@linuxtechi ~]# split -b 6M mybackup-2016-09-09.tgz mybackup-parts
```
Above command will split the mybackup compressed tar file into the smaller files each of size 6 MB in current working directory and split file names will starts from mybackup-partsaa … mybackup-partsag. In case if you want to append numbers in place of alphabets then use -d option in above split command.
```
[root@linuxtechi ~]# ls -l mybackup-parts*
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaa
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsab
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsac
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsad
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsae
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaf
-rw-r--r--. 1 root root 637219 Sep 10 03:05 mybackup-partsag
[root@linuxtechi ~]#
```
Now we can move these files into another server over the network and then we can merge all the files into a single tar compressed file using below mentioned command.
```
[root@linuxtechi ~]# cat mybackup-partsa* > mybackup-2016-09-09.tgz
[root@linuxtechi ~]#
```
Thats all, hope you like different examples of tar command. Please share your feedback and comments.
--------------------------------------------------------------------------------
via: http://www.linuxtechi.com/17-tar-command-examples-in-linux/
作者:[Pradeep Kumar ][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: http://www.linuxtechi.com/author/pradeep/

View File

@ -0,0 +1,308 @@
17 个 tar 命令使用例示
=====
Tar (tape archive 磁带归档的缩写LCTT 译注:最初设计用于将打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录) 是类 Unix 系统中使用最广泛的命令,用于归档多个文件或者目录到单个归档文件中,并且可以同时对归档文件使用 gzip 或者 bzip2 压缩技术。换句话说tar 命令也可以用于备份:先是归档多个文件和目录到一个单独的 tar 文件,然后在需要之时将 tar 文件中的文件和目录释放出来。
本文将介绍 tar 的 17 个使用例示。
tar 命令语法如下:
```
# tar <选项> <文件>
```
下面列举 tar 命令中一些常用的选项:
![](http://www.linuxtechi.com/wp-content/uploads/2016/09/tar-command-options.jpg)
注 : 在 tar 命令选项中的连接符 ( ) 是可选的 (LCTT 译注:不用 - 也没事)。
### 例示 1创建一个 tar 归档文件
现在来创建一个 tar 文件,将 /etc/ 目录和 /root/anaconda-ks.cfg 文件打包进去。
```
[root@linuxtechi ~]# tar -cvf myarchive.tar /etc /root/anaconda-ks.cfg
```
以上命令会在当前目录创建一个名为 “myarchive” 的 tar 文件,内含 /etc/ 目录和 /root/anaconda-ks.cfg 文件。
其中,‘-c 选项表示要创建 tar 文件,‘-v 选项用于输出 tar 进程到屏幕上,‘-f 选项则是指定归档文件名称。
```
[root@linuxtechi ~]# ls -l myarchive.tar
-rw-r--r--. 1 root root 22947840 Sep 7 00:24 myarchive.tar
[root@linuxtechi ~]#
```
### 例示 2列出归档文件中的内容
在 tar 命令中使用 t 选项可以不用释放其中的文件就可以快速列出文件中包含的内容。
```
[root@linuxtechi ~]# tar -tvf myarchive.tar
```
列出 tar 文件中的指定文件和目录。下列命令尝试查看 anaconda-ks.cfg 文件是否存在于 tar 文件中。
```
[root@linuxtechi ~]# tar -tvf myarchive.tar root/anaconda-ks.cfg
-rw------- root/root 953 2016-08-24 01:33 root/anaconda-ks.cfg
[root@linuxtechi ~]#
```
### 例示 3追加文件到归档 ( tar ) 文件中
-r 选项用于向已有的 tar 文件中追加文件。下面来将 /etc/fstab 添加到 data.tar 中。
```
[root@linuxtechi ~]# tar -rvf data.tar /etc/fstab
```
注:在压缩过的 tar 文件中无法进行追加文件操作。
### 例示 4从 tar 文件中释放文件以及目录
-x 选项用于释放出 tar 文件中的文件和目录。下面来释放上边创建的 tar 文件中的内容。
```
[root@linuxtechi ~]# tar -xvf myarchive.tar
```
这个命令会在当前目录中释放出 myarchive tar 文件中的内容。
### 例示 5释放 tar 文件到指定目录
假如你想要释放 tar 文件中的内容到指定的文件夹或者目录,使用 -C 选项后边加上指定的文件的路径。
```
[root@linuxtechi ~]# tar -xvf myarchive.tar -C /tmp/
```
### 例示 6释放 tar 文件中的指定文件或目录
假设你只要释放 tar 文件中的 anaconda-ks.cfg 到 /tmp 目录。
语法如下:
```
# tar xvf {tar-file } {file-to-be-extracted } -C {path-where-to-extract}
[root@linuxtechi tmp]# tar -xvf /root/myarchive.tar root/anaconda-ks.cfg -C /tmp/
root/anaconda-ks.cfg
[root@linuxtechi tmp]# ls -l /tmp/root/anaconda-ks.cfg
-rw-------. 1 root root 953 Aug 24 01:33 /tmp/root/anaconda-ks.cfg
[root@linuxtechi tmp]#
```
### 例示 7创建并压缩归档文件 ( .tar.gz 或 .tgz )
假设我们需要打包 /etc 和 /opt 文件,并用 gzip 工具将其压缩。可以在 tar 命令中使用 -z 选项来实现。这样 tar 文件的扩展名可以是 .tar.gz 或者 .tgz。
```
[root@linuxtechi ~]# tar -zcpvf myarchive.tar.gz /etc/ /opt/
```
```
[root@linuxtechi ~]# tar -zcpvf myarchive.tgz /etc/ /opt/
```
### 例示 8创建并压缩归档文件 ( .tar.bz2 或 .tbz2 )
假设我们需要打包 /etc 和 /opt 文件,并是用 bzip2 压缩。可以在 tar 命令中使用 -j 选项来实现。这样 tar 文件的扩展名可以是 .tar.bz2 或者 .tbz。
```
[root@linuxtechi ~]# tar -jcpvf myarchive.tar.bz2 /etc/ /opt/
```
```
[root@linuxtechi ~]# tar -jcpvf myarchive.tbz2 /etc/ /opt/
```
### 例示 9排除指定文件或类型后创建 tar 文件
创建 tar文件时在 tar 命令中使用 “exclude” 选项可以排除指定文件或者类型。假设在创建压缩的 tar 文件时要排除 html 文件。
```
[root@linuxtechi ~]# tar -zcpvf myarchive.tgz /etc/ /opt/ --exclude=*.html
```
### 例示 10列出 .tar.gz 或 .tgz 文件中的内容
使用 -t 选项可以查看 tar.gz 或 .tgz 文件中内容。如下:
```
[root@linuxtechi ~]# tar -tvf myarchive.tgz | more
.............................................
drwxr-xr-x root/root 0 2016-09-07 08:41 etc/
-rw-r--r-- root/root 541 2016-08-24 01:23 etc/fstab
-rw------- root/root 0 2016-08-24 01:23 etc/crypttab
lrwxrwxrwx root/root 0 2016-08-24 01:23 etc/mtab -> /proc/self/mounts
-rw-r--r-- root/root 149 2016-09-07 08:41 etc/resolv.conf
drwxr-xr-x root/root 0 2016-09-06 03:55 etc/pki/
drwxr-xr-x root/root 0 2016-09-06 03:15 etc/pki/rpm-gpg/
-rw-r--r-- root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
-rw-r--r-- root/root 1004 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
-rw-r--r-- root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7
-rw-r--r-- root/root 3140 2015-09-15 06:53 etc/pki/rpm-gpg/RPM-GPG-KEY-foreman
..........................................................
```
### 例示 11列出 .tar.bz2 或 .tbz2 文件中的内容
使用 -t 选项可以查看 tar.bz2 或 .tbz2 文件中内容。如下:
```
[root@linuxtechi ~]# tar -tvf myarchive.tbz2 | more
........................................................
rwxr-xr-x root/root 0 2016-08-24 01:25 etc/pki/java/
lrwxrwxrwx root/root 0 2016-08-24 01:25 etc/pki/java/cacerts -> /etc/pki/ca-trust/extracted/java/cacerts
drwxr-xr-x root/root 0 2016-09-06 02:54 etc/pki/nssdb/
-rw-r--r-- root/root 65536 2010-01-12 15:09 etc/pki/nssdb/cert8.db
-rw-r--r-- root/root 9216 2016-09-06 02:54 etc/pki/nssdb/cert9.db
-rw-r--r-- root/root 16384 2010-01-12 16:21 etc/pki/nssdb/key3.db
-rw-r--r-- root/root 11264 2016-09-06 02:54 etc/pki/nssdb/key4.db
-rw-r--r-- root/root 451 2015-10-21 09:42 etc/pki/nssdb/pkcs11.txt
-rw-r--r-- root/root 16384 2010-01-12 15:45 etc/pki/nssdb/secmod.db
drwxr-xr-x root/root 0 2016-08-24 01:26 etc/pki/CA/
drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/certs/
drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/crl/
drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/newcerts/
drwx------ root/root 0 2015-06-29 08:48 etc/pki/CA/private/
drwx------ root/root 0 2015-11-20 06:34 etc/pki/rsyslog/
drwxr-xr-x root/root 0 2016-09-06 03:44 etc/pki/pulp/
..............................................................
```
### 例示 12解压 .tar.gz 或 .tgz 文件
使用 -x-z 选项来解压 tar.gz 或 .tgz 文件。如下:
```
[root@linuxtechi ~]# tar -zxpvf myarchive.tgz -C /tmp/
```
以上命令将 tar 文件解压到 /tmp 目录。
注:现今的 tar 命令会执行解压动作前自动检查文件的压缩类型,这意味着我们在使用 tar 命令是可以不用指定文件的压缩类型。如下:
```
[root@linuxtechi ~]# tar -xpvf myarchive.tgz -C /tmp/
```
### 例示 13解压 .tar.bz2 或 .tbz2 文件
使用 -j-z 选项来解压 tar.bz2 或 .tbz2 文件。如下:
```
[root@linuxtechi ~]# tar -jxpvf myarchive.tbz2 -C /tmp/
```
```
[root@linuxtechi ~]# tar xpvf myarchive.tbz2 -C /tmp/
```
### 例示 14使用 tar 命令进行定时备份
总有一些实时场景需要我们对指定的文件和目录进行打包,已达到日常备份的目的。假设需要每天备份整个 /opt 命令,可以创建一个带 tar 命令的 cron 任务来完成。如下:
```
[root@linuxtechi ~]# tar -zcvf optbackup-$(date +%Y-%m-%d).tgz /opt/
```
在创建 cron 任务时使用以上命令。
### 例示 15使用 -T 及 -X 创建压缩归档文件
想像这样一个场景:把想要归档和压缩的文件及目录记录到到一个文件,有时候还要排除这个文件记录中的特定路径后,把这个文件当做 tar 命令的传入参数来完成归档任务。
要达到这样的效果,在命令中使用 -T 选项跟记录了归档路径的文件、‘-X 选项跟排除特定路径的文件即可。
假设要归档 /etc、/opt、/home 目录,并排除 /etc/sysconfig/kdump/etc/sysconfig/foreman 文件,可以创建 /root/tar-include/root/tar-exclude 然后分别输入一下内容:
```
[root@linuxtechi ~]# cat /root/tar-include
/etc
/opt
/home
[root@linuxtechi ~]#
[root@linuxtechi ~]# cat /root/tar-exclude
/etc/sysconfig/kdump
/etc/sysconfig/foreman
[root@linuxtechi ~]#
```
运行一下命令来创建一个压缩归档文件。
```
[root@linuxtechi ~]# tar zcpvf mybackup-$(date +%Y-%m-%d).tgz -T /root/tar-include -X /root/tar-exclude
```
### 例示 16查看 .tar、.tgz 和 .tbz2 文件的大小
使用如下命令来查看 (压缩) tar 文件的体积。
```
[root@linuxtechi ~]# tar -czf - data.tar | wc -c
427
[root@linuxtechi ~]# tar -czf - mybackup-2016-09-09.tgz | wc -c
37956009
[root@linuxtechi ~]# tar -czf - myarchive.tbz2 | wc -c
30835317
[root@linuxtechi ~]#
```
### 例示 17分割体积庞大的 tar 文件为多分小文件
类 Unix 系统中使用 split 命令来将大体积文件分割成小体积文件。大体积的 tar 当然也可以使用这个命令来进行分割。
假设需要将 mybackup-2016-09-09.tgz 分割成每份 6 MB 的小文件。
```
Syntax : split -b <Size-in-MB> <tar-file-name>.<extension> “prefix-name”
```
```
[root@linuxtechi ~]# split -b 6M mybackup-2016-09-09.tgz mybackup-parts
```
以上命令会在当前目录分割 mybackup-2016-09-09.tgz 文件成为多个 6 MB 的小文件,文件名为 mybackup-partsaa … mybackup-partsag。如果在要在分割文件后以数字而非字母来区分可以在以上的 split 命令使用 -d 选项。
```
[root@linuxtechi ~]# ls -l mybackup-parts*
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaa
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsab
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsac
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsad
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsae
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaf
-rw-r--r--. 1 root root 637219 Sep 10 03:05 mybackup-partsag
[root@linuxtechi ~]#
```
然后通过网络将这些分割文件转移到其他服务器,就可以合并成为一个单独的 tar 文件了,如下:
```
[root@linuxtechi ~]# cat mybackup-partsa* > mybackup-2016-09-09.tgz
[root@linuxtechi ~]#
```
文毕,希望你喜欢 tar 命令的这几个不同的例示。随时分享你的评论并分享你的心得。
--------------------------------------------------------------------------------
via: http://www.linuxtechi.com/17-tar-command-examples-in-linux/
作者:[Pradeep Kumar ][a]
译者:[GHLandy](https://github.com/GHLandy)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: http://www.linuxtechi.com/author/pradeep/