這兩天在研究tar指令,看了很多前輩的部落格。參考前輩的文章自己做實驗寫一下心得。
擒賊先擒王這篇文章寫的很仔細。參數詳情請參考連結。
隻用tar指令是打包的意思,發現一個有意思的事。先寫一下吧
在根目錄下建立名為tar的檔案夾,并建立四個文本檔案。
這裡多用了幾個指令,是為了練習一下。
rm -rf 是删除指令,-r 是删除目錄的意思, -f 是強制删除。生産環境下不要亂用!!
例一
[[email protected] /]# mkdir tar
[[email protected] /]# cd tar
[[email protected] tar]# ls
[[email protected] tar]# man help > 1.txt //将help指令的詳細資訊重定向到1.txt
[[email protected] tar]# cat 1.txt > 2.txt //将1.txt檔案内容重定向到2.txt
[[email protected] tar]# cp 2.txt 3.txt //複制2.txt并重命名為3.txt
[[email protected] tar]# touch 4.txt //建立4.txt空文檔
[[email protected] tar]# ls
1.txt 2.txt 3.txt 4.txt
用 du 指令檢視文檔大小
例二
[[email protected] tar]# du -sh * //檢視本目錄所有檔案的大小
104K 1.txt
104K 2.txt
104K 3.txt
0 4.txt
将 tar 檔案夾下所有的txt檔案打包(僅打包)成 all.tar
例三
[[email protected] tar]# tar -cvf all.tar *.txt //加上-v參數顯示打包過程
1.txt
2.txt
3.txt
4.txt
檢視打包後檔案大小
例四
[[email protected] tar]# du -sh all.tar //檢視指定檔案的大小
312K all.tar
重新寫入内容并檢視其大小
例五
[[email protected] tar]# man ls > 4.txt //将資訊重定向到檔案内
[[email protected] tar]# tar -uf all.tar 4.txt //用-u參數更新包内内容
[[email protected] tar]# man ls >> 4.txt //将資訊追加重定向到檔案
[[email protected] tar]# du -sh 4.txt
20K 4.txt
不知道這是什麼情況?明明已經更新檔案了可是包裡有好幾個 4.txt 檔案。
例六
[[email protected] tar]# tar -uf all.tar 4.txt //用-u參數更新包内内容
[[email protected] tar]# tar -tf all.tar //-t參數不解壓的情況下檢視包内檔案
1.txt
2.txt
3.txt
4.txt
4.txt
包的大小也不對,可是解壓出來後就隻有一個 4.txt 了?這是咋回事,沒弄明白。隻是包内檔案确實更新了!先記下,有空再仔細研究下原理。
例七
[[email protected] tar]# du -sh all.tar
768K all.tar
[[email protected] tar]# mkdir new //建立檔案夾
[[email protected] tar]# tar -xf all.tar -C /tar/new // -x參數解壓,-C (大寫)用于指定解壓檔案夾
[[email protected] tar]# cd new
[[email protected] new]# ls
1.txt 2.txt 3.txt 4.txt
[[email protected] new]# du -sh
332K .
增加新檔案
先把原來的檔案删了,再重新建立一個。檢視包内内容那麼别扭呢?
例八
[[email protected] tar]# rm -rf all.tar
[[email protected] tar]# tar -cf all.tar *.txt
[[email protected] tar]# ls
1.txt 2.txt 3.txt 4.txt all.tar
[[email protected] tar]# touch 5.txt
[[email protected] tar]# tar -rf all.tar 5.txt //-r 參數追加寫入檔案
[[email protected] tar]# tar -tf all.tar
1.txt
2.txt
3.txt
4.txt
5.txt
以上幾個參數不能疊加使用,即 tar 後必須且隻能出現一個,不能同時打包、更新、追加、解壓、檢視。
-f 參數隻能是最後一個參數,-f 參數後緊跟打包後的檔案名。
壓縮參數 -z -j
下面再玩玩壓縮參數吧,單獨使用 tar 不加 -z 或 -j 參數檔案隻會是打包。不會壓縮
先試試 -z 參數,gzip格式打包一下看一下效果吧!
例九
[[email protected] tar]# tar -czvf all.tar.gz *.txt && du -sh *
1.txt
2.txt
3.txt
4.txt
232K 1.txt
104K 2.txt
104K 3.txt
20K 4.txt
112K all.tar.gz
效果顯而易見,壓縮率還是很高的
下面來玩一下解壓某個檔案夾内的指定檔案/檔案夾吧!
又學了一招,指令回傳碼 (與 或)
與 || 前面的指令執行失敗後執行後面的指令。
或 && 是前面的指令執行成功後執行後面的指令
例十
[[email protected] tar]# ls /tar/new || mkdir /tar/new && touch /tar/new/5.txt && ls
ls: cannot access /tar/new: No such file or directory
1.txt 2.txt 3.txt 4.txt new
[[email protected] tar]# tar -czvf all.tar.gz * && rm -rf new && ls
1.txt
2.txt
3.txt
4.txt
new/
new/5.txt
1.txt 2.txt 3.txt 4.txt all.tar.gz
首先在本目錄下解壓
要知道路徑是個什麼東西
絕對路徑 :從根目錄開始寫起的檔案或目錄名稱
相對路徑 :相對于目前路徑的寫法
需要注意的是指令後的路徑為 相對路徑,實驗多次如果路徑輸錯會報警,指令執行不成功。
看看錯誤的示例
例十一
[[email protected] tar]# tar -xzvf all.tar.gz /tar/new/5.txt && ls
tar: /tar/new/5.txt: Not found in archive
tar: Exiting with failure status due to previous errors
以下是正确的示例
例十二
[[email protected] tar]# tar -xzvf all.tar.gz new/5.txt
//注意路徑為相對路徑
new/5.txt //成功
再來玩一下在别的目錄下解壓到某一個指定的目标吧
例十三
[[email protected] tar]# rm -rf all.tar.gz
[[email protected] tar]# ll
total 268
-rw-r--r--. 1 root root 54284 May 16 16:42 1.txt
-rw-r--r--. 1 root root 103499 May 8 05:53 2.txt
-rw-r--r--. 1 root root 103499 May 8 05:53 3.txt
-rw-r--r--. 1 root root 11 May 15 17:10 4.txt
drwxr-xr-x. 2 root root 18 May 16 17:40 new
[[email protected] tar]# cd /home
[[email protected] home]# tar -czvf all.tar.gz /tar/* && rm -rf /tar/new
tar: Removing leading `/' from member names //試了很多遍也沒有把這個警給去掉,沒找到原因
/tar/1.txt
/tar/2.txt
/tar/3.txt
/tar/4.txt
/tar/new/
/tar/new/5.txt
[[email protected] home]# tar -xzvf all.tar.gz tar/new/5.txt -C /tar/ && ll /tar
//-C 參數指解壓到指定目錄, 這個路徑是相對于 /home 目錄的
tar/new/5.txt
total 268
-rw-r--r--. 1 root root 54284 May 16 16:42 1.txt
-rw-r--r--. 1 root root 103499 May 8 05:53 2.txt
-rw-r--r--. 1 root root 103499 May 8 05:53 3.txt
-rw-r--r--. 1 root root 11 May 15 17:10 4.txt
哎呀大功告成總算完工了,繼續研究下一個指令。