本文爲循序漸進學運維繫列的基礎命令篇,文章已收錄gitee. 在Gitee上,點擊直達 有運維技術的全部技術棧和文檔,免費學習。 打造運維行業最全的知識體系,歡迎star
點贊再看,注意收藏,習慣養成,決勝未來
文章目錄
Linux操作系統中關於查找的命令有很多,今天我們着重討論以下幾個命令的使用:
which 查看可執行文件的位置
whereis 查看可執行文件的位置及相關文件
locate 配合數據庫緩存,快速查看文件位置
grep 過濾匹配,它是一個文件搜索工具
find 查找相關文件
1. which 和whereis 的用法:
關於which和whereis的用法是最簡單的,主要是用來查找可執行文件位置及相關文件-我們來看下案例:
案例:
[root@zmgaosh ~]# which cd
/usr/bin/cd
[root@zmgaosh ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz
[root@zmgaosh ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
2. locate的用法
1) locate介紹
locate命令和find -name 功能差不多,是它的另外一種寫法,但是這個要比find搜索快的多,因爲find命令查找的是具體目錄文件,而locate搜索的是一個數據庫/var/lib/mlocate/mlocate.db,這個數據庫中存有本地所有的文件信息;
這個數據庫是Linux自動創建並每天自動更新維護。
/usr/bin/updatedb 主要用來更新數據庫,通過crontab自動完成的
/usr/bin/locate 查詢文件位置
/etc/updatedb.conf updatedb的配置文件
/var/lib/mlocate/mlocate.db 存放文件信息的文件
2)locate 選項:
-b, --basename match only the base name of path names
-c, --count 只輸出找到的數量
-d, --database DBPATH 使用DBPATH指定的數據庫,而不是默認數據庫 /var/lib/mlocate/mlocate.db
-e, --existing only print entries for currently existing files
-L, --follow follow trailing symbolic links when checking file existence (default)
-h, --help 顯示幫助
-i, --ignore-case 忽略大小寫
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H don't follow trailing symbolic links when checking file existence
-0, --null separate entries with NUL on output
-S, --statistics don't search for entries, print statistics about eachused database
-q, --quiet 安靜模式,不會顯示任何錯誤訊息
-r, --regexp REGEXP 使用基本正則表達式
--regex 使用擴展正則表達式
-s, --stdio ignored, for backward compatibility
-V, --version 顯示版本信息
-w, --wholename match whole path name (default)
3) locate 的安裝
[root@zmgaosh ~]# yum install mlocate && updatedb
[root@zmgaosh ~]# ls 查看本地目錄,有zmedu.txt文件
a.sh a.txt file test zmedu.txt
[root@zmgaosh ~]# locate zmedu.txt #查找zmedu.txt文件
/root/zmedu.txt
[root@zmgaosh ~]# touch zmeduv2.txt #創建zmeduv2.txt
[root@zmgaosh ~]# locate zmeduv2.txt #查找v2但無法找到
[root@zmgaosh ~]# updatedb #更新
[root@zmgaosh ~]# locate zmeduv2.txt #可以看到已經找到了
/root/zmeduv2.txt
[root@zmgaosh ~]#
由上面的案例充分說明, 新增的文件無法locate,只有使用update後才能夠快速查到。
我們來查看下updatedb的配置文件
updatedb的配置文件
[root@zmgaosh ~]# cat /etc/updatedb.conf
PRUNE_BIND_MOUNTS = "yes" #是否限制搜索
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs fuse.glusterfs ceph fuse.ceph" #對哪些後綴的文件排除檢索
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp /var/lib/ceph" #檢索的路徑,即列出的路徑下的文件和子文件夾均跳過不進行檢索
grep的使用
1)grep的作用
過濾,它能夠使用正則表達式來搜索文本,並把結果打印出來
2)常用參數:
-v 取反
-i 忽略大小寫
^# 以#開頭
#$ 以#結尾
^$ 空行
-n 對過濾的內容加上行號
| 或者的意思
3)舉例:
[root@zmgaosh ~]# ps -aux |grep sshd |grep -v grep
root 4700 0.2 0.1 157640 6348 ? Ss 13:55 0:51 sshd: root@pts/0,pts/1
root 9315 0.0 0.1 112920 4312 ? Ss 6月17 0:00 /usr/sbin/sshd -D
這裏的 grep -v grep ,就是指的不顯示grep的查詢語句。
如果不用grep -v grep的結果如下:
[root@zmgaosh ~]# ps -aux |grep sshd
root 4700 0.2 0.1 157640 6348 ? Ss 13:55 0:51 sshd: root@pts/0,pts/1
root 9146 0.0 0.0 112732 968 pts/0 S+ 21:03 0:00 grep --color=auto sshd
root 9315 0.0 0.1 112920 4312 ? Ss 6月17 0:00 /usr/sbin/sshd -D
一般情況下運維排查目前是否有後門賬號的時候可以使用grep
[root@zmgaosh ~]# grep /bin/bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
我們要查詢目前的nologin用戶一共有多少的時候
[root@zmgaosh ~]# grep "nologin" /etc/passwd |wc -l
find命令的使用(重點 )
1)格式
格式:find pathname -options [-print]
命令字 路徑名稱 選項 輸出
2) find命令常用參數
find命令選項:
-name 按照文件名查找文件。 “名稱”
-perm 按照文件權限來查找文件。
-user 按照文件屬主來查找文件
-group 按照文件所屬的組來查找文件
-mtime -n / +n 按照文件的更改時間來查找文件,
- n 表示文件更改時間距現在n天以內
+ n 表示文件更改時間距現在n天以前
-type 查找某一類型的文件
b - 塊設備文件
d - 目錄
c - 字符設備文件
p - 管道文件
l- 符號鏈接文件
f - 普通文件
-size n 查找符合指定的文件大小的文件
-exec 對匹配的文件執行該參數所給出的其他linux命令, 相應命令的形式爲' 命令 {} \;,注意{ }和 \;之間的空格,{}代表查到的內容
3)範例
查找當前目錄下所有的txt文件
[root@zmgaosh ~]# find . -name "*.txt"
./zmedu.txt
./a.txt
./zmeduv2.txt
按照更改時間或訪問時間等查找文件
mtime: 文件最後一次修改的時間
atime: 最後一次訪問時間
ctime: 文件的最後一次變化時間,也就是修改時間
舉例: 查找root目錄下更改時間在1天之內的且被修改過的文件
[root@zmgaosh ~]# find /root/ -mtime -1
/root/
/root/.viminfo
/root/a.sh
/root/zmedu.txt
/root/.mysql_history
/root/a.txt
/root/test
/root/.bash_history
/root/zmeduv2.txt
/root/file
[root@zmgaosh ~]#
查找並執行相應的命令
使用exec選項
[root@zmgaosh ~]# touch {1,2,3}.bak #創建三個文件
[root@zmgaosh ~]# ls
1.bak 3.bak a.txt test zmeduv2.txt
2.bak a.sh file zmedu.txt
[root@zmgaosh ~]# find . -name "*.bak" -exec rm {} \; #查找以.bak結尾的文件並刪除
[root@zmgaosh ~]# ls
a.sh a.txt file test zmedu.txt zmeduv2.txt
查找一個目錄下的制定文件並拷貝到另外一個目錄
[root@zmgaosh ~]# ls
a.sh a.txt file test zmedu.txt zmeduv2.txt
[root@zmgaosh ~]# find . -name "*.txt" -exec cp {} /opt \;
[root@zmgaosh ~]# ls /opt/
a.txt zmedu.txt zmeduv2.txt
查找多個類型文件
比較符的使用:
-a and 並且
-o or 或者
+ 超過
- 低於
查找/etc/下文件大小爲大於20K 小於50K的文件,並統計個數
[root@zmgaosh ~]# find /etc -size +20k -a -size -50k |wc -l
17
按權限查找 -perm
查找/etc權限爲755的文件或者目錄
[root@zmgaosh ~]# find /etc -perm 755 |wc -l
232
總結:
查找命令中,find和grep是用的最多的命令,在面試中也經常出現
後面我會單獨寫一篇文章,主要筆試題經常出現的find和grep試題