Linux tar、zip、file、sort、 文件的歸檔和壓縮

1.tar命令進(jìn)行文件的歸檔和壓縮

? ? ? ? ?作用:打包、壓縮文件;tar 文件是把幾個(gè)文件和(或)目錄集合在一個(gè)文件里,該存檔文件可以通過使用gzip、bzip2或xz等壓縮工具進(jìn)行行壓縮后傳輸

用法:tar [OPTION...] [FILE]...

參數(shù):

-c create創(chuàng)建文件

-x -extract?[?ekstr?kt]提取解壓還原文件

-v --verbose顯示執(zhí)行詳細(xì)過程

-f --file指定備份文件

-t --list列出壓縮包中包括哪些文件,不解包,查看包中的內(nèi)容

-C (大寫)--directory指定解壓位置

例:給/boot/grub目錄打包

[root@root??opt]# tar -cvf grub.tar /boot/grub???#tar的參數(shù)前可以不加“-”

[root@root??opt]# tar cvf grub.tar /boot/grub

[root@root??opt]# tar xvf grub.tar??#解壓

注意:在使用絕對路徑名歸檔文件時(shí),將默認(rèn)從文件名中刪除該路徑中前面的/ 符號。這樣解壓時(shí),會(huì)直接解壓到當(dāng)前目錄。 如果不移除/壓縮時(shí),當(dāng)解包時(shí),直接按絕對路徑來釋放,會(huì)覆蓋原系統(tǒng)中此路徑的文件。

例1:指定解壓位置 ?-C

[root@root??opt]# tar xf grub.tar -C /mnt

[root@root??opt]# ls /mnt

boot

[root@root??opt]# ls /mnt/boot/grub/splash.xpm.gz

/mnt/boot/grub/splash.xpm.gz

例2:把兩個(gè)目錄或多個(gè)目錄+文件打包

[root@root??opt]# mkdir dir1 dir2

[root@root??opt]# touch a.txt b.txt c.txt d.txt

[root@root??opt]# ls

a.txt ?b.txt ?c.txt ?dir1 ?dir2 ?d.txt ?grub-2.tar ?grub.tar ?rh

[root@root??opt]# tar cf back.tar a.txt b.txt c.txt dir1 dir2

例3:不解包,查看tar包里面的內(nèi)容

[root@root??opt]# tar tf back.tar

a.txt

b.txt

c.txt

dir1/

dir2/

例4:解壓指定的文件

[root@root??opt]# tar xf etc.tar? etc/kernel/postinst.d/51-dracut-rescue-postinst.sh

注意:后面的路徑一定要跟tar tf查出來的路徑

2.tar歸檔+壓縮:

語法:tar czvf newfile.tar.gz SOURCE

常用參數(shù):

-z, --gzip以gzip方式壓縮 ?擴(kuò)展名: tar.gz

-j : ???????以bz2方式壓縮的 ?擴(kuò)展名:tar.bz2

-J : ???????以xz 方式壓縮 ??擴(kuò)展名:tar.xz

tar cf a.tar a.txt

tar zcf a.tar.gz

tar jcf a.tar.bz2

tar Jcf a.tar.xz

例1:創(chuàng)建tar.gz包

[root@root??~]# tar cvf etc.tar /etc

[root@root??test]# tar zcvf etc.tar.gz /etc??#歸檔,注意備份的名字后綴

[root@root??test]# tar zxvf etc.tar.gz???#解壓縮

例2:創(chuàng)建.tar.bz2包

語法:#tar jcvf newfile.tar.bz2??SOURCE

[root@root??~]# ?tar -jcvf etc.tar.bz2 /etc???

[root@root??~]# ?tar -jxvf etc.tar.bz2 /etc????#解壓縮

[root@root??~]# ?tar jxvf etc.tar.bz2 -C ?/opt ???#解壓到opt目錄下

例3:歸檔的添加和刪除

[root@root??opt]# tar tf a.tar

a.txt

b.txt

c.txt

d.txt

[root@root??opt]# tar rf a.tar hosts

[root@root??opt]# tar tf a.tar

a.txt

b.txt

c.txt

d.txt

hosts

[root@root??opt]# tar --delete -f a.tar a.txt

[root@root??opt]# tar tf a.tar

b.txt

c.txt

d.txt

hosts

例4:創(chuàng)建.tar.xz包

[root@root??~]# ?tar -Jcvf etc.tar.xz /etc

[root@root??~]# ?tar -xvf etc.tar.xz????#tar.xz 這類包,解壓縮

或:

[root@root??~]# ?tar -Jxvf etc.tar.xz??#

