tar、cpio歸檔文件

1、tar 歸檔、解壓用戶的文件

(1.)

[root@localhost ~]# tar cf dirname.tar  dir/* file*
	//將dir/* file*目錄打包保存爲dirname.tar

-c :創建一個新的歸檔文件

-f:指向一個歸檔文件(有f出現,後面必須存在文件)

[root@localhost tar]# tar -cf etc.tar /etc/(被壓縮文件的路徑)
tar: Removing leading `/' from member names
	//創建一個新的歸檔文件etc.tar,並將在路徑/etc/下的所有文件壓縮到歸檔文件etc.tar中

(2.)

-z:以 gzip形式將打包的文件壓縮 .tar.gz

[root@localhost tar]# tar -czf boot.tar.gz /boot/
tar: Removing leading `/' from member names

(3.)

-j:以bzip2的形式,將打包的文件壓縮 .tar.bz2

[root@localhost tar]# tar -cjf boot.tar.bz2 /tmp/
tar: Removing leading `/' from member names

(4.)

-t:查看歸檔文件內容

[root@localhost tar]# tar -tf etc.tar

(5.)

–delete +要刪除的文件 :刪除指定內容

[root@localhost tar]# tar --delete etc/hosts -f etc.tar
	//--delete:刪除etc/tar下的etc/hosts

(6.)

-r +要新加的內容:向打包文件中追加新內容

[root@localhost tar]# tar -f etc.tar -r /root/anaconda-ks.cfg.bak
	//向打包文件etc.tar 中追加新內容,新內容是/root下的anaconda-ks.cfg.bak

(7.)

-v:查看詳細信息

[root@localhost tar]# tar -tvf etc.tar |grep hosts
-rw-r--r-- root/root       460 2013-06-07 22:31 etc/hosts.deny等等

(8.)

-x:解壓

-xz:解壓 gzip 壓縮的歸檔文件

[root@localhost tar]# ls
boot.tar.gz 
[root@localhost tar]# tar -xzf boot.tar.gz
[root@localhost tar]# ls
boot  boot.tar.gz 

(9.)

-xj:解壓 bzip2 壓縮的歸檔文件

[root@localhost tar]# tar xjf boot.tar.bz2
[root@localhost tar]# ls
boot.tar.bz2	tmp

(10.)

-C :指定解壓路徑

[root@localhost tar]# tar xjf boot.tar.bz2 -C  test/
	//將 boot.tar.bz2文件中的內容解壓到指定目錄test中

(11.)

–remove-files :打包壓縮的同時刪除源文件

[root@localhost ~]# tar -czf test.tar.gz  file* --remove-files
file1
file1.txt
file1.zip
	//創建一個歸檔文件test.tar.gz,將~目錄下的所有以file開頭的文件打包壓縮到歸檔文件test.tar.gz中,在~下的原來的以file開頭的所有文件均被刪除

(12.)

&& :兩步,前一步解壓文件,後一步把源文件刪除

[root@localhost ~]# tar xzvf test.tar.gz && rm -rf 
	//將test.tar.gz文件中的內容解壓出來後,再將test.tar.gz文件刪除(&&前後的文件名必須一致)

2、cpio 歸檔、解壓系統的文件:

(cpio是Linux源生的歸檔命令)

爲什麼cpio文件中含有自己?

​ find找尋一部分目錄後,cpio開始工作,因此產生tree.cpio文件,tree.cpio文件存在於目錄中,此時find再找尋時就找到了tree.cpio文件,此時tree.cpio文件就存在於tree.cpio文件中了。

[root@localhost ~]# find  ./  -depth |cpio  -ov -F  tree1.cpio
	//表示把當前的文件歸檔到了tree.cpio,tree.cpio裏面含有它本身

參數:

​ -t :看歸檔內容

​ -F:指定歸檔文件

​ -o:copy-out 模式

​ -A:追加新文件

​ -i:copy-in模式,提取

​ -d:需要時自動創建目錄

​ -v:顯示詳細信息

​ -p:copy-pass模式

(1.)

-F:指定歸檔文件

[root@localhost ~]# find  ./  -depth  |cpio  -ov -F  tree1.cpio
	//把當前的文件 歸檔到了tree1.cpio ,tree.cpio裏面有本身
	
[root@localhost ~]# find ./   -depth  |cpio  -ov -F  /tmp/tree.cpio
	//把當前的文件 歸檔到/tmp/目錄下的/tree.cpio中
	
[root@localhost ~]# find /home -depth |cpio -ov -F /tmp/home.cpio
	//把/home下的文件 歸檔到/tmp/目錄下的home.cpio中

(2.)

–null:解析空字符

[root@localhost ~]# find  ./  -depth  -print0 |cpio  --null -ov -F  /tmp/tree2.cpio
	//--null:解析空字符

(3.)

-t:查看歸檔內容

[root@localhost ~]# cpio -t  -F tree.cpio   
或者 
[root@localhost ~]# cpio  -t  < tree.cpio
	//查看tree.cpio下的歸檔內容
[root@localhost ~]# cpio -t  -F  /tmp/home1.cpio  /root/*
	//查看home1.cpio歸檔文件裏面的/root/下的內容  (沒有隱藏文件)
[root@localhost ~]# cpio -t  -F  /tmp/home1.cpio  /root/.*
	//查看home1.cpio歸檔文件裏面的/root/下的內容  (全是隱藏文件)
[root@localhost ~]# cpio -t  -F  /tmp/home1.cpio  /root/{.* ,*}
	//查看home1.cpio歸檔文件裏面的/root/下的內容  (所有文件,包括隱藏文件和非隱藏文件)

(4.)

-o:copy-out 模式

-A:追加新文件

[root@localhost ~]# ls /root/test.txt |cpio -oA -F /tmp/home1.cpio
	//向歸檔文件home1.cpio中追加文件test.txt
[root@localhost ~]# find /boot -depth -print0 |cpio -oA -F /tmp/home1.cpio
	//添加新目錄/boot

(5.)

-i :copy-in模式,提取

-d:需要時自動創建目錄

-v:顯示詳細信息(過程)

[root@localhost ~]# cpio -idv -F /tmp/home1.cpio  
	//提取文件

(6.)

-p:copy-pass模式

[root@localhost ~]# find ~ -depth -print0 | cpio --null -pvd /tmp/abc   
	//複製~下的文件到/tmp/abc/root/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章