溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Linux查找命令與find命令的詳細(xì)介紹

發(fā)布時(shí)間:2021-08-17 14:51:26 來(lái)源:億速云 閱讀:117 作者:chen 欄目:建站服務(wù)器

本篇內(nèi)容介紹了“Linux查找命令與find命令的詳細(xì)介紹”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!



一、文件查找之locate命令

locate :
非實(shí)時(shí)的,查找時(shí)根據(jù)全系統(tǒng)文件數(shù)據(jù)庫(kù)進(jìn)行的,模糊查找,
update 手動(dòng)生成文件數(shù)據(jù)庫(kù)
速度快

依賴(lài)于updatedb數(shù)據(jù)庫(kù)

    
#手動(dòng)更新locate數(shù)據(jù)庫(kù)
[root@lovelace scripts]# updatedb
#使用locate查找文件 (速度好快的說(shuō))
[root@lovelace scripts]# locate *.py
/home/scripts/factorial.py
/home/scripts/input.py
/usr/lib/python2.4/fileinput.py
/usr/lib/python2.4/fileinput.pyc
/usr/lib/python2.4/fileinput.pyo
/usr/lib/python2.4/site-packages/dogtail/rawinput.py
/usr/lib/python2.4/site-packages/dogtail/rawinput.pyc
/usr/lib/python2.4/site-packages/dogtail/rawinput.pyo
/usr/lib/python2.4/test/pyclbr_input.py
/usr/lib/python2.4/test/pyclbr_input.pyc
/usr/lib/python2.4/test/pyclbr_input.pyo
/usr/lib/python2.4/test/test_fileinput.py
/usr/lib/python2.4/test/test_fileinput.pyc
/usr/lib/python2.4/test/test_fileinput.pyo

二、文件查找之find命令

find:
實(shí)時(shí)
精確
支持眾多查找規(guī)則
遍歷指定目錄中的所有文件完成查找,速度慢

find 查找路徑 查找標(biāo)準(zhǔn) 查找到以后的處理運(yùn)作
查找路徑:默認(rèn)為當(dāng)前目錄
查找標(biāo)準(zhǔn):默認(rèn)為指定路徑下的所有文件
查找到以后的處理操作:默認(rèn)為顯示

匹配標(biāo)準(zhǔn):
-name 'filename':對(duì)文件名作精確匹配
文件名通配:
* 任意長(zhǎng)度的任意字符
? 任意的單個(gè)字符
[] 選項(xiàng)內(nèi)的字符
-iname 'filename':文件名匹配時(shí)不區(qū)分大小寫(xiě)
-regex pattern 基于正則表達(dá)式進(jìn)行文件名匹配

-user username :根據(jù)屬主查找
-group groupname :根據(jù)屬組查找

-gid gid:根據(jù)gid查找
-uid uid::根據(jù)udi查找

-nouser 查找沒(méi)有屬主的文件
-nogroup 沒(méi)有屬組的文件

example: find /tmp -nouser
    
[root@lovelace scripts]# find /tmp -name test
/tmp/sources/httpd-2.2.17/srclib/apr-util/test
/tmp/sources/httpd-2.2.17/srclib/apr/test
/tmp/sources/httpd-2.2.17/modules/test
/tmp/sources/httpd-2.2.17/test
/tmp/test

根據(jù)文件類(lèi)型來(lái)查找

-type
f:普通文件
d: 目錄
c: 字符
b: 塊設(shè)備
l: 鏈接
p: 管道
s: 套接字

example:find /etc -type d
    
#查找/tmp目錄下名字為test 而且文件格式為目錄的
[root@lovelace scripts]# find /tmp -type d -a -name test
/tmp/sources/httpd-2.2.17/srclib/apr-util/test
/tmp/sources/httpd-2.2.17/srclib/apr/test
/tmp/sources/httpd-2.2.17/modules/test
/tmp/sources/httpd-2.2.17/test
/tmp/test

根據(jù)文件大小查找

-size
[+|-]#k 沒(méi)有+和-就代表是精確匹配
[+|-]#m
[+|-]#G

example:find /tmp -size +10M 查找/etc目錄下大于10M的文件

組合條件:這個(gè)需要了解摩根定律
-a:與 默認(rèn)
-o:或
-not :非
example:find /tmp -not -user user1 -o -not -type d
#查找/tmp目錄下文件格式為目錄,而且大小在26k到32k之間的目錄
[root@lovelace scripts]# find /tmp -type d -a -size +16k -a -size -32k
/tmp/sources/httpd-2.2.17/docs/manual/mod
/tmp/sources/php-5.2.13/ext/reflection/tests
/tmp/sources/php-5.2.13/ext/date/tests
/tmp/sources/php-5.2.13/ext/spl/tests
/tmp/sources/php-5.2.13/tests/classes
/tmp/sources/php-5.2.13/Zend/tests

 

根據(jù)文件時(shí)間戳來(lái)查找:
以天為單位的:

改變時(shí)間:-mtime
修改時(shí)間:-ctime
查看時(shí)間:-atime
[+|-]# 默認(rèn)時(shí)間戳為5天
-5:5天內(nèi)訪問(wèn)過(guò)
+5:至少5天沒(méi)訪問(wèn)過(guò)了
5:離現(xiàn)在剛好5天訪問(wèn)過(guò)

以分鐘為單位的:
-mmin:
-cmin:
-amin:

find /tmp -amin –5 5分鐘內(nèi)被訪問(wèn)過(guò)的文件

至少多久沒(méi)有被訪問(wèn)的,且文件大小超過(guò)多少的,執(zhí)行刪除操作
find /tmp -atime +30 -a –size +100M -exec 'rm -rf *' \;
    