對比三種壓縮方式后壓縮比例:

[root@root??~]# ll -h etc.tar*

-rw-r--r-- 1 0 root ??36M 5月 ?10 12:10 etc.tar

-rw-r--r-- 1 0 root ??9.6M 5月 ?10 12:14 etc.tar.bz2 ???#這個(gè)常用

-rw-r--r-- 1 0 root ??12M 5月 ?10 12:11 etc.tar.gz ???#這個(gè)常用

-rw-r--r-- 1 0 root ??7.7M 5月 ?10 12:16 etc.tar.xz ??#這個(gè)壓縮比例最高,壓縮的時(shí)間是最長

3.zip管理壓縮文件

zip軟件包解壓縮命令:

zip是壓縮程序,unzip是解壓程序。

例1:壓縮文件:

[root@root??~]# zip a.zip /etc/passwd?

例2:將所有.jpg的文件壓縮成一個(gè)zip包

[root@root??~]# zip all.zip *.jpg ?例3:壓縮一個(gè)目錄

[root@root??~]# zip -r grub.zip /boot/grub ??#一般不用

解壓縮:

[root@root??~]# unzip grub.zip

[root@root??~]# unzip grub.zip -d /opt/??#??-d ?解壓到指定的目標(biāo)/opt

4.了解gzip-bzip2- xz管理壓縮文件-file-sort查看文件

我們創(chuàng)建壓縮的TAR存檔,TAR命令它支持三種不同的壓縮方式。

gzip壓縮速度最快;

bzip2壓縮生成的文件比gzip小,但使用不如gzip廣;

xz壓縮工具相對較新,但是會(huì)提供最佳的壓縮率

壓縮工具:gzip ?bzip2 ?zip xz

常見的壓縮格式:.gz ?.bz2 ??.xz ?.zip

語法格式:

壓縮

gzip 文件 ?====》 ?gzip a.txt ??=====》 a.txt.gz

bzip2 文件 ===》 bzip2 b.txt ?=====》 b.txt.bz2

xz 文件 ===》xz c.txt ===》c.txt.xz

5.file查看文件

file命令

作用:?file - determine file type??#確定文件類型

注:linux系統(tǒng)不根據(jù)后綴名識別文件類型

用file命令查看文件的類型。

[root@root??opt]# file /etc/passwd

/etc/passwd: ASCII text

[root@root??opt]# file /etc

/etc: directory

[root@root??opt]# file /dev/sda

/dev/sda: block special

6.按一定規(guī)則排序查看文件

查看文件:

[root@root??~]#?ls -ltr ???按時(shí)間排序 ?t 表示時(shí)間, ?-r 從小到大,不加r參數(shù)由大到小

[root@root??~]# ls -lSr ?按大小排序 ?-r 從小到大 ?

[root@root??~]# ls -lSrh ?按大小排序 ?-r 從小到大 ?,加-h 參數(shù),看大小,更清楚

[root@root??~]# ls -lS ??從大到小

查看目錄:

[root@root??~]# du -sh /etc看某個(gè)目錄大小

查看分區(qū)大小:

[root@root??~]# df -h ?可以快速查看磁盤大小的存儲(chǔ)空間

7.排序:處理大量數(shù)據(jù)時(shí)會(huì)用到的命令sort

例1:默認(rèn)按字母規(guī)則進(jìn)行排序

[root@root??~]# cat ?/etc/passwd | sort | more

例2: 按數(shù)據(jù)排序

[root@root??~]# vim file2???#每行隨意寫一些數(shù)字

例2: 按數(shù)據(jù)排序,默認(rèn)從小到大

2

23

231

[root@root??mnt]# sort -n file2 ?#-n默認(rèn)從小到大 ?

[root@root??~]# sort ?-r file2???#-r 反序排序(升序變成降序進(jìn)行排序) 從大小到

例3:支持按月份排序

[root@root??~]# vim ?file3??#寫入以下內(nèi)容

January

March

April

February

[root@root??~]# sort -M file3

April

February

January

March

例4:組合使用

-t 指定一個(gè)用來區(qū)分鍵位置字符

-k 后面跟數(shù)字,指定按第幾列進(jìn)行排序

-r 反序排序(升序變成降序進(jìn)行排序)


[root@root??~]# sort ?-t ":" -nk3?-r?/etc/passwd | more??#按: 做分隔符,以第3列,也就是用戶UID,來從大到小排序

[root@root??~]# du -h ?/etc | sort ?-nr | more??#把etc目錄下所有文件,按從大到小排序

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容