1. 程式人生 > 其它 >查詢、壓縮、管道

查詢、壓縮、管道

1、查詢檔案

find命令

在linux系統中,按照我們的要求去查詢檔案。

格式:

find [查詢的路徑] [匹配模式] [匹配規則]

匹配模式:

-name : 按照名字去匹配

正則匹配:

* 匹配任意數量的任意字元(匹配零個或多個任意字元)
?: 匹配任意一個字元

-type : 按照檔案的型別匹配

常見的檔案型別:

f : 普通檔案
d : 普通資料夾
l : 連結檔案
c : 字元裝置檔案
b : 塊裝置檔案
s : socket檔案

-perm : 按照檔案的許可權來查詢

常見的檔案許可權:

755 資料夾的預設許可權
644 : 檔案的預設許可權

-user : 按照檔案的屬主來查詢
-nouser :查詢使用者被刪除了的檔案

-group : 按照檔案的屬組來查詢
-nogroup : 查詢沒有陣列的檔案

知識儲備:

刪除使用者:userdel [使用者名稱]
刪除使用者組:groupdel [屬組名]

-mtime : 按照修改檔案的時間來查詢

+ 查詢某個時間段之前的資料
- 查詢某個時間段之內的資料

-size : 按照檔案的大小來查詢

+ 查詢超過n的檔案
- 查詢小於n的檔案

知識儲備:

stat : 檢視檔案的各種時間

-ctime : 按照檔案的建立時間來查詢
-atime : 按照訪問時間來查詢檔案

-a(預設) : 並且

-o :或者

-exec(xargs) : 處理匹配之後的內容

案例1:查詢出/etc目錄下的hosts檔案

[root@localhost ~]# find /etc -name "hosts"
/etc/hosts

案例2:查詢出/etc目錄下的以ifcfg開頭的檔案

[root@localhost ~]# find /etc/ -name "ifcfg*"

案例3:查詢出/etc目錄下以.conf結尾的檔案

[root@localhost ~]# find /etc/ -name "*.conf"

案例4:查詢出/etc目錄下,檔名中包含host的檔案有哪些

[root@localhost ~]# find /etc/ -name "*host*"

案例5:查詢出/etc目錄下,所有的普通檔案。

[root@localhost ~]# find /etc/ -type f

案例6:查詢出/etc目錄下,所有的資料夾

[root@localhost ~]# find /etc/ -type d

案例7:查詢出/dev/目錄中的所有的塊裝置檔案

[root@localhost ~]# find /dev/ -type b

案例8:查詢出/dev目錄中所有的字元裝置檔案

[root@localhost ~]# find /dev/ -type c

案例9:查詢出/etc目錄中檔名包含nginx的普通檔案

[root@localhost ~]# find /etc/ -name "*nginx*" -type f

案例10:查詢出
/root目錄下,許可權為755的檔案

[root@localhost ~]# find /root/ -perm 755

案例11:查詢出屬主為test01的檔案

[root@localhost ~]# find /root/ -user test01

案例12:查詢屬主被刪除了的檔案

[root@localhost ~]# find /root/ -nouser

案例13:查詢屬組為test的檔案

[root@localhost ~]# find /root/ -group test

案例14:查詢屬組被刪除了的檔案
[root@localhost ~]# find /root/ -nogroup


案例15:查詢2天以前修改過的檔案

[root@localhost ~]# find /root/ -mtime +2

案例16:查詢2天以內建立的檔案

[root@localhost ~]# find /root/ -ctime -2

案例17:查詢2天以內訪問過的檔案

[root@localhost ~]# find /root/ -atime -2

案例18:查詢大於1M的檔案

[root@localhost ~]# find /root/ -size +1M

案例19:查詢小於1M的檔案

[root@localhost ~]# find /root/ -size -1M

案例20:查詢在3天以內建立的檔案,並刪除


[root@localhost tmp]# find /tmp/ -mtime -3 -type f -exec rm -rf {} \;

-exec處理查詢之後的內容
{}代表的是查詢到的內容、
\; : 固定搭配

知識儲備:
xargs :將所有的內容格式化成一行

[root@localhost tmp]# find /tmp/ -mtime -3 -type f | xargs -I {} rm -rf {}

練習1:要求將所有3天前建立的普通檔案加上.bak字尾

[root@localhost tmp]# find /tmp -ctime -3 -type f -exec mv {} {}.bak \;

[root@localhost tmp]# find /tmp/ -type f -ctime -3 | xargs -I {} mv {} {}.bak

2、Linux系統壓縮包

gzip:

壓縮軟體,將檔案做成一個壓縮包,會刪除原來的檔案,生成一個新的壓縮包檔案。

格式:
壓縮:gzip [檔案路徑]

解壓:gzip -d [壓縮包路徑]
bzip:

壓縮軟體,將檔案做成一個壓縮包,會刪除原來的檔案,生成一個新的壓縮包檔案。
格式:
壓縮:bzip2 [檔案路徑]

解壓:bzip2 -d [壓縮包路徑]
缺陷:

bzip2不能壓縮目錄。

gzip bzip2 壓縮率更大

tar:
打包檔案,不會刪除原檔案,也不會壓縮檔案;tar命令是可以跟gzip或者bzip2共同使用

格式:

tar [引數] 壓縮包名稱

引數:

-f : 指定壓縮包名稱

-c : 打包檔案

-z : 指定使用gzip壓縮,一般使用gzip壓縮的檔案都以.tar.gz作為副檔名

-j : 指定使用bzip2壓縮,一般使用bzip2壓縮的檔案都以.tar.bz2作為副檔名

-v : 顯示壓縮包壓縮的過程

-x : 解壓,不需要指定壓縮包的壓縮型別,它會自動匹配壓縮包的型別自行解壓。

-P :當壓縮包中存在根目錄是,自動移除根目錄

-t : 檢視壓縮包中的內容

練習1:將/etc目錄中的所有的普通檔案壓縮成/tmp/etc.tar.gz檔案

[root@localhost tmp]# tar -czvPf /tmp/etc.tar.gz $(find /etc/ -type f | xargs)

知識儲備:

$() : 相當於數學當中的()

(1 + 2) * 3

3、linux中的管道

用於傳輸資料,可以將前一個命令的執行結果,交給管道之後的命令去處理

格式:

|

案例1:刪除/tmp目錄下,一天以內建立的檔案

[root@localhost tmp]# find /tmp/ -ctime -1 -type f | xargs -I {} rm -rf {}

案例2:將etc中所有的普通檔案,複製到/tmp目錄中

[root@localhost tmp]# find /etc/ -type f | xargs -I {} cp -r {} /tmp/