#查找home目錄下至少5天沒(méi)被訪問(wèn)過(guò)的文件
[root@lovelace scripts]# find /home/ -ctime -5
/home/scripts
/home/scripts/list
/home/scripts/for
/home/scripts/for/dir.sh
/home/scripts/for/three.sh
/home/scripts/for/checkbash.sh
/home/scripts/for/sorsum.sh
/home/scripts/while
/home/scripts/while/readpasswd.sh
/home/scripts/while/catwhile.sh
/home/scripts/case
/home/scripts/case/showmenu.sh
/home/scripts/case/showmenu
/home/scripts/case/2showmenu.sh
/home/scripts/if
/home/scripts/if/grade.sh
/home/scripts/51cto
/home/scripts/51cto/info.tt
/home/scripts/51cto/info.sh
/home/scripts/51cto/1.sh
/home/scripts/51cto/argument.sh
/home/scripts/51cto/sum.sh

 

根據(jù)權(quán)限查找:
-perm mode :精確匹配
-perm -mode :每一位權(quán)限都必須精確匹配 文件權(quán)限能完全包含此mode的均符合標(biāo)準(zhǔn)
-perm /mode :9位權(quán)限中有任何一位符合條件的

example:find /tmp -perm –001 查找其他用戶(hù)有寫(xiě)權(quán)限的文件


find的動(dòng)作:
-print 默認(rèn)
-ls:類(lèi)似 ls -l的形式顯示文件的每一個(gè)信息

-ok command {} \; 會(huì)每次執(zhí)行進(jìn)行詢(xún)問(wèn)操作,需要用戶(hù)確認(rèn)
-exec command {} \; 不會(huì)驚醒詢(xún)問(wèn)操作
引用原來(lái)的額名字,使用{}

example:find /tmp -perm -020 -exec mv {} {}.new \;

#找出home中大小為16k到32k之間的文件,然后傳遞給exec 并顯示出來(lái)
[root@lovelace scripts]# find /home/ -size +16k -a -size -32k -exec ls -lh {} \;
-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/gconf.xml.defaults/%gconf-tree-li.xml
-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/gconf.xml.defaults/%gconf-tree-ug.xml
-rw-r--r-- 1 root root 25K 05-03 03:04 /home/nick/etc/gconf/schemas/drivemount.schemas
-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/schemas/gnome-volume-control.schemas
-rw-r--r-- 1 root root 21K 05-03 03:04 /home/nick/etc/gconf/schemas/system_smb.schemas
-rw-r--r-- 1 root root 22K 05-03 03:04 /home/nick/etc/gconf/schemas/desktop_gnome_thumbnailers.schemas
-rw-r--r-- 1 root root 17K 05-03 03:04 /home/nick/etc/gconf/schemas/apps_gnome_settings_daemon_default_editor.schemas

find和xargs命令
xargs: 作用是將參數(shù)列表轉(zhuǎn)換成小塊分段傳遞給其他命令,以避免參數(shù)列表過(guò)長(zhǎng)的問(wèn)題。
相較于-exec command 而言,此命令功能更強(qiáng)悍。和find合用的時(shí)候,一般是通過(guò)管道傳遞給xargs
find /tmp –size +100M | xargs ‘rm –rf '

三、其他查找命令

which: 定位一個(gè)命令的完整路徑,有可能會(huì)顯示出命令的別名

#使用which命令查找ls命令的完整路徑
[root@lovelace scripts]# which ls
alias ls='ls --color=tty'
/bin/ls
#使用ldd命令查看ls所依賴(lài)的庫(kù)文件(這里需要用到ls的完整路徑)
[root@lovelace scripts]# ldd /bin/ls
linux-gate.so.1 =>  (0x00cf8000)
librt.so.1 => /lib/librt.so.1 (0x00d8d000)
libacl.so.1 => /lib/libacl.so.1 (0x00d62000)
libselinux.so.1 => /lib/libselinux.so.1 (0x00de0000)
libc.so.6 => /lib/libc.so.6 (0x00110000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00d71000)
/lib/ld-linux.so.2 (0x00baf000)
libattr.so.1 => /lib/libattr.so.1 (0x003a9000)
libdl.so.2 => /lib/libdl.so.2 (0x00d55000)
libsepol.so.1 => /lib/libsepol.so.1 (0x00d98000)

whereis:與which類(lèi)似,而且會(huì)額外的給出給出該命令的man頁(yè)的完整路徑
    
#使用whereis查看命令的完整路徑和相應(yīng)的man文件
[root@lovelace scripts]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
#man 查看對(duì)應(yīng)的文件
[root@lovelace scripts]# man 1 ls
#man 查看對(duì)應(yīng)的文件
[root@lovelace scripts]#
[root@lovelace scripts]# man 1p ls

whatis:將會(huì)在whatis數(shù)據(jù)庫(kù)中查詢(xún)file,當(dāng)你想確認(rèn)系統(tǒng)命令和重要的配置文件的時(shí)候,這個(gè)命令就非常重要了,可以當(dāng)做一個(gè)簡(jiǎn)單的man命令。
[root@lovelace scripts]# whatis ls
ls                   (1)  - list directory contents
ls                   (1p)  - list directory contents

總結(jié):之前面對(duì)which、whatis、whereis的時(shí)候,總是犯糊涂,這幾個(gè)命令的作用老是給搞混,所以特此留記,以防止在搞混,另外,find命令對(duì)我們的運(yùn)維工作至關(guān)重要,尤其是針對(duì)服務(wù)器被入侵之后,文件是否被篡改有著非同一般的功效。

“Linux查找命令與find命令的詳細(xì)介紹”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI