{"id":466,"date":"2014-07-07T16:22:36","date_gmt":"2014-07-07T16:22:36","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=466"},"modified":"2014-07-07T16:22:36","modified_gmt":"2014-07-07T16:22:36","slug":"linux%e4%b8%8b%e7%9a%84tar%e5%91%bd%e4%bb%a4%e7%9a%84%e4%b8%80%e4%ba%9b%e5%9d%91","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/466.html","title":{"rendered":"Linux\u4e0b\u7684tar\u547d\u4ee4\u7684\u4e00\u4e9b\u5751"},"content":{"rendered":"<p>\u7528Linux\u7528\u4e86\u8fd9\u4e48\u957f\u65f6\u95f4\uff0c\u78b0\u5230\u7684\u5751\u4e5f\u4e0d\u5c11\uff0c\u5176\u4e2d\u5bf9\u4e8etar\u547d\u4ee4\u7684&#8211;exclude\u9009\u9879\u5c31\u6709\u4e0d\u5c11\uff0c\u521a\u521a\u53c8\u78b0\u4e86\u4e00\u4e2a\uff0c\u8bb0\u5f55\u4e00\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">tar -cpzf \/media\/sda7\/backup.tgz --exclude=\/proc --exclude=\/lost+found --exclude=\/mnt --exclude=\/sys --exclude=\/media \/<\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u5199\u6cd5\u62a5\u9519\uff0c\u56e0\u4e3atar\u547d\u4ee4\u6709\u4e00\u4e2a\u56fa\u5b9a\u683c\u5f0f\u201c<span style=\"color: #000000;\">tar -jcpf target source<\/span>\u201d\uff0c\u6240\u4ee5\uff0c\u6b63\u786e\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># tar -cpzf \/media\/sda7\/backup.tgz \/ --exclude=\/proc --exclude=\/lost+found --exclude=\/mnt --exclude=\/sys --exclude=\/media<\/pre>\n<p>\u6216<\/p>\n<pre class=\"lang:default decode:true  \"># tar -cvpzf --exclude=\/proc --exclude=\/lost+found --exclude=\/mnt --exclude=\/sys --exclude=\/media \/media\/sda7\/backup.tgz\u00a0\/<\/pre>\n<p>&#8211;exclude\u4ec5\u4ec5\u662f\u4e2a\u9009\u9879\uff0c\u524d\u540e\u65e0\u6240\u8c13\uff0c\u4f46\u662f\u4e0d\u8981\u7834\u574f\u4e86\uff1atar -jcpf file.tar.bz2 \/path \u7684\u56fa\u5b9a\u683c\u5f0f\uff08\u56e0\u4e3atar\u547d\u4ee4\u7684-f\u9009\u9879\u7684\u7528\u6cd5\uff1a<span style=\"color: #000000;\">-f target source\uff0c\u5176\u4e2d\u5728target \u548c source \u4e2d\u95f4\u4e0d\u53ef\u4ee5\u5939\u6742\u9009\u9879<\/span>\uff09<\/p>\n<p>\u53c2\u8003\uff1a<a href=\"http:\/\/bbs.chinaunix.net\/thread-3761777-1-1.html\" target=\"_blank\">tar\u547d\u4ee4\u4e2d\u7684&#8211;exclude\u9009\u9879-Shell-ChinaUnix.net<\/a><\/p>\n<p>\u8865\u5145\u4e00\u4e0btar\u547d\u4ee4\u4f7f\u7528\u7684\u603b\u4f53\u4ecb\u7ecd\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\uff1a<\/p>\n<div style=\"color: #000000;\">\n<p style=\"color: #494949;\">\u8bed\u6cd5\uff1atar [\u4e3b\u9009\u9879+\u8f85\u9009\u9879] \u6587\u4ef6\u6216\u8005\u76ee\u5f55<\/p>\n<p style=\"color: #494949;\">\u4f7f\u7528\u8be5\u547d\u4ee4\u65f6\uff0c\u4e3b\u9009\u9879\u662f\u5fc5\u987b\u8981\u6709\u7684\uff0c\u5b83\u544a\u8bc9tar\u8981\u505a\u4ec0\u4e48\u4e8b\u60c5\uff0c\u8f85\u9009\u9879\u662f\u8f85\u52a9\u4f7f\u7528\u7684\uff0c\u53ef\u4ee5\u9009\u7528\u3002<\/p>\n<p><strong>\u4e3b\u9009\u9879\uff1a<\/strong><\/p>\n<p><span style=\"color: #000000;\">c \u521b\u5efa\u65b0\u7684\u6863\u6848\u6587\u4ef6\u3002\u5982\u679c\u7528\u6237\u60f3\u5907\u4efd\u4e00\u4e2a\u76ee\u5f55\u6216\u662f\u4e00\u4e9b\u6587\u4ef6\uff0c\u5c31\u8981\u9009\u62e9\u8fd9\u4e2a\u9009\u9879\u3002\u76f8\u5f53\u4e8e\u6253\u5305\u3002<\/span><\/p>\n<p style=\"color: #494949;\"><span style=\"color: #000000;\">x \u4ece\u6863\u6848\u6587\u4ef6\u4e2d\u91ca\u653e\u6587\u4ef6\u3002\u76f8\u5f53\u4e8e\u62c6\u5305\u3002<\/span><\/p>\n<p style=\"color: #494949;\">t \u5217\u51fa\u6863\u6848\u6587\u4ef6\u7684\u5185\u5bb9\uff0c<strong><span style=\"color: #ff0000;\">\u67e5\u770b<\/span><\/strong>\u5df2\u7ecf\u5907\u4efd\u4e86\u54ea\u4e9b\u6587\u4ef6\u3002<\/p>\n<p style=\"color: #494949;\"><span style=\"color: #ff0000;\"><span style=\"font-size: medium;\">\u7279\u522b\u6ce8\u610f\uff0c\u5728\u53c2\u6570\u7684\u4e0b\u8fbe\u4e2d\uff0c c\/x\/t \u4ec5\u80fd\u5b58\u5728\u4e00\u4e2a\uff01\u4e0d\u53ef\u540c\u65f6\u5b58\u5728\uff01\u56e0\u4e3a\u4e0d\u53ef\u80fd\u540c\u65f6\u538b\u7f29\u4e0e\u89e3\u538b\u7f29\u3002<\/span><\/span><\/p>\n<p><strong>\u8f85\u52a9\u9009\u9879\uff1a<\/strong><\/p>\n<p style=\"color: #494949;\">-z \uff1a\u662f\u5426\u540c\u65f6\u5177\u6709 gzip \u7684\u5c5e\u6027\uff1f\u4ea6\u5373\u662f\u5426\u9700\u8981\u7528 gzip \u538b\u7f29\u6216\u89e3\u538b\uff1f \u4e00\u822c\u683c\u5f0f\u4e3axx.tar.gz\u6216xx. tgz<\/p>\n<p style=\"color: #494949;\">-j \uff1a\u662f\u5426\u540c\u65f6\u5177\u6709 bzip2 \u7684\u5c5e\u6027\uff1f\u4ea6\u5373\u662f\u5426\u9700\u8981\u7528 bzip2 \u538b\u7f29\u6216\u89e3\u538b\uff1f\u4e00\u822c\u683c\u5f0f\u4e3axx.tar.bz2<\/p>\n<p style=\"color: #494949;\">-v \uff1a\u538b\u7f29\u7684\u8fc7\u7a0b\u4e2d\u663e\u793a\u6587\u4ef6\uff01\u8fd9\u4e2a\u5e38\u7528<\/p>\n<p style=\"color: #494949;\">-f \uff1a\u4f7f\u7528\u6863\u540d\uff0c\u8bf7\u7559\u610f\uff0c<strong><span style=\"color: #ff0000;\">\u5728 f \u4e4b\u540e\u8981\u7acb\u5373\u63a5\u5f52\u6863\u540d<\/span><\/strong>\uff01\u4e0d\u8981\u518d\u52a0\u5176\u4ed6\u53c2\u6570\uff01<\/p>\n<p style=\"color: #494949;\">-p \uff1a\u4f7f\u7528\u539f\u6587\u4ef6\u7684\u539f\u6765\u5c5e\u6027\uff08\u5c5e\u6027\u4e0d\u4f1a\u4f9d\u636e\u4f7f\u7528\u8005\u800c\u53d8\uff09<\/p>\n<p style=\"color: #494949;\">&#8211;exclude FILE\uff1a\u5728\u538b\u7f29\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e0d\u8981\u5c06 FILE \u6253\u5305\uff01<\/p>\n<p style=\"color: #494949;\"><strong>\u8303\u4f8b\uff1a<\/strong><\/p>\n<p style=\"color: #494949;\"><strong><span style=\"color: #0000ff;\">\u8303\u4f8b\u4e00<\/span><\/strong>\uff1a\u5c06\u6574\u4e2a \/etc \u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u5168\u90e8\u6253\u5305\u6210\u4e3a \/tmp\/etc.tar<\/p>\n<p style=\"color: #494949;\">[root@linux ~]# tar -cvf\u00a0<span style=\"color: #ff0000;\"><strong>\/tmp\/etc.tar<\/strong><\/span>\u00a0\/etc\u3000\u3000&lt;==\u4ec5\u6253\u5305\uff0c\u4e0d\u538b\u7f29\uff01<\/p>\n<p style=\"color: #494949;\">[root@linux ~]# tar -zcvf\u00a0<strong><span style=\"color: #ff0000;\">\/tmp\/etc.tar.gz<\/span><\/strong>\u00a0\/etc\u3000\u3000&lt;==\u6253\u5305\u540e\uff0c\u4ee5 gzip \u538b\u7f29<\/p>\n<p style=\"color: #494949;\">[root@linux ~]# tar -jcvf\u00a0<span style=\"color: #ff0000;\"><strong>\/tmp\/etc.tar.bz2<\/strong><\/span>\u00a0\/etc\u3000\u3000&lt;==\u6253\u5305\u540e\uff0c\u4ee5 bzip2 \u538b\u7f29<\/p>\n<p style=\"color: #494949;\"># \u7279\u522b\u6ce8\u610f\uff0c\u5728\u53c2\u6570 f \u4e4b\u540e\u7684\u6587\u4ef6\u6863\u540d\u662f\u81ea\u5df1\u53d6\u7684\uff0c\u6211\u4eec\u4e60\u60ef\u4e0a\u90fd\u7528 .tar \u6765\u4f5c\u4e3a\u8fa8\u8bc6\u3002\uff08<strong><span style=\"color: #ff0000; font-size: medium;\">\u5728-f\u9009\u9879\u4e4b\u540e\u8981\u7acb\u5373\u63a5\u201c\u5f52\u6863\u540d\u201d\uff0c\u800c\u4e0d\u662f\u8981\u5f52\u6863\u7684\u76ee\u5f55or\u6587\u4ef6<\/span><\/strong>\uff09<\/p>\n<p style=\"color: #494949;\"># \u5982\u679c\u52a0 z \u53c2\u6570\uff0c\u5219\u4ee5 .tar.gz \u6216 .tgz \u6765\u4ee3\u8868 gzip \u538b\u7f29\u8fc7\u7684 tar file \uff5e<\/p>\n<p style=\"color: #494949;\"># \u5982\u679c\u52a0 j \u53c2\u6570\uff0c\u5219\u4ee5 .tar.bz2 \u6765\u4f5c\u4e3a\u9644\u6863\u540d\u554a\uff5e<\/p>\n<p style=\"color: #494949;\"># \u4e0a\u8ff0\u6307\u4ee4\u5728\u6267\u884c\u7684\u65f6\u5019\uff0c\u4f1a\u663e\u793a\u4e00\u4e2a\u8b66\u544a\u8baf\u606f\uff1a<\/p>\n<p style=\"color: #494949;\"># \u300etar: Removing leading `\/&#8221; from member names\u300f\u90a3\u662f\u5173\u65bc\u7edd\u5bf9\u8def\u5f84\u7684\u7279\u6b8a\u8bbe\u5b9a\u3002<\/p>\n<p style=\"color: #494949;\"><span style=\"color: #0000ff;\"><strong>\u8303\u4f8b\u4e8c<\/strong><\/span>\uff1a\u67e5\u9605\u4e0a\u8ff0 \/tmp\/etc.tar.gz \u6587\u4ef6\u5185\u6709\u54ea\u4e9b\u6587\u4ef6\uff1f<\/p>\n<p style=\"color: #494949;\">[root@linux ~]# tar -ztvf \/tmp\/etc.tar.gz<\/p>\n<p style=\"color: #494949;\"># \u7531\u65bc\u6211\u4eec\u4f7f\u7528 gzip \u538b\u7f29\uff0c\u6240\u4ee5\u8981\u67e5\u9605\u8be5 tar file \u5185\u7684\u6587\u4ef6\u65f6\uff0c\u5c31\u5f97\u8981\u52a0\u4e0a z \u8fd9\u4e2a\u53c2\u6570\u4e86\uff01\u8fd9\u5f88\u91cd\u8981\uff01<\/p>\n<p style=\"color: #494949;\"><span style=\"color: #0000ff;\"><strong>\u8303\u4f8b\u4e09<\/strong><\/span>\uff1a\u5c06 \/tmp\/etc.tar.gz \u6587\u4ef6\u89e3\u538b\u7f29\u5728 \/usr\/local\/src \u4e0b<\/p>\n<p style=\"color: #494949;\">[root@linux ~]# cd \/usr\/local\/src<\/p>\n<p style=\"color: #494949;\">[root@linux src]# tar -zxvf \/tmp\/etc.tar.gz<\/p>\n<p style=\"color: #494949;\"># \u5728\u9884\u8bbe\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u538b\u7f29\u6863\u5728\u4efb\u4f55\u5730\u65b9\u89e3\u5f00\u7684\uff01\u4ee5\u8fd9\u4e2a\u8303\u4f8b\u6765\u8bf4\uff1a<span style=\"line-height: 1.5;\">\u6211\u5148\u5c06\u5de5\u4f5c\u76ee\u5f55\u53d8\u6362\u5230 \/usr\/local\/src \u5e95\u4e0b\uff0c\u5e76\u4e14\u89e3\u5f00 \/tmp\/etc.tar.gz <\/span>\u5219\u89e3\u5f00\u7684\u76ee\u5f55\u4f1a\u5728 \/usr\/local\/src\/etc \uff0c\u53e6\u5916\uff0c\u5982\u679c\u60a8\u8fdb\u5165 \/usr\/local\/src\/etc \u5219\u4f1a\u53d1\u73b0\uff0c\u8be5\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u5c5e\u6027\u4e0e \/etc\/ \u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff01<\/p>\n<p style=\"color: #494949;\"><span style=\"color: #0000ff;\"><strong>\u8303\u4f8b\u56db<\/strong><\/span>\uff1a\u5728 \/tmp \u5e95\u4e0b\uff0c\u6211\u53ea\u60f3\u8981\u5c06 \/tmp\/etc.tar.gz \u5185\u7684 etc\/passwd \u89e3\u5f00\u800c\u5df2<\/p>\n<p style=\"color: #494949;\">[root@linux ~]# cd \/tmp<\/p>\n<p style=\"color: #494949;\">[root@linux tmp]# tar -zxvf \/tmp\/etc.tar.gz etc\/passwd<\/p>\n<p style=\"color: #494949;\"># \u6211\u53ef\u4ee5\u900f\u8fc7 tar -ztvf \u6765\u67e5\u9605 tarfile \u5185\u7684\u6587\u4ef6\u540d\u79f0\uff0c\u5982\u679c\u5355\u53ea\u8981\u4e00\u4e2a\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u900f\u8fc7\u8fd9\u4e2a\u65b9\u5f0f\u6765\u4e0b\u8fbe\uff01\u6ce8\u610f\u5230\uff01 etc.tar.gz \u5185\u7684\u6839\u76ee\u5f55 \/ \u662f\u88ab\u62ff\u6389\u4e86\uff01<\/p>\n<p style=\"color: #494949;\"><span style=\"color: #0000ff;\"><strong>\u8303\u4f8b\u4e94<\/strong><\/span>\uff1a\u6211\u8981\u5907\u4efd \/home, \/etc \uff0c\u4f46\u4e0d\u8981 \/home\/dmtsai<\/p>\n<p style=\"color: #494949;\">[root@linux ~]# tar &#8211;exclude \/home\/dmtsai -zcvf myfile.tar.gz \/home\/* \/etc<\/p>\n<p style=\"color: #494949;\"><strong>\u53e6\u5916\uff1atar\u547d\u4ee4\u7684-C\u53c2\u6570<\/strong><\/p>\n<p style=\"color: #494949;\"><strong><span style=\"color: #000000;\">\u3000\u3000$ tar -cvf file2.tar \/home\/usr2\/file2<\/span><\/strong><br \/>\ntar: Removing leading &#8216;\/&#8217; from members names<br \/>\nhome\/usr2\/file2<br \/>\n\u8be5\u547d\u4ee4\u53ef\u4ee5\u5c06\/home\/usr2\/file2\u6587\u4ef6\u6253\u5305\u5230\u5f53\u524d\u76ee\u5f55\u4e0b\u7684file2.tar\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<span style=\"color: #ff0000;\">\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6807\u8bc6\u7684\u6e90\u6587\u4ef6\uff0c\u5728\u7528tar\u547d\u4ee4\u538b\u7f29\u540e\uff0c\u6587\u4ef6\u540d\u8fde\u540c\u7edd\u5bf9\u8def\u5f84\uff08\u8fd9\u91cc\u662fhome\/usr2\/\uff0c\u6839\u76ee\u5f55&#8217;\/&#8217;\u88ab\u81ea\u52a8\u53bb\u6389\u4e86\uff09\u4e00\u5e76\u88ab\u538b\u7f29\u8fdb\u6765<\/span>\u3002\u4f7f\u7528tar\u547d\u4ee4\u89e3\u538b\u7f29\u540e\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u60c5\u51b5\uff1a<br \/>\n$ tar -xvf file2.tar<br \/>\n$ ls<br \/>\n\u2026\u2026 \u2026\u2026 home \u2026\u2026 \u2026\u2026<br \/>\n\u89e3\u538b\u7f29\u540e\u7684\u6587\u4ef6\u540d\u4e0d\u662f\u60f3\u8c61\u4e2d\u7684file2\uff0c\u800c\u662fhome\/usr2\/file2\u3002<\/p>\n<p><strong>\u3000\u3000$ tar -cvf file2.tar -C \/home\/usr2 file2<br \/>\n<\/strong>\u3000\u3000\u8be5\u547d\u4ee4\u4e2d\u7684-C dir\u53c2\u6570\uff0c\u5c06tar\u7684\u5de5\u4f5c\u76ee\u5f55\u4ece\u5f53\u524d\u76ee\u5f55\u6539\u4e3a\/home\/usr2\uff0c\u5c06file2\u6587\u4ef6\uff08\u4e0d\u5e26\u7edd\u5bf9\u8def\u5f84\uff09\u538b\u7f29\u5230file2.tar\u4e2d\u3002\u6ce8\u610f<span style=\"color: #ff0000;\">\uff1a-C dir\u53c2\u6570\u7684\u4f5c\u7528\u5728\u4e8e\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55\uff0c\u5176\u6709\u6548\u671f\u4e3a\u8be5\u547d\u4ee4\u4e2d\u4e0b\u4e00\u6b21-C dir\u53c2\u6570\u4e4b\u524d<\/span>\u3002<br \/>\n\u4f7f\u7528tar\u7684-C dir\u53c2\u6570\uff0c\u540c\u6837\u53ef\u4ee5\u505a\u5230\u5728\u5f53\u524d\u76ee\u5f55\/home\/usr1\u4e0b\u5c06\u6587\u4ef6\u89e3\u538b\u7f29\u5230\u5176\u4ed6\u76ee\u5f55\uff0c\u4f8b\u5982\uff1a<br \/>\n<strong>\u3000\u3000$ tar -xvf file2.tar -C \/home\/usr2<\/strong><br \/>\n\u800ctar\u4e0d\u7528-C dir\u53c2\u6570\u65f6\u662f\u65e0\u6cd5\u505a\u5230\u7684\uff1a<br \/>\n$ tar -xvf file2.tar \/home\/usr2<br \/>\ntar: \/tmp\/file: Not found in archive<br \/>\ntar: Error exit delayed from previous errors<\/p>\n<\/div>\n<div style=\"color: #000000;\"><span style=\"color: #000000; font-size: medium;\">\u53c2\u8003\u6587\u7ae0\uff1a<a href=\"http:\/\/www.cnblogs.com\/li-hao\/archive\/2011\/10\/03\/2198480.html\" target=\"_blank\">linux\u4e0b\u4f7f\u7528tar\u547d\u4ee4<\/a><\/span><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528Linux\u7528\u4e86\u8fd9\u4e48\u957f\u65f6\u95f4\uff0c\u78b0\u5230\u7684\u5751\u4e5f\u4e0d\u5c11\uff0c\u5176\u4e2d\u5bf9\u4e8etar\u547d\u4ee4\u7684&#8211;exclude\u9009\u9879\u5c31\u6709\u4e0d\u5c11\uff0c\u521a\u521a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[210,211],"class_list":["post-466","post","type-post","status-publish","format-standard","hentry","category-linux","category-tools","tag-exclude","tag-tar"],"views":4467,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=466"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}