linux 命令tar,Linux命令之tar详解

Linux命令之tar详解

tar是Linux的内置命令,所以这个工具在安装完linux系统就有了。他是用来管理压缩包,可以

生成压缩包

解开压缩包

查看压缩包的内容

下面就解释用tar来完成这3个功能,并且给出例子

tar命令的选项有:

-A, –catenate, –concatenate

append tar files to an archive

-c, –create

create a new archive

-d, –diff, –compare

find differences between archive and file system

–delete

delete from the archive (not on mag tapes!)

-r, –append

append files to the end of an archive

-t, –list

list the contents of an archive

–test-label

test the archive volume label and exit

-u, –update

only append files newer than copy in archive

-x, –extract, –get

extract files from an archive

-C, –directory=DIR

change to directory DIR

-f, –file=ARCHIVE

use archive file or device ARCHIVE

-j, –bzip2

filter the archive through bzip2

-J, –xz

filter the archive through xz

-p, –preserve-permissions

extract  information  about  file permissions (default for superuser)

-v, –verbose

verbosely list files processed

-z, –gzip

–check-device

check device numbers when creating incremental archives (default)

-g, –listed-incremental=FILE

handle new GNU-format incremental backup

-G, –incremental

handle old GNU-format incremental backup

–ignore-failed-read

do not exit with nonzero on unreadable files

–level=NUMBER

dump level for created listed-incremental archive

-n, –seek

archive is seekable

–no-check-device

do not check device numbers when creating incremental archives

–no-seek

archive is not seekable

–occurrence[=NUMBER]

process  only  the NUMBERth occurrence of each file in the archive; this option is valid only in conjunction with one of the subcommands –delete, –diff, –extract or –list and when a list of files

is given either on the command line or via the -T option; NUMBER defaults to 1

–sparse-version=MAJOR[.MINOR]

set version of the sparse format to use (implies –sparse)

-S, –sparse

handle spars

Overwrite control:

-k, –keep-old-files

don’t replace existing files when extracting, treat them as errors

–skip-old-files

don’t replace existing files when extracting, silently skip over them

–keep-newer-files

don’t replace existing files that are newer than their archive copies

–no-overwrite-dir

preserve metadata of existing directories

–overwrite

–overwrite-dir

overwrite metadata of existing directories when extracting (default)

–recursive-unlink

empty hierarchies prior to extracting directory

–remove-files

remove files after adding them to the archive

-U, –unlink-first

remove each file prior to extracting over it

-W, –verify

attempt to verify the archive after writing it

Select output stream:

–ignore-command-error ignore exit codes of children

–no-ignore-command-error

treat non-zero exit codes of children as error

-O, –to-stdout

extract files to standard output

–to-command=COMMAND

pipe extracted files to another program

Handling of file attributes:

–acls Save the ACLs to the archive

–atime-preserve[=METHOD]

preserve access times on dumped files, either by restoring the times after reading (METHOD=’replace’; default) or by not setting the times in the first place (METHOD=’system’)

–delay-directory-restore

delay setting modification times and permissions of extracted directories until the end of extraction

–group=NAME

force NAME as group for added files

–mode=CHANGES

force (symbolic) mode CHANGES for added files

–mtime=DATE-OR-FILE

set mtime for added files from DATE-OR-FILE

-m, –touch

don’t extract file modified time

e files efficiently

Overwrite control:

-k, –keep-old-files

don’t replace existing files when extracting, treat them as errors

–skip-old-files

don’t replace existing files when extracting, silently skip over them

–keep-newer-files

don’t replace existing files that are newer than their archive copies

–no-overwrite-dir

preserve metadata of existing directories

–overwrite

–overwrite-dir

overwrite metadata of existing directories when extracting (default)

–recursive-unlink

empty hierarchies prior to extracting directory

–remove-files

remove files after adding them to the archive

-U, –unlink-first

remove each file prior to extracting over it

-W, –verify

attempt to verify the archive after writing it

Select output stream:

–ignore-command-error ignore exit codes of children

–no-ignore-command-error

treat non-zero exit codes of children as error

-O, –to-stdout

extract files to standard output

–to-command=COMMAND

pipe extracted files to another program

Handling of file attributes:

–acls Save the ACLs to the archive

–atime-preserve[=METHOD]

preserve access times on dumped files, either by restoring the times after reading (METHOD=’replace’; default) or by not setting the times in the first place (METHOD=’system’)

–delay-directory-restore

delay setting modification times and permissions of extracted directories until the end of extraction

–group=NAME

force NAME as group for added files

–mode=CHANGES

force (symbolic) mode CHANGES for added files

–mtime=DATE-OR-FILE

set mtime for added files from DATE-OR-FILE

-m, –touch

don’t extract file modified time

我们重点说几个

-c 是创建压缩包

-x 是解压压缩包

-v 是给出详细信息

-f 表示是压缩文档, 这个选项是上面的列出的3个功能必须加的。

-j 表示要处理的是bzip2

-g 表示要处理的是gzip

-r 在压缩文档添加内容

-d 用来比较2个压缩文档

创建压缩文档

c和f选项是必须的,v可以没有,v是用来列出压缩文档添加的内容

如果加j表示bzip2格式

tar -cvf xxx.tar.gz file1 file2 dir1 dir2

tar -cvfj xxx.tar.bz2 file1 file2 dir1 dir2

解压缩文档

x和f是必须的

tar -xvf xxx.tar.gz

tar -jxvf xxx.tar.bz2

列出压缩文档内容

t和f是必须的

tar -tvf xxx.tar.gz

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

分享到:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你解答关于Linux tar命令的问题。 tar命令Linux中一个常用的归档命令,它的功能是将多个文件或目录打包成一个文件,并且可以对打包后的文件进行压缩,常用的扩展名为.tar.gz或.tar.bz2。 下面是tar命令的一些常用选项和用法: 1. 打包文件:tar -cvf archive.tar file1 file2 file3 ... 选项: -c:创建一个新的归档文件。 -v:显示打包的文件列表和详细信息。 -f:指定打包后的文件名。 例如: tar -cvf myfiles.tar file1.txt file2.txt folder1/ 这个命令将文件file1.txt、file2.txt和目录folder1打包成一个名为myfiles.tar的文件。 2. 解压文件:tar -xvf archive.tar 选项: -x:解包已存在的归档文件。 -v:显示解包的文件列表和详细信息。 -f:指定需要解包的文件名。 例如: tar -xvf myfiles.tar 这个命令将文件myfiles.tar解压缩,并且将打包的文件和目录还原到原来的位置。 3. 压缩文件:tar -czvf archive.tar.gz file1 file2 file3 ... 选项: -c:创建一个新的归档文件。 -z:使用gzip压缩算法。 -v:显示打包的文件列表和详细信息。 -f:指定打包后的文件名。 例如: tar -czvf myfiles.tar.gz file1.txt file2.txt folder1/ 这个命令将文件file1.txt、file2.txt和目录folder1打包并压缩成一个名为myfiles.tar.gz的文件。 4. 解压缩文件:tar -xzvf archive.tar.gz 选项: -x:解包已存在的归档文件。 -z:使用gzip压缩算法。 -v:显示解包的文件列表和详细信息。 -f:指定需要解包的文件名。 例如: tar -xzvf myfiles.tar.gz 这个命令将文件myfiles.tar.gz解压缩,并且将打包的文件和目录还原到原来的位置。 以上是tar命令的一些常用选项和用法,希望能够对你有所帮助。如果有其他问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值