tar #不负责压缩,只负责归档打包
选项:
z #使用gzip格式进行压缩
j # 使用bzip2格式进行压缩
J #使用xz格式进行压缩
c #创建压缩包
v #显示过程
f #指定压缩包的名称
czf #打包压缩 tar.gz格式的压缩包
cjf #打包压缩 tar.bz2格式的压缩包
cJf #打包压缩 tar.xz格式的压缩包
t #查看压缩中的内容
tf #指定要查看的压缩包名称
x #解压
xf #解压指定的压缩包
-C #解压到指定的目录
h #打包软连接的真实路径
P #使用绝对路径打包
X #指定排除文件的列表文件
--exclude= #指定排除的文件
--exclude-from= #指定排除文件的列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
[root@qls ~]# ll total 656 -rw-r--r-- 1 root root 670293 Jul 29 09:47 services [root@qls ~]# tar czf services.tar.gz services [root@qls ~]# ll total 792 -rw-r--r-- 1 root root 670293 Jul 29 09:47 services -rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz [root@qls ~]# tar cjf services.tar.bz2 services [root@qls ~]# ll total 916 -rw-r--r-- 1 root root 670293 Jul 29 09:47 services -rw-r--r-- 1 root root 124166 Jul 29 09:48 services.tar.bz2 -rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz [root@qls ~]# tar cJf services.tar.xz services [root@qls ~]# ll total 1016 -rw-r--r-- 1 root root 670293 Jul 29 09:47 services -rw-r--r-- 1 root root 124166 Jul 29 09:48 services.tar.bz2 -rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz -rw-r--r-- 1 root root 100640 Jul 29 09:49 services.tar.xz [root@qls ~]# tar czf etc.tar.gz /etc tar: Removing leading `/' from member names #删根的操作是正常的 [root@qls ~]# ll total 10952 -rw-r--r-- 1 root root 10171684 Jul 29 09:56 etc.tar.gz [root@qls ~]# tar tf services.tar.gz services [root@qls ~]# tar xf services.tar.gz [root@qls ~]# ll total 10952 -rw-r--r-- 1 root root 10171684 Jul 29 09:56 etc.tar.gz -rw-r--r-- 1 root root 670293 Jul 29 09:47 services -rw-r--r-- 1 root root 124166 Jul 29 09:48 services.tar.bz2 -rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz -rw-r--r-- 1 root root 100640 Jul 29 09:49 services.tar.xz [root@qls ~]# tar xf etc.tar.gz [root@qls ~]# ll total 10964 drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 10171684 Jul 29 09:56 etc.tar.gz -rw-r--r-- 1 root root 670293 Jul 29 09:47 services -rw-r--r-- 1 root root 124166 Jul 29 09:48 services.tar.bz2 -rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz -rw-r--r-- 1 root root 100640 Jul 29 09:49 services.tar.xz [root@qls ~]# tar cjf etc.tar.bz2 /etc tar: Removing leading `/' from member names [root@qls ~]# tar cJf etc.tar.xz /etc tar: Removing leading `/' from member names [root@qls ~]# ll total 26808 -rw-r--r-- 1 root root 8994047 Jul 29 10:01 etc.tar.bz2 -rw-r--r-- 1 root root 10171684 Jul 29 09:56 etc.tar.gz -rw-r--r-- 1 root root 7238084 Jul 29 10:01 etc.tar.xz -rw-r--r-- 1 root root 670293 Jul 29 09:47 services -rw-r--r-- 1 root root 124166 Jul 29 09:48 services.tar.bz2 -rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz -rw-r--r-- 1 root root 100640 Jul 29 09:49 services.tar.xz [root@qls ~]# tar xf etc.tar.bz2 #解压的时候,自动识别压缩包的格式 [root@qls ~]# ll total 26820 drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 8994047 Jul 29 10:01 etc.tar.bz2 -rw-r--r-- 1 root root 10171684 Jul 29 09:56 etc.tar.gz -rw-r--r-- 1 root root 7238084 Jul 29 10:01 etc.tar.xz [root@qls ~]# tar xf etc.tar.gz -C /opt/ [root@qls ~]# ll /opt/ total 668 drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 670293 Jul 29 09:47 services [root@qls ~]# tar czvf etc.tar.gz /etc [root@qls ~]# tar xvf etc.tar.gz [root@qls ~]# ll /bin -d lrwxrwxrwx. 1 root root 7 Jul 6 02:13 /bin -> usr/bin [root@qls ~]# tar czf bin.tar.gz /bin tar: Removing leading `/' from member names [root@qls ~]# ll total 9952 -rw-r--r-- 1 root root 110 Jul 29 10:14 bin.tar.gz drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 10171684 Jul 29 10:04 etc.tar.gz [root@qls ~]# tar xf bin.tar.gz [root@qls ~]# ll total 9952 lrwxrwxrwx 1 root root 7 Jul 6 02:13 bin -> usr/bin -rw-r--r-- 1 root root 110 Jul 29 10:14 bin.tar.gz drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 10171684 Jul 29 10:04 etc.tar.gz [root@qls ~]# tar czhf bin1.tar.gz /bin tar: Removing leading `/' from member names tar: Removing leading `/' from hard link targets [root@qls ~]# ll total 36880 lrwxrwxrwx 1 root root 7 Jul 6 02:13 bin -> usr/bin -rw-r--r-- 1 root root 27573768 Jul 29 10:16 bin1.tar.gz -rw-r--r-- 1 root root 110 Jul 29 10:14 bin.tar.gz drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 10171684 Jul 29 10:04 etc.tar.gz #打包的时候有个删根的操作 怎么避免这个删根的操作 #第一种 使用相对路径打包 [root@qls ~]# cd / [root@qls /]# tar czf /root/etc1.tar.gz etc [root@qls /]# ll /root total 46852 dr-xr-xr-x 2 root root 20480 Jul 29 08:58 bin -rw-r--r-- 1 root root 27573768 Jul 29 10:16 bin1.tar.gz -rw-r--r-- 1 root root 110 Jul 29 10:14 bin.tar.gz drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 10171684 Jul 29 10:19 etc1.tar.gz -rw-r--r-- 1 root root 10171684 Jul 29 10:04 etc.tar.gz #真实打包场景 [root@qls ~]# cd / && tar czf /root/etc3.tar.gz etc && cd - /root [root@qls ~]# ll total 581012 dr-xr-xr-x 2 root root 20480 Jul 29 08:58 bin -rw-r--r-- 1 root root 27573768 Jul 29 10:16 bin1.tar.gz -rw-r--r-- 1 root root 110 Jul 29 10:14 bin.tar.gz drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 10171684 Jul 29 10:19 etc1.tar.gz -rw-r--r-- 1 root root 347209728 Jul 29 10:22 etc2.tar.gz -rw-r--r-- 1 root root 10171684 Jul 29 10:22 etc3.tar.gz #第二种方法 使用绝对路径打包 告诉系统不要删根 #使用绝对路径打包 [root@qls ~]# tar czPf etc4.tar.gz /etc #使用绝对路经解压 [root@qls ~]# tar xPf etc4.tar.gz [root@qls ~]# cat /etc/services >> /opt/services [root@qls ~]# ll /opt/ total 1744 drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 158 Jul 29 10:28 hosts -rw-r--r-- 1 root root 1340586 Jul 29 10:29 services [root@qls ~]# tar xPf opt.tar.gz [root@qls ~]# ll /opt/ total 672 drwxr-xr-x 77 root root 8192 Jul 29 08:56 etc -rw-r--r-- 1 root root 158 Jul 29 10:28 hosts -rw-r--r-- 1 root root 670293 Jul 29 09:47 services #排除不需要打包的文件 [root@qls ~]# tar czf etc1.tar.gz /etc tar: Removing leading `/' from member names [root@qls ~]# ll total 9936 -rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz [root@qls ~]# ll /etc/services -rw-r--r-- 1 root root 670293 Jun 7 2013 /etc/services [root@qls ~]# tar czf etc2.tar.gz --exclude=/etc/services /etc tar: Removing leading `/' from member names [root@qls ~]# ll total 19740 -rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz -rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz [root@qls ~]# tar tf etc2.tar.gz | grep services etc/firewalld/services/ #排除多个文件 [root@qls ~]# tar czf etc3.tar.gz --exclude=/etc/services --exclude=/etc/passwd /etc tar: Removing leading `/' from member names [root@qls ~]# ll total 29544 -rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz -rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz -rw-r--r-- 1 root root 10035297 Jul 29 10:54 etc3.tar.gz [root@qls ~]# tar tf etc3.tar.gz | grep -E 'services|passwd' etc/security/opasswd etc/passwd- etc/pam.d/passwd etc/firewalld/services/ [root@qls ~]# tar czf etc4.tar.gz --exclude=/etc/{services,passwd} /etc tar: Removing leading `/' from member names [root@qls ~]# ll total 39348 -rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz -rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz -rw-r--r-- 1 root root 10035297 Jul 29 10:54 etc3.tar.gz -rw-r--r-- 1 root root 10035297 Jul 29 10:55 etc4.tar.gz [root@qls ~]# cat paichu.list /etc/fstab /etc/group /etc/hosts /etc/passwd /etc/services [root@qls ~]# tar czfX etc5.tar.gz paichu.list /etc tar: Removing leading `/' from member names [root@qls ~]# ll total 49152 -rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz -rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz -rw-r--r-- 1 root root 10035297 Jul 29 10:54 etc3.tar.gz -rw-r--r-- 1 root root 10035297 Jul 29 10:55 etc4.tar.gz -rw-r--r-- 1 root root 10033927 Jul 29 10:58 etc5.tar.gz -rw-r--r-- 1 root root 59 Jul 29 10:57 paichu.list [root@qls ~]# tar tf etc5.tar.gz | grep -Ew 'fstab|group|hosts|passwd|services' etc/security/group.conf etc/group- etc/passwd- etc/hosts.allow etc/hosts.deny etc/pam.d/passwd etc/iproute2/group etc/firewalld/services/ [root@qls ~]# tar czf etc6.tar.gz --exclude-from=paichu.list /etc tar: Removing leading `/' from member names [root@qls ~]# ll total 58952 -rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz -rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz -rw-r--r-- 1 root root 10035297 Jul 29 10:54 etc3.tar.gz -rw-r--r-- 1 root root 10035297 Jul 29 10:55 etc4.tar.gz -rw-r--r-- 1 root root 10033927 Jul 29 10:58 etc5.tar.gz -rw-r--r-- 1 root root 10033927 Jul 29 11:01 etc6.tar.gz -rw-r--r-- 1 root root 59 Jul 29 10:57 paichu.list |
tar命令与find结合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
[root@qls ~]# tar czf log1.tar.gz <code>find /var/log/ -type f -name "*.log" tar: Removing leading `/' from member names [root@qls ~]# tar czf log2.tar.gz $(find /var/log/ -type f -name "*.log") tar: Removing leading `/' from member names [root@qls ~]# find /var/log/ -type f -name "*.log" -exec tar czf log3.tar.gz {} \; tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names tar: Removing leading `/' from member names [root@qls ~]# [root@qls ~]# find /var/log/ -type f -name "*.log" | xargs tar czf log4.tar.gz tar: Removing leading `/' from member names [root@qls ~]# ll total 916 -rw-r--r-- 1 root root 309155 Jul 29 11:09 log1.tar.gz -rw-r--r-- 1 root root 309155 Jul 29 11:09 log2.tar.gz -rw-r--r-- 1 root root 356 Jul 29 11:10 log3.tar.gz -rw-r--r-- 1 root root 309155 Jul 29 11:10 log4.tar.gz [root@qls ~]# tar tf log1.tar.gz var/log/tuned/tuned.log var/log/audit/audit.log var/log/anaconda/anaconda.log var/log/anaconda/X.log var/log/anaconda/program.log var/log/anaconda/packaging.log var/log/anaconda/storage.log var/log/anaconda/ifcfg.log var/log/anaconda/ks-script-3PCKae.log var/log/anaconda/journal.log var/log/boot.log var/log/vmware-vmsvc.log var/log/vmware-network.8.log var/log/vmware-network.5.log var/log/vmware-network.2.log var/log/vmware-network.7.log var/log/vmware-network.4.log var/log/vmware-network.1.log var/log/test.log var/log/yum.log var/log/vmware-network.9.log var/log/nginx/error.log var/log/nginx/access.log var/log/vmware-network.6.log var/log/vmware-network.3.log var/log/vmware-network.log [root@qls ~]# tar tf log3.tar.gz var/log/vmware-network.log 当使用-exec时, 把find查找的文件是进行一个个赋值给{}的 xargs 把find命令查找出来的文件统一的赋值给了后面 |
Linux下将一个文件压缩分包成多个小文件
压缩
将文件test分包压缩成10M 的文件:
tar czf - test | split -b 10m - test.tar.gz
解压
将第一步分拆的多个包解压:
cat test.tar.gz* | tar -xzv