實驗六 文件及目錄的壓縮解壓縮相關命令的使用
【實驗目的】
1、掌握linux壓縮文件實質
2、掌握linux中壓縮及解壓縮指令的用法
【實驗環境】
1、 標准配置PC一台
2、 linux操作系統:CentOS 7.0
3、 虛擬機軟件:VMWare 14.0以上版本
【實驗重點及難點】
1、壓縮及解壓縮指令的用法
【實驗內容】
實驗內容1:熟悉CentOS7的基本命令
1、gzip指令
1)用gzip壓縮/tmp中的/passwd文件,並設定壓縮等級為1(沒有就從/etc/passwd復制一個)
gzip -1 passwd
2)解壓縮該文件
gunzip passwd.gz
3)用gzip壓縮/tmp中的/passwd文件,並設定壓縮等級為9
cd /tmp;gzip -9 passwd
4)查看壓縮后的文件內容
zcat passwd.gz
5)解壓縮該文件,並定向輸出為passwd
gunzip -c passwd.gz > password -c或--stdout或--to-stdout:把解壓后的文件輸出到標准輸出設備。
Linux壓縮保留源文件的方法:
gzip –c filename > filename.gz
Linux解壓縮保留源文件的方法:
gunzip –c filename.gz > filename
2、bzip2指令
1)用bzip2壓縮/tmp中的/passwd文件,並設定壓縮等級為1
bzip2 -1 passwd
2)解壓縮該文件
bunzip passwd.bz2
3)用bzip2壓縮/tmp中的/passwd文件,並設定壓縮等級為9
bzip2 -9 passwd
4)查看壓縮后的文件內容
bzcat passwd.bz2
5)解壓縮該文件,並定向輸出為passwd
bunzip2 -c passwd.bz2 > passwd
3、tar指令
1)把/etc內的文件打包之后存儲到/tmp下(第一個壓縮文件)
tar -cvf etc1.tar /etc/
1、 -c: 建立壓縮檔案,打包指定目錄和文件 -v:顯示所有過程
參數-f是必須的
-f: 文件名:使用檔案名字,切記,這個參數是最后一個參數,后面只能接檔案名。
2)把/etc內的文件打包並且用bzip2的方式壓縮,之后存儲到/tmp下(第二個壓縮文件)
tar -cjvf etc2.tar.bz2 /etc/ -j:有bz2屬性的
3)把/etc內的文件打包並且用gzip的方式壓縮,之后存儲到/tmp下(第三個壓縮文件)
tar -czvf etc3.tar.gz /etc/ -z:有gzip屬性的
4)分別查看壓縮后的文件中都包含哪些文件
tar -tvf etc1.tar;tar -tjvf etc2.tar.bz2;tar -tzvf etc3.tar.gz -t:查看內容
5)解壓縮第一個壓縮文件
tar -xvf etc1.tar -C /tmp/etc1 -x:解壓 -C:解壓位置
6)把第二個壓縮文件解壓縮到/tmp/etc2中(沒有就建一個)
tar -xjvf etc2.tar.bz2 -C /tmp/etc2
7)把第三個壓縮文件中的/passwd文件解壓縮到/tmp/etc3中(沒有就建一個)
tar -xzvf etc3.tar.gz -C /tmp/etc2
8)備份/etc中的所有文件到/tmp中,並且保證權限不變
tar -cvpf etcp.tar /tmp -p :使用原文件的原來屬性(屬性不會依據使用者而變) -P :可以使用絕對路徑來壓縮!
9)把/home中比2010.8.15新的文件備份到/tmp下面
tar -czvf home.tar.gz /home/ --newer-mtime 2010/8/15
10)把/etc和/root中的數據備份到/tmp下面,但是不備份/root下的initial-setup-ks.cfg文件
tar -czvf etcroot.tar.gz /etc/ /root/ --exclude /root/initial-setup-ks.cfg
4、綜合指令練習
1)使用centos在centos的家目錄下新建文件夾testdir
mkdir testdir
2)進入testdir,創建文本文件firstfile,並在其中輸入姓名和學號的全拼
cd testdir;touch firstfile;vi firstfile
3)在testdir中創建文件firstfile的副本
cp firstfile fitstfile.bak
4)在testdir中創建文件firstfile的軟硬鏈接文件
ln -s firstfile first-soft
ln -d fitstfile first-hard
5)修改firstfile的內容
vi firstfile
6)查看firstfile的副本,firstfile的軟硬鏈接文件的內容
7)返回centos的家目錄,將目錄testdir打包並壓縮為testdir.tar.gz
cd /home/centos;tar -czvf.tar.gz testdir/
8)進入目錄/tmp中,並在其中創建子目錄tdir
cd /tmp;mkdir tdir
9)將剛才創建的壓縮包復制到當前目錄的子目錄tdir中
cp /home/centos/testdir.tar.gz . . :當前目錄
10)在/tmp目錄中解壓該壓縮包,觀察解壓后的目錄出現在何處
tar -xzvf testdir.tar.gz
11)使用選項-C將該壓縮包解壓到指定目錄中/tmp/tdir2(沒有就創建一個),觀察解壓后的目錄出現在何處
mkdir /tmp/tdir2;tar -xzvf testdir.tar.gz -C /tmp/tdir2/
12)將testdir再次壓縮為testdir2.tar.gz,此次使用p和P選項
tar -czvpPf testdir2.tar.gz
13)將testdir2.tar.gz復制到/tmp/tdir中fa
cp testdir.tar.gz /tmp/tidr
14)將centos的家目錄的testdir目錄刪除,接着將/tmp下解壓出的testdir目錄也刪除
rm -rf
15) 將壓縮包/tmp下的testdir2.tar.gz解壓,完成后在centos家目錄下和/tmp目錄中搜索testdir目錄的位置,觀察其結果。
tar -czvpPf testdir2.tar.gz /home/centos/testdir