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