为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

Tar命令基本指南

2012-02-03 10页 pdf 105KB 31阅读

用户头像

is_520942

暂无简介

举报
Tar命令基本指南 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 The Ultimate Tar Command Tutorial with 10 Practical The Ultimate Tar Command Tutorial with 10 Practical The Ultimate Tar Command Tutorial with 10 Practical The Ultimate Tar Command Tutorial with 10 Practical Examp...
Tar命令基本指南
默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 The Ultimate Tar Command Tutorial with 10 Practical The Ultimate Tar Command Tutorial with 10 Practical The Ultimate Tar Command Tutorial with 10 Practical The Ultimate Tar Command Tutorial with 10 Practical ExamplesExamplesExamplesExamples TarTarTarTar 命令命令命令命令基本基本基本基本指南指南指南((((附带附带附带附带 10101010 个实用的例子个实用的例子个实用的例子个实用的例子)))) by SathiyaMoorthy on April 26, 2010 ShareThis 由 SathiyaMoorthy 于 2010 年 4 月 26 日分享此文 On Unix platform, tar commandtar commandtar commandtar command is the primary archiving utility. Understanding various tar command options will help you master the archive file manipulation. 在 UNIX 平台中,tar 命令是主要的归档工具。对 tar 命令的一些选项的理解有 助于你掌握文件归档的操作方法。 In this article, let us review various tar examplestar examplestar examplestar examples including how to create tar archives (with gzip and bzip compression), extract a single file or directory, view tar archive contents, validate the integrity of tar archives, finding out the difference between tar archive and file system, estimate the size of the tar archives before creating it etc., 在本文中,让我们复习若干 tar 实例,这些实例包括如何创建 tar 归档文件(使 用 gzip 和 bzip 压缩)、解压单一文件或目录、查看 tar 归档文件的内容、检验 tar 归档文件的完整性、查找归档文件和文件系统的区别以及在创建压缩文件之 前估算 tar 归档文件的大小等等。 1.1.1.1. Creating an archive using tar commandCreating an archive using tar commandCreating an archive using tar commandCreating an archive using tar command 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 1.1.1.1.使用使用使用使用 tartartartar 命令创建命令创建命令创建命令创建 tartartartar 归档文件归档文件归档文件归档文件 Creating an uncompressed tar archive using option cvfCreating an uncompressed tar archive using option cvfCreating an uncompressed tar archive using option cvfCreating an uncompressed tar archive using option cvf 使用选项使用选项使用选项使用选项 cvfcvfcvfcvf 创建一个未压缩的创建一个未压缩的创建一个未压缩的创建一个未压缩的 tartartartar 归档文件归档文件归档文件归档文件 This is the basic command to create a tar archive. 这是创建一个 tar 归档文件的基本命令。 $ tar cvf archive_name.tar dirname/ In the above command: 在上述命令中: • c – create a new archive 创建一个新归档文件 • v – verbosely list files which are processed.列举出被处理的冗长 文件 • f – following is the archive file name 后面紧跟归档文件名 Creating a tar gzipped arcCreating a tar gzipped arcCreating a tar gzipped arcCreating a tar gzipped archive using option cvzfhive using option cvzfhive using option cvzfhive using option cvzf 使用选项使用选项使用选项使用选项 cvzfcvzfcvzfcvzf 创建一个压缩创建一个压缩创建一个压缩创建一个压缩的的的的 gzip targzip targzip targzip tar 归档文件归档文件归档文件归档文件 The above tar cvf option, does not provide any compression. To use a gzip compression on the tar archive, use the z option as shown below. 上述的 tar cvf 选项不能提供任何压缩功能。为了运用 gzip 压缩 tar 归档文件, 使用如下所示的 z 选项: $ tar cvzf archive_name.tar.gz dirname/ • z – filter the archive through gzip 凭借 gzip 命令筛选归档文件 Note:Note:Note:Note: .tgz is same as .tar.gz 提示:.taz 等同于.tar.gz。 Creating a bzipped tar archive using option cvjfCreating a bzipped tar archive using option cvjfCreating a bzipped tar archive using option cvjfCreating a bzipped tar archive using option cvjf 使用使用使用使用 cvjfcvjfcvjfcvjf 选项创建一个压缩的选项创建一个压缩的选项创建一个压缩的选项创建一个压缩的 bzip tarbzip tarbzip tarbzip tar 归档文件归档文件归档文件归档文件 Create a bzip2 tar archive as shown below: 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 创建一个 bzip2 tar 归档文件,如下所示: $ tar cvjf archive_name.tar.bz2 dirname/ • j – filter the archive through bzip2 凭借 bzip2 命令筛选归档文件 gzip vs bzip2gzip vs bzip2gzip vs bzip2gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip. gzip 对比 bzip2:bzip2 压缩和解压比 gzip 需要更多的时间;而 bzip2 归档文 件的大小比 gzip 归档文件小。 Note:Note:Note:Note: .tbz and .tb2 is same as .tar.bz2 提示:.tbz 和.tb2 等同于.tar.bz2 Note:Note:Note:Note: I like to keep the ‘cvf’ option unchanged for all archive creation, and add additional option at the end, which is easier to remember. i.e cvf for archive creation, cvzf for compressed gzip archive creation, cvjf for compressed bzip2 archive creation etc., 提示:对于创建所有的归档文件,我喜欢保持“cvf”选项不变,以及在后面添 加容易记忆的额外选项。例如,使用 cvf 选项创建归档文件,cvzf 创建压缩的 gzip 归档文件,cvjf 选项创建压缩的 bzip2 归档文件等等。 2.2.2.2. Extracting (untar) an archive using tar commandExtracting (untar) an archive using tar commandExtracting (untar) an archive using tar commandExtracting (untar) an archive using tar command 2.2.2.2.使用使用使用使用 ttttarararar 命令解压缩命令解压缩命令解压缩命令解压缩((((untaruntaruntaruntar))))一个归档文件一个归档文件一个归档文件一个归档文件 Extract a *.tar file using option xvfExtract a *.tar file using option xvfExtract a *.tar file using option xvfExtract a *.tar file using option xvf 使用选项使用选项使用选项使用选项 xvfxvfxvfxvf 解压一个解压一个解压一个解压一个*.tar*.tar*.tar*.tar 文件文件文件文件 Extract a tar file using option x as shown below: 使用选项 x 解压一个 tar 文件,如下所示: $ tar xvf archive_name.tar • x – extract files from archive 从归档文件中解压出文件 Extract a gzipped tar archive ( *.tarExtract a gzipped tar archive ( *.tarExtract a gzipped tar archive ( *.tarExtract a gzipped tar archive ( *.tar.gz ) using option xvzf.gz ) using option xvzf.gz ) using option xvzf.gz ) using option xvzf 使用选项使用选项使用选项使用选项 xvzfxvzfxvzfxvzf 解压一个压缩的解压一个压缩的解压一个压缩的解压一个压缩的 gzip targzip targzip targzip tar 归档文件归档文件归档文件归档文件 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 Use the option z for uncompressing a gzip tar archive. 使用选项 z 解压一个压缩的 gzip tar 归档文件。 $ tar xvzf archive_name.tar.gz Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjfExtracting a bzipped tar archive ( *.tar.bz2 ) using option xvjfExtracting a bzipped tar archive ( *.tar.bz2 ) using option xvjfExtracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf 使用选项使用选项使用选项使用选项 xvjfxvjfxvjfxvjf 解压一个压缩的解压一个压缩的解压一个压缩的解压一个压缩的 bzip tarbzip tarbzip tarbzip tar 归档文件归档文件归档文件归档文件((((*.tar.bz2*.tar.bz2*.tar.bz2*.tar.bz2)))) Use the option j for uncompressing a bzip2 tar archive. 使用选项 j 解压一个压缩的 bzip2 tar 归档文件。 $ tar xvjf archive_name.tar.bz2 Note:Note:Note:Note: In all the above commands v is optional, which lists the file being processed. 提示:在上述的命令中,v 是一个选项,用来列举被处理的文件。 3.3.3.3. Listing an arcListing an arcListing an arcListing an archive using tar commandhive using tar commandhive using tar commandhive using tar command 3.3.3.3.使用使用使用使用 tartartartar 命令列举一个归档文件命令列举一个归档文件命令列举一个归档文件命令列举一个归档文件 View the tar archive file content without extracting using option tvfView the tar archive file content without extracting using option tvfView the tar archive file content without extracting using option tvfView the tar archive file content without extracting using option tvf 使用选项使用选项使用选项使用选项 tvftvftvftvf 查看一个查看一个查看一个查看一个 tartartartar 归档文件的内容而无需解压文件归档文件的内容而无需解压文件归档文件的内容而无需解压文件归档文件的内容而无需解压文件 You can view the *.tar file content before extracting as shown below. 在解压之前,你可以查看*.tar 文件的内容,如下所示: $ tar tvf archive_name.tar View the *.tar.gz file content without extracting using option tvzfView the *.tar.gz file content without extracting using option tvzfView the *.tar.gz file content without extracting using option tvzfView the *.tar.gz file content without extracting using option tvzf 使用选项使用选项使用选项使用选项 tvzftvzftvzftvzf 查看查看查看查看*.tar.gz*.tar.gz*.tar.gz*.tar.gz 文件的内容而无需解压文件文件的内容而无需解压文件文件的内容而无需解压文件文件的内容而无需解压文件 You can view the *.tar.gz file content before extracting as shown below. 在解压之前,你可以查看*.tar.gz 文件的内容,如下所示: $ tar tvzf archive_name.tar.gz 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 View the *.taView the *.taView the *.taView the *.tar.bz2 file content without extracting using option tvjfr.bz2 file content without extracting using option tvjfr.bz2 file content without extracting using option tvjfr.bz2 file content without extracting using option tvjf 使用选项使用选项使用选项使用选项 tvjftvjftvjftvjf 查看查看查看查看*.tar.*.tar.*.tar.*.tar.bbbbzzzz2222 文件的内容而无需解压文件文件的内容而无需解压文件文件的内容而无需解压文件文件的内容而无需解压文件 You can view the *.tar.bz2 file content before extracting as shown below. 在解压之前,你可以查看*.tar.bz2 文件的内容,如下所示: $ tar tvjf archive_name.tar.bz2 4.4.4.4. Listing out the tar file conteListing out the tar file conteListing out the tar file conteListing out the tar file content with less commandnt with less commandnt with less commandnt with less command 4.4.4.4.使用使用使用使用 lesslesslessless 命令来列举出命令来列举出命令来列举出命令来列举出 tartartartar 文件的内容文件的内容文件的内容文件的内容 When the number of files in an archive is more, you may pipe the output of tar to less. But, you can also use less command directly to view the tar archive output, as explained in one of our previous article Open & View 10 Different File Types with Linux Less Command — The Ultimate Power of Less. 当 tar 归档文件中的文件数目比较多时,你需要使用管道将 tar 文件的输出重定 向到 less 命令中。然而,你也可以使用 less 命令直接查看 tar 归档文件的输出, 如我们前面的一篇文章《使用 Less 命令打开和查看 10 种不同的文件类型—Less 基本功能》解释的。 5.5.5.5. Extract a single file from tar, tar.gz, tar.bz2 fileExtract a single file from tar, tar.gz, tar.bz2 fileExtract a single file from tar, tar.gz, tar.bz2 fileExtract a single file from tar, tar.gz, tar.bz2 file 5.5.5.5.从从从从 tartartartar、、、、tar.gztar.gztar.gztar.gz 和和和和 tar.bz2tar.bz2tar.bz2tar.bz2 文件中解压出一个单独的文件文件中解压出一个单独的文件文件中解压出一个单独的文件文件中解压出一个单独的文件 To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file. 为了从一个 tar 归档文件中解压出一个特殊的文件,在 tar xvf 命令末尾使用特 殊的文件名,如下所示。下面的命令仅仅只能从一个大的 tar 文件中解压出一个 特殊的文件。 $ tar -xvf archive_file.tar /path/to/file Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below. 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 根据压缩方法 gzip 或 bzip2 分别使用相关的选项 z 或 j,如下所示: $ tar -xvzf archive_file.tar.gz /path/to/file $ tar -xvjf archive_file.tar.bz2 /path/to/file 6.6.6.6. Extract a single directory from tar, tar.gz, tar.bz2 filExtract a single directory from tar, tar.gz, tar.bz2 filExtract a single directory from tar, tar.gz, tar.bz2 filExtract a single directory from tar, tar.gz, tar.bz2 fileeee 6.6.6.6.从从从从 tartartartar、、、、tar.gztar.gztar.gztar.gz 和和和和 tar.bz2tar.bz2tar.bz2tar.bz2 文件中解压出单一的目录文件中解压出单一的目录文件中解压出单一的目录文件中解压出单一的目录 To extract a single directory (along with it’s subdirectory and files) from a tar archive, specify the directory name at the end of the tar xvf command as shown below. The following extracts only a specific directory from a large tar file. 从一个 tar 归档文件中解压出一个单独的目录(和其子目录和文件一起),只需 在 tar xvf 命令的末尾使用特殊的目录名。下面的例子仅仅从一个大的 tar 文件 中解压出一个特殊的目录。 $ tar -xvf archive_file.tar /path/to/dir/ To extract multiple directories from a tar archive, specify those individual directory names at the end of the tar xvf command as shown below. 为了从一个 tar 归档文件中解压出多个目录,只需在 tar xvf 命令的末尾使用特 殊的目录名,如下所示: $ tar -xvf archive_file.tar /path/to/dir1/ /path/to/dir2/ Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below. 根据压缩方法 gzip 或 bzip2 分别使用相关的选项 z 或 j,如下所示: $ tar -xvzf archive_file.tar.gz /path/to/dir/ $ tar -xvjf archive_file.tar.bz2 /path/to/dir/ 7.7.7.7. Extract group of files from tar, tar.gz, tar.bz2 archives Extract group of files from tar, tar.gz, tar.bz2 archives Extract group of files from tar, tar.gz, tar.bz2 archives Extract group of files from tar, tar.gz, tar.bz2 archives using regular expressionusing regular expressionusing regular expressionusing regular expression 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 7.7.7.7.使用正则表达式从使用正则表达式从使用正则表达式从使用正则表达式从 tartartartar、、、、tar.gztar.gztar.gztar.gz 和和和和 tar.bz2tar.bz2tar.bz2tar.bz2 归档文件中解压出一类文归档文件中解压出一类文归档文件中解压出一类文归档文件中解压出一类文 件件件件 You can specify a regex, to extract files matching a specified pattern. For example, following tar command extracts all the files with pl extension. 你可以指定一个正则表达式,使用指定的模式匹配解压文件。例如,使用如下所 示的 tar 命令解压出所有扩展名为 pl 的文件: $ tar -xvf archive_file.tar --wildcards '*.pl' Options explanation: 选项解释: • –wildcards *.pl – files with pl extension • -通配符*.pl – 扩展名为 pl 的所有文件 8.8.8.8. Adding a file or directory to an existing archive using option Adding a file or directory to an existing archive using option Adding a file or directory to an existing archive using option Adding a file or directory to an existing archive using option ––––rrrr 8.8.8.8.使用选项使用选项使用选项使用选项----rrrr 将一个文件或目录添加到已存在的归档文件中将一个文件或目录添加到已存在的归档文件中将一个文件或目录添加到已存在的归档文件中将一个文件或目录添加到已存在的归档文件中 You can add additional files to an existing tar archive as shown below. For example, to append a file to *.tar file do the following: 你可以给一个已存在的 tar 归档文件添加额外的文件。例如,向*.tar 文件中增 加一个文件,如下所示: $ tar rvf archive_name.tar newfile This newfile will be added to the existing archive_name.tar. Adding a directory to the tar is also similar, 向一个已存在的 archive_name.tar 文件中添加一个新文件,同理向一个 tar 文 件添加一个目录也类似。 $ tar rvf archive_name.tar newdir/ 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 Note:Note:Note:Note: You cannot add file or directory to a compressed archive. If you try to do so, you will get “tar: Cannot update compressed archives” error as shown below. 提示:不能向一个已压缩文件中添加文件或目录。如果你尝试这样操作,将得到 如下所示的警告“tar: Cannot update compressed archives”: $ tar -rvzf archive_name.tgz newfile tar: Cannot update compressed archives Try `tar --help' or `tar --usage' for more information. 9.9.9.9. Verify files available in tar using option Verify files available in tar using option Verify files available in tar using option Verify files available in tar using option ––––WWWW 9.9.9.9.使使使使用选项用选项用选项用选项----WWWW 检验检验检验检验 tartartartar 归档文件中的可用文件归档文件中的可用文件归档文件中的可用文件归档文件中的可用文件 As part of creating a tar file, you can verify the archive file that got created using the option W as shown below. 作为创建一个 tar 文件的一部分,你可以使用选项 W 来检验一个已创建的归档文 件,如下所示: $ tar cvfW file_name.tar dir/ If you are planning to remove a directory/file from an archive file or from the file system, you might want to verify the archive file before doing it as shown below. 如果你打算从一个归档文件或文件系统中移除一个目录或文件,在进行下面的操 作前,你可能想检验归档文件。 $ tar tvfW file_name.tar Verify 1/file1 1/file1: Mod time differs 1/file1: Size differs Verify 1/file2 Verify 1/file3 If an output line starts with Verify, and there is no differs line then the file/directory is Ok. If not, you should investigate the issue. 如果输出行以 Verify 开头,且没有 differs 行,那么文件或目录合格;否则, 你应该调查问。 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 Note:Note:Note:Note: for a compressed archive file ( *.tar.gz, *.tar.bz2 ) you cannot do the verification. 提示:对于压缩归档文件(比如*.tar.gz、*.tar.bz2),不能进行检验。 Finding the difference between an archive and file system can be done even for a compressed archive. It also shows the same output as above excluding the lines with Verify. 即使对于一个压缩的归档文件,也可以查找归档文件和文件系统之间的区别。它 也会显示上述包含的 Verify 行相同的输出信息。 Finding the difference between gzip archive file and file system 查找 gzip 归档文件和文件系统之间的区别 $ tar dfz file_name.tgz Finding the difference between bzip2 archive file and file system 查找 bzip2 归档文件和文件系统之间的区别 $ tar dfj file_name.tar.bz2 10.10.10.10. Estimate the tar archive sizeEstimate the tar archive sizeEstimate the tar archive sizeEstimate the tar archive size 10.10.10.10.估算估算估算估算 tartartartar 归档文件的大小归档文件的大小归档文件的大小归档文件的大小 The following command, estimates the tar file size ( in KB ) before you create the tar file. 以下命令,可以在创建 tar 文件之前估算 tar 文件的大小(用 KB 表示)。 $ tar -cf - /directory/to/archive/ | wc -c 20480 The following command, estimates the compressed tar file size ( in KB ) before you create the tar.gz, tar.bz2 files. 以下命令,可以在创建 tar.gz、tar.bz2 文件之前估算压缩的 tar 文件大小(用 KB 表示)。 $ tar -czf - /directory/to/archive/ | wc -c 508 默之默之默之默之筆記筆記筆記筆記之之之之 Shell筆記譯筆記譯筆記譯筆記譯文文文文 $ tar -cjf - /directory/to/archive/ | wc -c 428 默之譯 庚寅三月十五 2010-4-28
/
本文档为【Tar命令基本指南】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索