文件的查找目前使用的主要有以下幾個命令:
| 命令 | 說明 |
|---|---|
| which | 查看可執(zhí)行文件的位置 |
| whereis | 查看可執(zhí)行文件的位置及相關(guān)文件 |
| locate | 配合數(shù)據(jù)庫緩存,快速查看文件位置 |
| grep | 過濾匹配,它是一個文件搜索工具 |
| find | 查找相關(guān)文件 |
1 which
which命令的作用是,在PATH變量指定的路徑中,搜索某個系統(tǒng)命令的位置,并且返回第一個搜索結(jié)果。也就是說,使用which命令,就可以看到某個系統(tǒng)命令是否存在,以及執(zhí)行的到底是哪一個位置的命令。
[root@virtue ~]# which cd
/usr/bin/cd
[root@virtue ~]# type cd
cd 是 shell 內(nèi)嵌
[root@virtue ~]# which squid
/usr/bin/which: no squid in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
*centos7中并沒有出現(xiàn)內(nèi)嵌命令無法查找的情況
2 whereis
whereis命令只能用于程序名的搜索,而且只搜索二進(jìn)制文件(參數(shù)-b)、man說明文件(參數(shù)-m)和源代碼文件(參數(shù)-s)。如果省略參數(shù),則返回所有信息。
whereis命令會查找一個記錄系統(tǒng)內(nèi)文件信息的數(shù)據(jù)庫文件,所以速度會很快。缺點是數(shù)據(jù)庫文件并不是實時更新,默認(rèn)情況一星期更新一次,因此如果最近刪除的文件或者創(chuàng)建的文件是有可能錯誤的顯示的。為了防止這種情況,可以手動的強(qiáng)制更新數(shù)據(jù)庫文件。
命令參數(shù)
-b 定位可執(zhí)行文件。
-m 定位幫助文件。
-s 定位源代碼文件。
-u 搜索默認(rèn)路徑下除可執(zhí)行文件、源代碼文件、幫助文件以外的其它文件。
-B 指定搜索可執(zhí)行文件的路徑。
-M 指定搜索幫助文件的路徑。
-S 指定搜索源代碼文件的路徑。
普通查找
[root@virtue ~]# whereis svn
svn:
[root@virtue ~]# whereis git
git: /usr/bin/git /usr/share/man/man1/git.1.gz
查找二進(jìn)制文件
[root@virtue ~]# whereis -b git
git: /usr/bin/git
查找?guī)椭募?/h6>
[root@virtue ~]# whereis -m git
git: /usr/share/man/man1/git.1.gz
3 locate
[root@virtue ~]# whereis -m git
git: /usr/share/man/man1/git.1.gz
locate可以快速的找到文件的位置,因為locate查找的是數(shù)據(jù)庫的文件來確定文件的位置,而并不是深入各個文件系統(tǒng)查找。一般情況下,數(shù)據(jù)庫文件通過corntab自動更新,通常每天更新一次。
命令參數(shù)
| 命令 | 說明 |
|---|---|
| -e | 將排除在尋找的范圍之外。 |
| -1 | 如果是 1 則啟動安全模式。無法查看權(quán)限不足的文件。該模式會降低速度,因為需要實際的去檔案系統(tǒng)中獲得權(quán)限資料。 |
| -f | 將特定的檔案系統(tǒng)排除在外。 |
| -q | 安靜模式,不會顯示任何錯誤訊息。 |
| -n | 至多顯示 n個輸出。 |
| -r | 使用正規(guī)運(yùn)算式做尋找的條件。 |
| -o | 指定資料庫存的名稱。 |
| -d | 指定資料庫的路徑 |
普通查找
查找內(nèi)容: /etc下說有以sh開頭的文件
[root@virtue ~]# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
手動更新數(shù)據(jù)庫
[root@virtue ~]# updatedb
4 grep
grep命令是用來過濾文件內(nèi)容的命令,它能夠使用正則表達(dá)式來搜索文本,并把結(jié)果打印出來
命令參數(shù)
| 命令 | 說明 |
|---|---|
| v | 取反 |
| -i | 忽略大小寫 |
| ^# | 以#開頭 |
| #$ | 以#結(jié)尾 |
| ^$ | 空行 |
| -n | 對過濾的內(nèi)容加上行號 |
| | | 或者的意思 |
普通過濾
[root@virtue ~]# ps aux | grep sshd | grep -v grep
root 3175 0.0 0.0 145720 5364 ? Ss 11:03 0:03 sshd: root@pts/0
root 24841 0.0 0.0 105872 4080 ? Ss 2017 0:09 /usr/sbin/sshd -D
過濾內(nèi)容添加行號
[root@virtue ~]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
多重條件過濾內(nèi)容
[root@virtue ~]# grep "nologin\|root" /etc/passwd | wc -l
18
[root@virtue ~]# egrep "nologin|root" /etc/passwd | wc -l
18
5 find
Linux下find命令在目錄結(jié)構(gòu)中搜索文件,并執(zhí)行指定的操作。即使系統(tǒng)中含有網(wǎng)絡(luò)文件系統(tǒng)( NFS),只你具有相應(yīng)的權(quán)限,find命令在該文件系統(tǒng)中同樣有效。 在運(yùn)行一個非常消耗資源的find命令時,很多人都傾向于把它放在后臺執(zhí)行,因為遍歷一個大的文件系統(tǒng)可能會花費(fèi)很長的時間(這里是指30G字節(jié)以上的文件系統(tǒng))。
命令參數(shù)
| 命令參數(shù) | 說明 |
|---|---|
| pathname | find命令所查找的目錄路徑。例如用.來表示當(dāng)前目錄,用/來表示系統(tǒng)根目錄。 |
| find命令將匹配的文件輸出到標(biāo)準(zhǔn)輸出。 | |
| -print0 | 與xargs配套使用,以“\0”做為定界符。 |
| -exec | find命令對匹配的文件執(zhí)行該參數(shù)所給出的shell命令。相應(yīng)命令的形式為'command' { } \;,注意{ }和\;之間的空格。 |
| -ok | 和-exec的作用相同,只不過以一種更為安全的模式來執(zhí)行該參數(shù)所給出的shell命令,在執(zhí)行每一個命令之前,都會給出提示,讓用戶來確定是否執(zhí)行。 |
命令選項
| 命令選項 | 說明 |
|---|---|
| -name | 按照文件名查找文件。 |
| -perm | 按照文件權(quán)限來查找文件。 |
| -prune | 使用這一選項可以使find命令不在當(dāng)前指定的目錄中查找,如果同時使用-depth選項,那么-prune將被find命令忽略。 |
| -depth | 在查找文件時,首先查找當(dāng)前目錄中的文件,然后再在其子目錄中查找。 |
| -user | 按照文件屬主來查找文件。 |
| -nouser | 查找無有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。 |
| -group | 按照文件所屬的組來查找文件。 |
| -nogroup | 查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。 |
| -mtime -n/+n | 按照文件的更改時間來查找文件,-n表示文件更改時間距現(xiàn)在n天以內(nèi),+n表示文件更改時間距現(xiàn)在n天以前。 |
| -type | 查找某一類型的文件,諸如:b - 塊設(shè)備文件/d - 目錄/c - 字符設(shè)備文件/p - 管道文件/l - 符號鏈接文件/f - 普通文件 |
| -newer file1 ! file2 | 查找更改時間比文件file1新但比文件file2舊的文件。 |
| -size n [c] | 查找文件長度為n塊的文件,帶有c時表示文件長度以字節(jié)計。 |
| -fstype | 查找位于某一類型文件系統(tǒng)中的文件,這些文件系統(tǒng)類型通??梢栽谂渲梦募?etc/fstab中找到,該配置文件中包含了本系統(tǒng)中有關(guān)文件系統(tǒng)的信息。 |
| -mount | 在查找文件時不跨越文件系統(tǒng)mount點。 |
| -follow | 如果find命令遇到符號鏈接文件,就跟蹤至鏈接所指向的文件。 |
| -cpio | 對匹配的文件使用cpio命令,將這些文件備份到磁帶設(shè)備中。 |
關(guān)于訪問時間的參數(shù)具體如下所示:
| 命令選項 | 說明 |
|---|---|
| -amin n | 查找系統(tǒng)中最后N分鐘訪問的文件 |
| -atime n | 查找系統(tǒng)中最后n*24小時訪問的文件 |
| -cmin n | 查找系統(tǒng)中最后N分鐘被改變文件狀態(tài)的文件 |
| -ctime n | 查找系統(tǒng)中最后n*24小時被改變文件狀態(tài)的文件 |
| -mmin n | 查找系統(tǒng)中最后N分鐘被改變文件數(shù)據(jù)的文件 |
| -mtime n | 查找系統(tǒng)中最后n*24小時被改變文件數(shù)據(jù)的文件 |
find命令應(yīng)用實例
# 按名稱查找
[root@virtue ~]# find . -name "*.asm"
./Python-3.6.1/Modules/_decimal/libmpdec/vcdiv64.asm
./Python-3.6.1/Modules/_ctypes/libffi_msvc/win64.asm
./hello.asm
# 按時間進(jìn)行查找
[root@virtue log]# find . -mtime +100
./ppp
./audit
[root@virtue log]# find . -mtime -2
./lastlog
./wtmp
./audit/audit.log
./yum.log
./cron
./messages
./secure
# 按照權(quán)限查找
[root@virtue log]# find /root -perm 777
/root/Python-3.6.1/build/temp.linux-x86_64-3.6/libffi/include/ffitarget.h
/root/Python-3.6.1/build/temp.linux-x86_64-3.6/libffi/include/ffi_common.h
/root/.vim/bundle/sparkup/TextMate/Sparkup.tmbundle/Support/sparkup.py
/root/.vim/bundle/sparkup/ftplugin
/root/.vim/bundle/sparkup/vim/ftplugin/html/sparkup.py
/root/.vim/bundle/sparkup/vim/ftplugin/htmldjango
/root/.vim/bundle/sparkup/vim/ftplugin/smarty
/root/.vim/bundle/sparkup/vim/ftplugin/xml
# 按照用戶/屬組查找
[root@virtue tsa]# find . -user peter
./12.txt
./1.txt
./backup.sh
./sendmail.py
[root@virtue tsa]# find . -group peter
./12.txt
./1.txt
./backup.sh
./sendmail.py
[root@virtue tsa]# userdel -r peter
[root@virtue tsa]# find . -nouser
./12.txt
./1.txt
./backup.sh
./sendmail.py
[root@virtue tsa]# find . -nogroup
./12.txt
./1.txt
./backup.sh
./sendmail.py
# 按照文件類型查找
[root@virtue tsa]# find . -type d
.
./a12
./a39
[root@virtue tsa]# find . -type f
./12.txt
./1.txt
./backup.sh
./sendmail.py
./a12/12.txt
./a12/summary.md
./a39/detail.html
./a39/style.css
./a39/script.js
# 查找后指定命令
[root@virtue tsa]# find . -nogroup -ls
36783493 4 -rw-rw-r-- 1 1001 1001 26 1月 11 16:27 ./12.txt
36783494 4 -rw-rw-r-- 1 1001 1001 9 1月 11 16:26 ./1.txt
36783495 4 -rw-rw-r-- 1 1001 1001 26 1月 11 16:26 ./backup.sh
36780031 0 -rw-rw-r-- 1 1001 1001 0 1月 11 16:24 ./sendmail.py
[root@virtue tsa]# find . -nogroup -ls
36783493 4 -rw-rw-r-- 1 1001 1001 26 1月 11 16:27 ./12.txt
36783494 4 -rw-rw-r-- 1 1001 1001 9 1月 11 16:26 ./1.txt
36783495 4 -rw-rw-r-- 1 1001 1001 26 1月 11 16:26 ./backup.sh
36780031 0 -rw-rw-r-- 1 1001 1001 0 1月 11 16:24 ./sendmail.py
[root@virtue tsa]# find . -name "*.py" -delete
[root@virtue tsa]# ll
總用量 12
-rw-rw-r--. 1 1001 1001 26 1月 11 16:27 12.txt
-rw-rw-r--. 1 1001 1001 9 1月 11 16:26 1.txt
drwxr-xr-x. 2 root root 36 1月 11 16:34 a12
drwxr-xr-x. 2 root root 56 1月 11 16:35 a39
-rw-rw-r--. 1 1001 1001 26 1月 11 16:26 backup.sh
# 查找后exec完成指定操作
[root@virtue tsa]# find . -type f -exec ls -l {} \;
-rw-rw-r--. 1 1001 1001 26 1月 11 16:27 ./12.txt
-rw-rw-r--. 1 1001 1001 9 1月 11 16:26 ./1.txt
-rw-rw-r--. 1 1001 1001 26 1月 11 16:26 ./backup.sh
-rw-r--r--. 1 root root 0 1月 11 16:34 ./a12/12.txt
-rw-r--r--. 1 root root 0 1月 11 16:34 ./a12/summary.md
-rw-r--r--. 1 root root 0 1月 11 16:35 ./a39/detail.html
-rw-r--r--. 1 root root 0 1月 11 16:35 ./a39/style.css
-rw-r--r--. 1 root root 0 1月 11 16:35 ./a39/script.js
# 使用exec刪除查找到的文件(慎重:刪除時并不會進(jìn)行確認(rèn)操作)
[root@virtue tsa]# find . -name "*.txt" -exec rm {} \;
[root@virtue tsa]# find . -type f -exec ls -l {} \;
-rw-rw-r--. 1 1001 1001 26 1月 11 16:26 ./backup.sh
-rw-r--r--. 1 root root 0 1月 11 16:34 ./a12/summary.md
-rw-r--r--. 1 root root 0 1月 11 16:35 ./a39/detail.html
-rw-r--r--. 1 root root 0 1月 11 16:35 ./a39/style.css
-rw-r--r--. 1 root root 0 1月 11 16:35 ./a39/script.js
# 安全的刪除方式
[root@virtue tsa]# find . -name "s*"
./a12/summary.md
./a39/style.css
./a39/script.js
[root@virtue tsa]# find . -name "s*" -ok rm {} \;
< rm ... ./a12/summary.md > ? y
< rm ... ./a39/style.css > ? y
< rm ... ./a39/script.js > ?
[root@virtue tsa]# find . -name "s*"
./a39/script.js
# 多次執(zhí)行exec
[root@virtue tsa]# find /etc/ -name passwd -exec grep root {} \; -exec ls -l {} \;
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
-rw-r--r--. 1 root root 1085 1月 11 16:33 /etc/passwd
find和xargs配合使用應(yīng)用實例
# 普通的配合使用
[root@virtue tsa]# find . -name "*.txt" | xargs ls -l
-rw-r--r--. 1 root root 0 1月 12 16:01 ./a.txt
-rw-r--r--. 1 root root 0 1月 12 16:01 ./b.txt
-rw-r--r--. 1 root root 0 1月 12 16:01 ./c.txt
-rw-r--r--. 1 root root 0 1月 12 16:01 ./d.txt
-rw-r--r--. 1 root root 0 1月 12 16:01 ./e.txt
-rw-r--r--. 1 root root 0 1月 12 16:01 ./f.txt
# 配合使用打包
[root@virtue tsa]# find . -name "*.txt" -print0 | xargs -0 tar cf find.tar
[root@virtue tsa]# tar tf find.tar
./a.txt
./b.txt
./c.txt
./d.txt
./e.txt
./f.txt
find邏輯運(yùn)算符
| 命令 | 說明 |
|---|---|
| -a | 并且 |
| -o | 或者 |
| + | 超過 |
| - | 低于 |
# find運(yùn)算符應(yīng)用實例
[root@virtue tsa]# find . -name "a*" -a -type d
./a12
./a39
[root@virtue tsa]# find /etc/ -size +20k -a -size -50k | wc -l
27