Linux-經常使用命令

經常使用命令集合

文件查找 find、locate

find

find: 文件查找,針對文件名,精確查找,磁盤搜索,io讀寫,cpu開銷大linux

find [options] [path...] [expression] [action]正則表達式

b - 塊設備文件。
d - 目錄。
c - 字符設備文件。
p - 管道文件。
l - 符號連接文件。
f - 普通文件。
s - socket文件
-size n[cwbkMG] : 文件大小 爲 n 個由後綴決定的數據塊。其中後綴爲:
b: 表明 512 位元組的區塊(若是用戶沒有指定後綴,則默認爲 b)
c: 表示字節數
k: 表示 kilo bytes (1024字節)
w: 字 (2字節)
M:兆字節(1048576字節)
G: 千兆字節 (1073741824字節)
-depth 在查找文件時,首先查找當前目錄中的文件,而後再在其子目錄中查找。
-maxdepth 查找最大目錄層數 如 1,即只查找一層目錄
-fstype 查找位於某一類型文件系統中的文件,這些文件系統類型一般能夠在配置文件
/etc/fstab中找到,該配置文件中包含了本系統中有關文件系統的信息。
-mount 在查找文件時不跨越文件系統mount點。
-follow 若是find命令遇到符號連接文件,就跟蹤至連接所指向的文件。
-cpio 對匹配的文件使用cpio命令,將這些文件備份到磁帶設備中。
-and 條件與
-or 條件或

===expression===算法

按文件名:

[root@localhost ~]# find /etc -name "ifcfg-eth0"
[root@localhost ~]# find /etc -iname "ifcfg-eth0"            //-i忽略大小寫
[root@localhost ~]# find /etc -iname "ifcfg-eth*"

按文件大小:

[root@localhost ~]# find /etc -size +5M                        //大於5M
[root@localhost ~]# find /etc -size 5M
[root@localhost ~]# find /etc -size -5M
[root@localhost ~]# find /etc -size +5M -ls                    //-ls找到的處理動做

指定查找的目錄深度:

-maxdepth levels
-mindepth levels
[root@localhost ~]# find / -maxdepth 4 -a  -name "ifcfg-eth0"

按時間找(atime,mtime,ctime):

[root@localhost ~]# find /etc -mtime +5                      //修改時間超過5天
[root@localhost ~]# find /etc -mtime 5                       //修改時間等於5天
[root@localhost ~]# find /etc -mtime -5                      //修改時間5天之內

按文件屬主、屬組找:

[root@localhost ~]# find /home -user jack                    //屬主是jack的文件
[root@localhost ~]# find /home -group hr                     //屬組是hr組的文件
[root@localhost ~]# find /home -user jack -group hr
[root@localhost ~]# find /home -user jack -a -group hr
[root@localhost ~]# find /home -user jack -o -group hr

[root@localhost ~]# find /home -nouser
[root@localhost ~]# find /home -nogroup
[root@localhost ~]# find /home -nouser -o -nogroup 

按文件類型:

[root@localhost ~]# find /dev -type f                           //f普通
[root@localhost ~]# find /dev -type d                           //d目錄
[root@localhost ~]# find /dev -type l                           //l連接
[root@localhost ~]# find /dev -type b                           //b塊設備
[root@localhost ~]# find /dev -type c                           //c字符設備
[root@localhost ~]# find /dev -type s                           //s套接字
[root@localhost ~]# find /dev -type p                           //p管道文件

按邏輯查找:

[root@localhost ~]# find ./ -name '*.py' -and  -name '1*'                      //查找已py結尾而且已1開頭的文件
[root@localhost ~]# find ./ -name '*.py' -a  -name '1*'                        //上面的-and能夠簡寫爲-a
[root@localhost ~]# find / -name 'passwd' -or -name 'group'                    //查找名字包含passwd 或者 包含group的文件
[root@localhost ~]# find / -name 'passwd' -o -name 'group'                     //一樣-or能夠簡寫爲-o
[root@localhost ~]# find ./ -name '*.log' -and -ctime +5 |xargs rm -rf {}\;    //查找已.log結尾而且建立時間在5天之前的文件,找到後刪除

按文件權限:

[root@localhost ~]# find . -perm 644 -ls 
[root@localhost ~]# find . -perm -644 -ls
[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find . -perm -222 -ls                            //全局可寫
[root@localhost ~]# find /usr/bin /usr/sbin -perm -4000 -ls          //包含set uid
[root@localhost ~]# find /usr/bin /usr/sbin -perm -2000 -ls          //包含set gid
[root@localhost ~]# find /usr/bin /usr/sbin -perm -1000 -ls          //包含sticky

按正則表達式:

-regex pattern
[root@localhost ~]# find /etc  -regex  '.*ifcfg-eth[0-9]'
.*       任意多個字符
[0-9]  任意一個數字

[root@localhost ~]# find /etc -regex '.*ifcfg-enp0s25'
/etc/sysconfig/network-scripts/ifcfg-enp0s25

[root@localhost ~]# find /etc -regex '.*ifcfg-enp0s[0-9]+'
/etc/sysconfig/network-scripts/ifcfg-enp0s25

==找到後處理的動做 ACTIONS: (默認動做-print)==shell

-print
-ls
-delete
-exec 後面跟自定義的shell命令
-ok 後面跟自定義的shell命令數據庫

[root@localhost ~]# find /etc -name "ifcfg*"
[root@localhost ~]# find /etc -name "ifcfg*" -print
[root@localhost ~]# find /etc -name "ifcfg*" -ls
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;

[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
[root@localhost ~]# find /etc -name "ifcfg*" -delete

擴展知識:find結合xargs
[root@localhost ~]# find . -name "yang*.txt" |xargs rm -rf         
[root@localhost ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp

find練習

1. 將/etc/中的全部目錄(僅目錄)複製到/tmp下,目錄結構不變
# find /etc -type d -exec mkdir /tmp/{} \;


2. 將/etc目錄複製到/var/tmp//var/tmp/etc中的全部目錄設置權限777(僅目錄)
    將/var/tmp/etc中全部文件權限設置爲666
# cp -rf /etc /var/tmp/ 
# chmod -R a=rwX /var/tmp/etc/
或者
find /var/tmp/etc/ -type d -exec chmod 777 {} \;         //分號是找到一個設置一個權限
find /var/tmp/etc/ -type d -exec chmod 777 {} \+        //加號是統一找到後設置權限
find /var/tmp/etc/ ! -type d -exec chmod 777 {} \+ 

3. 如下命令的區別是什麼?
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \+
[root@localhost ~]# mkdir dir1
[root@localhost ~]# touch dir1/file{1..20}

[root@localhost ~]# find /root/dir1 -name "file5"
[root@localhost ~]# find /root/dir1 ! -name "file5"

[root@localhost ~]# find /root/dir1 -name "file5" -o -name "file9" 
/root/dir1/file5
/root/dir1/file9

[root@localhost ~]# find /root/dir1 -name "file5" -o -name "file9" -ls
1466515    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file9

[root@localhost ~]# find /root/dir1 -name "file5" -ls  -o -name "file9" -ls
1466499    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file5
1466515    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file9

[root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -ls
1466499    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file5
1466515    0 -rw-r--r--   1 root     root            0 6月  5 11:15 /root/dir1/file9

[root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
removed ‘/root/dir1/file5’
removed ‘/root/dir1/file9’

 locate

(查詢的數據庫: /var/lib/mlocate/mlocate.db)  express

計劃任務:天天自動更新數據庫 /etc/cron.daily/mlocate.cron
手動更新數據庫:updatedbvim

# locate ifcfg-eth0
# locate ifcfg-enp0s25

文件過濾 grep

 grep工具:行過濾windows

OPTIONS:
    -i: 不區分大小寫
    -v: 查找不包含指定內容的行,反向選擇
    -w: 按單詞搜索
    -c: 統計匹配到的次數
    -n: 顯示行號
    -r: 逐層遍歷目錄查找
    -A: 顯示匹配行及前面多少行
    -B: 顯示匹配行及後面多少行
    -C: 顯示匹配行先後多少行
    --color=auto :能夠將找到的關鍵詞部分加上顏色的顯示
    -l:只列出匹配的文件名
    -L:列出不匹配的文件名
    -e: 使用正則搜索
    ^key:以什麼開頭
    key$:以什麼結尾

每次過濾出來的內容顯示顏色:bash

vim ~/.bashrc
alias grep='grep --color=auto'
source ~/.bashrc

 使用方法 >>參考socket

文件打包壓縮 

打包壓縮--tar

 tar 建議針對目錄,打包壓縮多個文件,不會改變文件的屬性和權限

用法:

tar optino 打包壓縮後的文件,須要打包壓縮的文件

選項:

-c         建立tar包
-f         指定包名
-v         顯示詳細信息
-z         使用gzip工具壓縮
-j         使用bzip2工具壓縮
-J         使用xz工具壓縮
-t         查看tar包內容
-x         解壓tar包
-C         指定解壓路徑
-r         追加文件到tar包
說明:
參數前面的「-」無關緊要
# tar -cvf /tmp/DIR.tar dir/        # 將dir目錄打包放在/tmp下取名叫DIR.tar
# tar -tf /tmp/DIR.tar            # 查看DIR.tar裏面的內容
# tar -r /etc/hosts -f /tmp/DIR.tar    # 追加hosts文件到DIR.tar包裏
# tar -tf /tmp/DIR.tar 
# tar -r inittab -f /tmp/DIR.tar 
# tar -tf /tmp/DIR.tar 
# tar -xf /tmp/DIR.tar -C backup/
# tar cvzf /tmp/$(date +%F).tar.gz backup/ dir/ test.gz 
# tar -xf /tmp/2017-07-18.tar.gz -C /tmp/aaa/

注意:

一、通常狀況下,將-f參數放到全部參數的最後面

二、若是往tar包裏面追加內容,那麼儘量寫相對路徑

示例:

查閱上述tar包內有哪些文件:

# tar -ztvf log.tar.gz

因爲咱們使用 gzip 壓縮的log.tar.gz,因此要查閱log.tar.gz包內的文件時,就得要加上z這個選項了。

將tar包解壓縮:
# tar -zxvf /opt/soft/test/log.tar.gz

在預設的狀況下,咱們能夠將壓縮檔在任何地方解開的

只將tar內的部分文件解壓出來:
# tar -zxvf /opt/soft/test/log30.tar.gz log2013.log

我能夠透過tar -ztvf來查閱 tar 包內的文件名稱,若是單隻要一個文件,就能夠透過這個方式來解壓部分文件!

文件備份下來,而且保存其權限:

# tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log

這個-p的屬性是很重要的,尤爲是當您要保留本來文件的屬性時。

在文件夾當中,比某個日期新的文件才備份:

# tar -N "2012/11/13" -zcvf log17.tar.gz test

備份文件夾內容是排除部分文件:

# tar --exclude scf/service -zcvf scf.tar.gz scf/*

打包壓縮--zip

zip兼容unix和windows,能夠壓縮多個文件或目錄

用法:

壓縮

zip [option] 壓縮後的文件 須要壓縮的文件(能夠多個)

解壓縮

unzip  須要解壓的文件

unzip -d path  須要解壓的文件

選項

zip 命令:是一個應用普遍的跨平臺的壓縮工具,壓縮文件的後綴爲 zip文件

-A 自動解壓文件
-c 給壓縮文件加註釋
-d 刪除文件
-F 修復損壞文件
-k 兼容 DOS
-m 壓縮完畢後,刪除源文件
-q 運行時不顯示信息處理信息
-r 處理指定目錄和指定目錄下的使用子目錄
-v 顯示信息的處理信息
-x 「文件列表」 壓縮時排除文件列表中指定的文件
-y 保留符號連接
-b<目錄> 指定壓縮到的目錄
-i<格式> 匹配格式進行壓縮
-L 顯示版權信息
-t<日期> 指定壓縮文件的日期
-<壓縮率> 指定壓縮率

練習:

一、將/boot、/etc目錄下全部的文件壓縮到/tmp目錄裏,叫20170718.zip
# zip -r /tmp/$(date +%Y%m%d).zip /boot /etc

二、將20170718.zip文件解壓到指定目錄/backup裏
# mkdir /backup
# unzip /tmp/20170718.zip -d /backup
# zip /tmp/$(date +%F).dir dir/
# unzip /tmp/2017-07-18.dir -d backup/
# zip -m myfile.zip ./rpm_info.txt      #向壓縮文件中myfile.zip中添加rpm_info.txt文件

# zip -r filename.zip file1 file2 file3 /usr/work/school     #多個文件或目錄,

# -x  排除指定文件的運用;壓縮當前文件全部內容,出了 images和upload目錄下的全部文件
# zip -r back.zip ./* -x "./images/*" -x "./upload/*"

打包壓縮--gzip

gzip 壓縮速度快,壓縮率低,CPU開銷比較低

用法:

壓縮

gzip 須要壓縮的文件 [file1 file2 ...]

解壓縮

gzip -d 須要解壓的文件

gunzip 須要解壓的文件

注意:

 保留原文件須要加-r選項

選項

 -a或--ascii  使用ASCII文字模式。 
 -c或--stdout或--to-stdout  把解壓後的文件輸出到標準輸出設備。 
 -f或-force  強行解開壓縮文件,不理會文件名稱或硬鏈接是否存在以及該文件是否爲符號鏈接。 
 -h或--help  在線幫助。 
 -l或--list  列出壓縮文件的相關信息。 
 -L或--license  顯示版本與版權信息。 
 -n或--no-name  解壓縮時,若壓縮文件內含有遠來的文件名稱及時間戳記,則將其忽略不予處理。 
 -N或--name  解壓縮時,若壓縮文件內含有原來的文件名稱及時間戳記,則將其回存到解開的文件上。 
 -q或--quiet  不顯示警告信息。 
 -r或--recursive  遞歸處理,將指定目錄下的全部文件及子目錄一併處理。 
 -S<壓縮字尾字符串>或--suffix<壓縮字尾字符串>  更改壓縮字尾字符串。 
 -t或--test  測試壓縮文件是否正確無誤。 
 -v或--verbose  顯示指令執行過程。 

 -V或--version 顯示版本信息。
# tar cf test.tar -R test   #gzip不能壓縮目錄,先打包

# gzip test.tar             #壓縮

# gzip -l test.tar.gz       #查看壓縮包中的內容

# gzip -dv test.tar.gz     #解壓


# gzip -rv /var/www    #遞歸的壓縮目錄
# gzip -dr test6     # 遞歸地解壓目錄

打包壓縮--bzip2

bzip2 壓縮速度慢 壓縮率高 CPU開銷大

用法:

壓縮

bzip2 須要壓縮的文件

解壓縮

bzip2 -d 須要解壓的文件

bunzip2 須要解壓的文件

選項

-c --stdout
    將數據壓縮或解壓縮至標準輸出。
-d --decompress
    強制解壓縮。 bzip2, bunzip2 以及 bzcat 其實是同一個程序,進行何種操做將根據程序名肯定。 指定該選項後將不考慮這一機制,強制 bzip2 進行解壓縮。
-z --compress
-d 選項的補充:強制進行壓縮操做,而無論執行的是哪一個程序。
-t --test
    檢查指定文件的完整性,但並不對其解壓縮。 實際上將對數據進行實驗性的解壓縮操做,而不輸出結果。
-f --force
    強制覆蓋輸出文件。一般 bzip2 不會覆蓋已經存在的文件。該選項還強制 bzip2 打破文件的硬鏈接,缺省狀況下 bzip2 不會這麼作。
-k --keep
    在壓縮或解壓縮時保留輸入文件(不刪除這些文件)。
-s --small
    在壓縮、 解壓縮及檢查時減小內存用量。 採用一種修正的算法進行壓縮和測試, 每一個數據塊僅須要 2.5 個字節。 這意味着任何文件均可以在 2300k 的內存中進行解壓縮, 儘管速度只有一般狀況下的一半。
    在壓縮時,-s將選定 200k 的塊長度,內存用量也限制在 200k 左右, 代價是壓縮率會下降。 總之,若是機器的內存較少(8兆字節或更少), 可對全部操做都採用-s選項。參見下面的內存管理。
-q --quiet
    壓制不重要的警告信息。屬於 I/O 錯誤及其它嚴重事件的信息將不會被壓制。
-v --verbose
    詳盡模式 -- 顯示每一個被處理文件的壓縮率。 命令行中更多的 -v 選項將增長詳細的程度, 使 bzip2 顯示出許多主要用於診斷目的信息。
-L --license -V --version
    顯示軟件版本,許可證條款及條件。
-1 to -9
    在壓縮時將塊長度設爲 100 k、200 k .. 900 k。 對解壓縮沒有影響。參見下面的內存管理。
--
    將全部後面的命令行變量看做文件名,即便這些變量以減號"-"打頭。 可用這一選項處理以減號"-"打頭的文件名, 例如:bzip2 -- -myfilename.
--repetitive-fast --repetitive-best
    這些選項在 0.9.5 及其以上版本中是多餘的。 在較早的版本中,這兩個選項對排序算法 的行爲提供了一些粗糙的控制,有些狀況下頗有用。 0.9.5 及其以上版本採用了改進的算法而與這些選項無關
# bzip2 -z abc.sh           #壓縮

# bzip2 -kv abc.sh          #壓縮原文保留

# bzip2 -9 -c abc.sh >abc.bz2       #壓縮原文保留

# bzip2 -tv test.bz2     # 模擬解壓

# bzip2 -k test          # 生成新文件,原文件也保留

# bzip2 -dc test.bz2     # 解壓到標準輸出

打包壓縮--xz

xz 高壓縮率, 解壓速度快 可是壓縮時間較長,CPU開銷相對較大

用法:

壓縮

xz 須要壓縮的文件

解壓縮

xz -d 須要解壓的文件

unxz 須要解壓的文件

 

 

 

 

 未完....            待續