tar 的–exclude参数,实现不包括某些文件

最近有人问我tar压缩能不能不包含不想要的文件或者文件夹
压缩包很利于传送,备份等等。但比如大量的日志文件一般是不需要备份,或者需要分开备份。
tar参数中的–exclude,这个是我在看鸟哥私房菜时看到的。当时觉得肯定有用,就多留意了几眼,但使用的时候发现不是那么回事,所以后来就多研究了一下。没想到现在对于备份来说很舒服~~~得心应手,如果要包含其他路径的文件需要使用–include
例子:
在我的test文件夹下面,包含一些symbian的sisx安装包及andoid包apk安装包等等
test的大致目录结构:

[root@**** html]# tree -L 2 test
test
|-- AndroidPad_v1.0.0_20111116_online_unsigned_1.apk
::
|-- KingGoo.Com.sh
|-- PACK_NAME.apk
|-- android.php
|-- androidpad.php
|-- bak
|   `-- Vancl_2.0.0.0000_14_20120117_online_unsigned_versionCode_20.apk
|-- debug
|-- head.php
|-- index.php
|-- index1.php
|-- lingxin
|-- make.php
|-- make.sh
|-- make.sh.bk
|-- package.jar
|-- s60V3_1.2.1_2011-08-22-1755_online_signed.sis
::
|-- source_id.txt
|-- symbian.php
|-- temp
|   |-- AndroidPhone_v1.3.2_demo_unsign_versionCode7.apk_android
|   |-- KingGoo.Com_Err.log
|   |-- android
|   |-- androidpad
|   |-- make.log
|   |-- symbian
|   `-- temp
`-- test.php

8 directories, 86 files

我不想要*.sis、*.sisx、*.apk则:

[root@**** html]# tar -czvf  test.tar.gz  test --exclude=test/*.sisx  --exclude=test/*.apk --exclude=test/temp/* --exclude=test/*.sis
test/
test/temp/
test/make.sh
test/package.jar
test/android.php
test/head.php
test/test.php
test/make.sh.bk
test/make.php
test/source_id.txt
test/index.php
test/index.php.bkkkk
test/KingGoo.Com.sh
test/bak/
test/index.php.bk
test/symbian.php
test/debug/
test/index.php.bkk
test/index1.php
test/index.php.bkkk
test/androidpad.php
test/lingxa

查看一下,压缩包内的文件(非解压)

[root@**** html]# tar  -tf  test.tar.gz 
test/
test/temp/
test/make.sh
test/package.jar
test/android.php
test/head.php
test/test.php
test/make.sh.bk
test/make.php
test/source_id.txt
test/index.php
test/index.php.bkkkk
test/KingGoo.Com.sh
test/bak/
test/index.php.bk
test/symbian.php
test/debug/
test/index.php.bkk
test/index1.php
test/index.php.bkkk
test/androidpad.php
test/lingxa
- THE END -
版权声明:
转载原创文章请注明,文章出处://kinggoo.com
原文地址:https://kinggoo.com/linux-tarexlude.htm
发表评论?

4 条评论。

  1. 解压单个文件

    tar -tf一下 查看你要解压的那个文件
    如:我要将这个压缩文件内的run-ldap-tests解压出来
    [root@localhost src]# tar -tf mod_ldap-2.9.3.tar.gz
    mod_ldap-2.9.3/
    mod_ldap-2.9.3/mod_ldap.c
    mod_ldap-2.9.3/run-ldap-tests

    然后tar -zxvf 压缩包 要解压出来的文件绝对路径
    (要知道你要想被解压出来的文件路径 如:mod_ldap-2.9.3/mod_ldap.c)
    [root@localhost src]# tar -zxvf mod_ldap-2.9.3.tar.gz mod_ldap-2.9.3/mod_ldap.c
    mod_ldap-2.9.3/mod_ldap.c
    [root@localhost src]# ls mod_ldap-2.9.3
    mod_ldap.c

  2. 描述:

    tar命令用于从一个磁带或磁盘归档中转存和加压的工具。

    tar的第一个参数应该是一个函数。为下面的一种:Acdrtux,或为长整型的选项。

    一个函数的前面可以加上也可以不加上’-‘字符。而一个长的选项必须加上’–‘字符。

    一些选项后面有带参数的话需要单独使用一个分隔符。对与带参数的长选项需要使用

    =Value来指定值。

    Function字符:

    -A,–catenate,–concatenate:将指定的归档文件与原归档文件合并。(注:必须

    为.tar的两个归档文件)

    -c,–create:创建新的归档文件。

    -d,–diff,–compare: 比较归档文件与文件的不同之处。

    –delete:从归档文件中删除指定的文件。(必须为.tar的文件)

    -r,–append:将指定的文件追加到归档文件的尾部。(必须为.tar的文件)

    -t,–list :显示归档文件的内容。

    -u,–update 只追加比归档文件中的文件更新的文件。(必须为.tar的文件)

    -x,–extract,–get:从归档文件中解压出文件。

    Other options:

    operation modifiers:

    -[0-7][lmh] :选择驱动和信息密度。

    -?,–help:显示帮助信息并退出。

    -a,–auto-compress:根据归档文件的后缀自动选择压缩工具。

    –add-file=File:将指定的文件加到归档文件。

    -C,–directory Dir:将工作目录转为Dir.

    –backup:移除前先备份。

    –no-auto-compress:不根据归档文件的后缀自动使用compress命令压缩。

    –atime-preserve:在转存文件时保留访问时间。

    –anchored:从文件名开始匹配。

    –no-anchored:从’/’后匹配。

    -b,–blocking-factor Blocks:每条记录为Blocks*512大小。

    –check-device:当创建一个递增的归档文件时检查设备号。

    –no-check-device:当创建一个递增的归档文件时不检查设备号。

    –checkpoint:显示每第Number(默认为10)条记录的处理信息。

    –delay-directory-restore:延迟设置修改时间和允许解压。

    –no-delay-directory-restore:取消–delay-directory-restore设置。

    –exclude=Pattern:不包括满足Pattern的文件。

    –exclude-caches:不包括包含CACHEDIR.TAG的目录。

    –exclude-tag=File:不包括含指定文件File的目录的所有内容。但包含

    File文件。

    –exclude-tag-all=File:不包括含指定文件File的目录中的所有内容。

    –exclude-tag-under=File:不包括含指定文件的目录下所有的内容。

    -f,–file Archive:指定归档文件的文件名或设备归档文件.

    –force-local:即使归档文件名中含:也强制认为归档文件为本地归档文件。

    –group=Name:强制认为Name为增加的文件组。

    -h,–dereference:进入符号连接目录中。

    –HANG:挂起SECS秒(默认为3600s)

    –ignore-zeros,-i:忽略归档文件中的0块的文件。

    -I,–use-compress-program Prog:指定压缩程序。

    –ignore-case:忽略大小写。

    –no-ignore-case:不忽略大小写(此为默认)

    –ignore-command-error:忽略命令行错误。

    –no-ignore-command-error:不忽略命令行错误。

    –ignore-failed-read:忽略读取失败错误。

    –index-file=File:将详细输出到指定文件中。

    -j,–bzip2:使用bzip2程序压缩。

    -J,–xz:使用xz压缩。

    -z,–gzip:使用gzip压缩。

    -Z,–compress,–uncompress:使用compress压缩。

    -k,–keep-old-files:当解压时若目标文件已存在则不替换它。

    –keep-newer-files:不替换已存在的目标文件比解压时归档文件里

    更新的文件。

    -l,–check-links:若不是所有链接文件都被转存则打印一条信息。

    -L,–tap-length Number:在归档文件写入了Number*1024b时更换磁带。

    –lzma: 使用lzma压缩。

    –lzop:使用lzop压缩。

    -M,–multi-volume:创建多磁盘的归档文件。

    -m:不解压文件的修改时间。

    –mode=Changes:强制改变追加的文件的模式为Change

    –numeric-owner:总是使用用户或组的ID号。

    -O,–to-stdout:将解压文件输出到标准输出上。

    –overwrite:当解压时若目标文件已存在则覆盖。

    –overwrite-dir:当解压时若目标目录已存在则覆盖。

    –no-overwrite-dir:不覆盖目录。

    –owner=Name:强制将Name用户的文件追加。

    -p,–preserve-permissions,–same-permissions

    保留文件的权限(仅管理员使用)

    -s,–preserve-order,–same-order

    保留文件的顺序。

    –preserve:等价与 -p和-s

    –recursive:递归处理目录及其子目录的内容(此为默认)

    –no-recursive: 不递归处理。

    –same-owner:试着解压属于同一用户的文件。

    –no-same-owner:只解压你自己的文件。

    -T,–files-from File:从指定的文件File中读取解压和创建的文件名。

    –to-command=Command:将解压的文件通过管道传给指定的命令Command

    –totals:在执行完归档文件后打印总字节数。

    –transform,–xform Expression:

    使用Expression来转换文件名。

    -w,–interactive,–confirmation:作每个动作都会询问你。

    -W,–verify:在写归档文件后试图核实归档文件。

    –wildcard:使用通配符,此为默认。

    –no-wildcard:不使用通配符。

    -X,–exclude-from File:从指定文件File中读取不包括的文件。

    使用权限:

    所有者

    实例:

    tar -cf example.tar example #创建归档文件example.tar

    tar -czf example.tar.gz example #创建并使用gzip压缩归档文件example.tar.gz

    tar -xzf example.tar.gz -C dir#解压归档文件的内容到dir目录中

    tar -Af 1.tar 2.tar#将2.tar合并到1.tar中

    tar czf example.tgz example –exclude=example1 –exclude=example2 #备份example目录下除了example1和example2外所有的文件

  3. 不错,我要转载一下。

回复给 Tscccn ¬
取消回复


此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Trackbacks and Pingbacks: