歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux tar壓縮排除某個文件夾

Linux tar壓縮排除某個文件夾

日期:2017/2/28 14:26:23   编辑:Linux教程

一般直接用tar命令打包很簡單,直接使用 tar -zcvf test.tar.gz test 即可。

在很多時候,我們要對某一個目錄打包,而這個目錄下有幾十個子目錄和子文件,我們需要在打包的時候排除其中1、2個目錄或文件。

這時候我們在用tar命令打包的時候,增加參數 --exclude 就能達到目的。

例如:

我們以tomcat 為例,打包的時候我們要排除 tomcat/logs 目錄,命令如下:

tar -zcvf tomcat.tar.gz --exclude=tomcat/logs tomcat

如果要排除多個目錄,增加 --exclude 即可,如下命令排除logs和libs兩個目錄及文件xiaoshan.txt:

tar -zcvf tomcat.tar.gz --exclude=tomcat/logs --exclude=tomcat/libs --exclude=tomcat/xiaoshan.txt tomcat

這裡要說一下注意事項:

大家都知道Linux在使用tab鍵的時候會對目錄名稱自動補全,這很方便,大家也比較常用。

如我們輸入 tomcat/lo 的時候按tab鍵,命令行會自動生成 tomcat/logs/ ,對於目錄,最後會多一個 “/”

這裡大家要注意的時候,在我們使用tar 的--exclude 命令排除打包的時候,不能加“/”,否則還是會把logs目錄以及其下的文件打包進去。

錯誤寫法:

tar -zcvf tomcat.tar.gz --exclude=tomcat/logs/ --exclude=tomcat/libs/ tomcat

正確寫法:

tar -zcvf tomcat.tar.gz --exclude=tomcat/logs --exclude=tomcat/libs tomcat

Linux tar壓縮時排除某個目錄或文件的參數 http://www.linuxidc.com/Linux/2013-01/78709.htm

Copyright © Linux教程網 All Rights Reserved