本文介紹RHEL7.2文件的歸檔和壓縮
歸檔的好處:方便使用、查詢、閱讀,易於管理 (批量刪除文件)
命令:tar
作用:將許多文件一起保存至一個單獨的磁帶或磁盤歸檔,並能從歸檔中單獨還原所需文件
用法: tar [選項...] [FILE]...
將文件歸檔至grub.tar
[root@localhost ~]# tar -cvf grub.tar httpd.conf install.log install.log.syslog
httpd.conf
install.log
install.log.syslog
查看grub.tar歸檔中的文件
[root@localhost ~]# tar -tvf grub.tar
-rw-r--r-- root/root 0 2015-10-15 15:32 httpd.conf
-rw-r--r-- root/root 47316 2015-10-14 00:28 install.log
-rw-r--r-- root/root 10733 2015-10-14 00:26 install.log.syslog
解檔grub.tar至test目錄
[root@localhost ~]# mkdir test
[root@localhost ~]# tar -xvf grub.tar -C test
httpd.conf
install.log
install.log.syslog
[root@localhost ~]# ll test/
total 60
-rw-r--r-- 1 root root 0 10月 15 2015 httpd.conf
-rw-r--r-- 1 root root 47316 10月 14 2015 install.log
-rw-r--r-- 1 root root 10733 10月 14 2015 install.log.syslog
linux對於文件的擴展名沒有像windows要求的那麼嚴格,所以在使用linux的過程中經常會遇到有些文件根本就沒有擴展名,哪麼我們應該如何去判斷沒有擴展名的文件,到底是文件還是目錄呢?
命令:file
作用:確定文件類型
語法:file [選項...] [文件...]
[root@localhost ~]# touch a.txt
[root@localhost ~]# file a.txt
a.txt: empty
[root@localhost ~]# file test/
test/: directory
[root@localhost ~]# file /etc/init.d/network
/etc/init.d/network: Bourne-Again shell script text executable
將文件進行歸檔後的歸檔文件大小為所有文件大小之和
命令:du
作用:計算每個文件的磁盤用量,目錄則取總用量
語法:du [選項]... [文件]...
[root@localhost ~]# mkdir grub
[root@localhost ~]# tar -cvf grub.tar httpd.conf install.log install.log.syslog
httpd.conf
install.log
install.log.syslog
[root@localhost ~]# tar -xvf grub.tar -C grub
httpd.conf
install.log
install.log.syslog
[root@localhost ~]# du -sh grub/*
0 grub/httpd.conf
48K grub/install.log
12K grub/install.log.syslog
[root@localhost ~]# ll -sh grub.tar
60K -rw-r--r-- 1 root root 60K 8月 15 16:24 grub.tar
壓縮文件能節約硬盤的資源和加快文件傳輸時的速率
壓縮格式:xz
壓縮文件格式:.xz
壓縮語法:tar Jcvf filename.xz SOURCE
解壓語法:tar Jxvf filename.xz <-C 目標解壓目錄>
[root@localhost archive]# tar -Jcvf archive.xz httpd.conf install.log
httpd.conf
install.log
[root@localhost archive]# tar -Jxvf archive.xz
httpd.conf
install.log
壓縮格式:bzip2
壓縮文件格式:.tar.bz2
壓縮語法:tar jcvf filename.tar.bz2 SOURCE
解壓語法:tar jxvf filename.tar.bz2 <-C 目標解壓目錄>
[root@localhost archive]# tar -jcvf archive.tar.bz2 httpd.conf install.log
httpd.conf
install.log
[root@localhost archive]# tar -jxvf archive.tar.bz2
httpd.conf
install.log
壓縮格式:gzip2
壓縮文件格式:.tar.gz或tgz
壓縮語法:tar zcvf filename.tgz SOURCE
解壓語法:tar zxvf filename.tgz <-C 目標解壓目錄>
[root@localhost archive]# tar -zcvf archive.tgz httpd.conf install.log
httpd.conf
install.log
[root@localhost archive]# tar -zxvf archive.tgz
httpd.conf
install.log
壓縮格式:zip
壓縮文件格式:.zip
壓縮語法:zip -r filename.zip SOURCE
解壓語法:unzip <-d 目標解壓目錄> filename.zip
[root@localhost archive]# zip -r archive.zip httpd.conf install.log
adding: httpd.conf (stored 0%)
adding: install.log (deflated 75%)
[root@localhost archive]# unzip archive.zip
Archive: archive.zip
replace httpd.conf? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: httpd.conf
replace install.log? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: install.log