linux下查找文件及完整文件路徑實(shí)現(xiàn)方式
一、指令文件的的搜尋
我們知道在終端機(jī)模式當(dāng)中,連續(xù)輸入兩次[tab]按鍵就能夠知道使用者有多少指令可以下達(dá)。 那你知不知道這些指令的完整文件名放在哪里?
舉例來(lái)說(shuō),ls 這個(gè)常用的指令放在哪里呢? 就通過(guò) which 或 type 來(lái)找尋吧!
which (尋找“可執(zhí)行文件”)
[root@study ~]# which [-a] command
選項(xiàng)或參數(shù):
-a :將所有由 PATH 目錄中可以找到的指令均列出,而不止第一個(gè)被找到的指令名稱(chēng)
范例一:搜尋 ifconfig 這個(gè)指令的完整文件名
[root@study ~]# which ifconfig
/sbin/ifconfig
范例二:用 which 去找出 which 的文件名為何?
[root@study ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/bin/alias
/usr/bin/which
# 竟然會(huì)有兩個(gè) which ,其中一個(gè)是 alias 這玩意兒呢!那是啥?
# 那就是所謂的“命令別名”,意思是輸入 which 會(huì)等于后面接的那串指令啦!
# 更多的數(shù)據(jù)我們會(huì)在 bash 章節(jié)中再來(lái)談的!
范例三:請(qǐng)找出 history 這個(gè)指令的完整文件名
[root@study ~]# which history
/usr/bin/which: no history in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:
/usr/sbin:/usr/bin:/root/bin)
[root@study ~]# history --help
-bash: history: --: invalid option
history: usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg
# 瞎密?怎么可能沒(méi)有 history ,我明明就能夠用 root 執(zhí)行 history 的?。?/pre>二、文件文件名的搜尋
再來(lái)談一談怎么搜尋文件吧!在 Linux 下面也有相當(dāng)優(yōu)異的搜尋指令呦!通常 find 不很常用的!因?yàn)樗俣嚷猓?也很操硬盤(pán)!
一般我們都是先使用 whereis ,如果真的找不到了,才以 find 來(lái)搜尋呦!
為什么呢?因?yàn)?whereis 只找系統(tǒng)中某些特定目錄下面的文件而已,locate 則是利用數(shù)據(jù)庫(kù)來(lái)搜尋文件名,當(dāng)然兩者就相當(dāng)?shù)目焖伲?并且沒(méi)有實(shí)際的搜尋硬盤(pán)內(nèi)的文件系統(tǒng)狀態(tài),比較省時(shí)間啦!
1、whereis (由一些特定的目錄中尋找文件文件名)
[root@study ~]# whereis [-bmsu] 文件或目錄名 選項(xiàng)與參數(shù): -l :可以列出 whereis 會(huì)去查詢(xún)的幾個(gè)主要目錄而已 -b :只找 binary 格式的文件 -m :只找在說(shuō)明文檔 manual 路徑下的文件 -s :只找 source 來(lái)源文件 -u :搜尋不在上述三個(gè)項(xiàng)目當(dāng)中的其他特殊文件 范例一:請(qǐng)找出 ifconfig 這個(gè)文件名 [root@study ~]# whereis ifconfig ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
等一下我們會(huì)提到 find 這個(gè)搜尋指令, find 是很強(qiáng)大的搜尋指令,但時(shí)間花用的很大?。ㄒ?yàn)?find 是直接搜尋硬盤(pán),為如果你的硬盤(pán)比較老舊的話(huà),嘿嘿!有的等?。?這個(gè)時(shí)候 whereis 就相當(dāng)?shù)暮糜昧?!另外?whereis 可以加入選項(xiàng)來(lái)找尋相關(guān)的數(shù)據(jù),例如,如果你是要找可可執(zhí)行文件 (binary) 那么加上 -b 就可以啦! 如果不加任何選項(xiàng)的話(huà),那么就將所有的數(shù)據(jù)列出來(lái)啰!
那么 whereis 到底是使用什么咚咚呢?為何搜尋的速度會(huì)比 find 快這么多? 其實(shí)那也沒(méi)有什么,只是因?yàn)?whereis 只找?guī)讉€(gè)特定的目錄而已~并沒(méi)有全系統(tǒng)去查詢(xún)之故。所以說(shuō),whereis 主要是針對(duì) /bin /sbin 下面的可執(zhí)行文件, 以及 /usr/share/man 下面的 man page 文件,跟幾個(gè)比較特定的目錄來(lái)處理而已。所以速度當(dāng)然快的多!不過(guò),就有某些文件是你找不到的啦! 想要知道 whereis 到底查了多少目錄?可以使用 whereis -l 來(lái)確認(rèn)一下即可!
2、fing命令
| 選項(xiàng) | 描述 |
|---|---|
| -name | 按照文件名搜索(區(qū)分大?。?/td> |
| -type | 按照文件類(lèi)型搜索 |
| -size | 按照文件大小搜索 |
| -mtime | 按照文件修改時(shí)間搜索 |
| -exec | 對(duì)搜索結(jié)果執(zhí)行指定命令 |
| -maxdepth | 設(shè)置搜索深度 |
| -mindepth | 設(shè)置搜索深度 |
| -user | 按照文件所有者搜索 |
| -group | 按照文件所屬組搜索 |
| -perm | 按照文件權(quán)限搜索 |
| 將搜索結(jié)果輸出到標(biāo)準(zhǔn)輸出 | |
| -delete | 刪除搜索結(jié)果 |
| -ok | 和 - exec 類(lèi)似,但需要確認(rèn) |
| -iname | 按照文件名搜索(不區(qū)分大小寫(xiě)) |
| -printf | 按照指定格式輸出搜索結(jié)果 |
| -regex | 按照正則表達(dá)式搜索 |
| -newer | 按照文件修改時(shí)間搜索 |
| -depth | 從深度優(yōu)先搜索改為廣度優(yōu)先搜索 |
| -mount | 只搜索當(dāng)前掛載的文件系統(tǒng) |
| -xdev | 同 - mound 選項(xiàng)(注:原文可能存在拼寫(xiě)誤差,標(biāo)準(zhǔn)選項(xiàng)為 - mount,-xdev 功能與之類(lèi)似,均用于限制在當(dāng)前文件系統(tǒng)搜索) |
2.1、按文件名
查找文件/etc 下所有以conf為結(jié)尾的文件
[root@localhost etc]# find /etc/ -name "*.conf" /etc/resolv.conf /etc/fonts/conf.d/65-0-khmeros-base.conf /etc/fonts/conf.d/61-urw-fallback-backwards.conf /etc/fonts/conf.d/66-ucs-miscfixed.conf /etc/fonts/conf.d/31-cantarell.conf /etc/fonts/conf.d/60-overpass.conf /etc/fonts/conf.d/59-liberation-sans.conf /etc/fonts/conf.d/65-0-lohit-devanagari.conf /etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf /etc/fonts/conf.d/65-sil-padauk.conf /etc/fonts/conf.d/57-dejavu-sans.conf /etc/fonts/conf.d/65-0-lohit-kannada.conf /etc/fonts/conf.d/59-liberation-mono.conf /etc/fonts/conf.d/65-0-nhn-nanum-gothic.conf /etc/fonts/conf.d/10-hinting-slight.conf /etc/fonts/conf.d/66-sil-abyssinica.conf /etc/fonts/conf.d/10-scale-bitmap-fonts.conf /etc/fonts/conf.d/66-sil-nuosu.conf /etc/fonts/conf.d/20-unhint-small-vera.conf /etc/fonts/conf.d/65-0-ttf-arphic-uming.conf /etc/fonts/conf.d/25-no-bitmap-fedora.conf /etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf /etc/fonts/conf.d/25-unhint-nonlatin.conf /etc/fonts/conf.d/65-0-lohit-tamil.conf /etc/fonts/conf.d/30-metric-aliases.conf /etc/fonts/conf.d/69-gnu-free-sans.conf /etc/fonts/conf.d/30-urw-aliases.conf /etc/fonts/conf.d/69-gnu-free-serif.conf /etc/fonts/conf.d/40-nonlatin.conf
2.2按文件類(lèi)型查詢(xún)
find 路徑 -type 類(lèi)型
類(lèi)型:
普通文件 f 目錄d 符號(hào)鏈接l 塊設(shè)備文件b
字符設(shè)備文件c socket文件s 管道文件p
[root@localhost etc]# find /etc/ -type d |more /etc/ /etc/fonts /etc/fonts/conf.d /etc/grub.d /etc/pki /etc/pki/rpm-gpg /etc/pki/ca-trust /etc/pki/ca-trust/extracted /etc/pki/ca-trust/extracted/java /etc/pki/ca-trust/extracted/openssl /etc/pki/ca-trust/extracted/pem /etc/pki/ca-trust/source /etc/pki/ca-trust/source/anchors /etc/pki/ca-trust/source/blacklist /etc/pki/java /etc/pki/tls /etc/pki/tls/certs
2.3按照文件大小查找
通項(xiàng):find 路徑 -size 范圍
范圍:+表示大于 -表示小于 等于不需要符號(hào)
大小:M(必須大寫(xiě)) k(必須小寫(xiě)) c(表示字符)
查找該目錄下0k到10k的文件
[root@localhost nginx]# ll 總用量 28 drwxr-xr-x. 2 root root 54 6月 29 01:55 conf.d -rw-r--r--. 1 root root 1007 5月 30 03:07 fastcgi_params -rw-r--r--. 1 root root 5349 5月 30 03:07 mime.types lrwxrwxrwx. 1 root root 29 6月 29 01:41 modules -> ../../usr/lib64/nginx/modules -rw-r--r--. 1 root root 918 6月 29 04:05 nginx.conf -rw-r--r--. 1 root root 632 6月 29 01:32 nginx.conf.rpmsave -rw-r--r--. 1 root root 636 5月 30 03:07 scgi_params -rw-r--r--. 1 root root 664 5月 30 03:07 uwsgi_params [root@localhost nginx]# find /etc/nginx/ -size +0 -size -10 /etc/nginx/ /etc/nginx/conf.d /etc/nginx/conf.d/default.conf.rpmsave /etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf.rpmsave /etc/nginx/fastcgi_params /etc/nginx/modules /etc/nginx/scgi_params /etc/nginx/uwsgi_params /etc/nginx/nginx.conf
2.4按照文件日期查找
2.4.1 按照創(chuàng)建日期查找 Ctime
find 路徑 -ctime -n/+n
[root@localhost nginx]# find /etc/nginx/ -ctime -7 /etc/nginx/ /etc/nginx/conf.d /etc/nginx/conf.d/default.conf.rpmsave /etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf.rpmsave /etc/nginx/fastcgi_params /etc/nginx/mime.types /etc/nginx/modules /etc/nginx/scgi_params /etc/nginx/uwsgi_params /etc/nginx/.nginx.conf.swp /etc/nginx/nginx.conf /etc/nginx/KIND /etc/nginx/kdw
2.4.2按照修改日期查找 mtime
find 路徑 -mtime -n/+n
[root@localhost nginx]# find . -mtime -3 . ./KIND ./kdw [root@localhost nginx]# ls -la
2.4.3按照訪問(wèn)日期查找 atime
find 路徑 -atime -n/+n
[root@localhost nginx]# find . -atime -1 . ./conf.d ./conf.d/default.conf ./mime.types ./modules ./nginx.conf ./KIND ./kdw
2.5按深度查找
2.5.1查找起始點(diǎn)以下n層的目錄,不超過(guò)n層 (就是從當(dāng)前目錄的上一級(jí)開(kāi)始查找)
find 路徑 -maxdepth n(層數(shù))
[root@localhost ~]# cd /etc/nginx/ [root@localhost nginx]# find . -maxdepth 2 . ./conf.d ./conf.d/default.conf.rpmsave ./conf.d/default.conf ./nginx.conf.rpmsave ./fastcgi_params ./mime.types ./modules ./scgi_params ./uwsgi_params ./.nginx.conf.swp ./nginx.conf #就相當(dāng)于在nginx 以下的文件及連接等都列出來(lái)
搜距離起始點(diǎn)n層以下的目錄(即最少n層)
find 路徑 -mindepth n(層數(shù))
[root@localhost nginx]# ll 總用量 28 drwxr-xr-x. 2 root root 54 6月 29 01:55 conf.d -rw-r--r--. 1 root root 1007 5月 30 03:07 fastcgi_params -rw-r--r--. 1 root root 5349 5月 30 03:07 mime.types lrwxrwxrwx. 1 root root 29 6月 29 01:41 modules -> ../../usr/lib64/nginx/modules -rw-r--r--. 1 root root 918 6月 29 04:05 nginx.conf -rw-r--r--. 1 root root 632 6月 29 01:32 nginx.conf.rpmsave -rw-r--r--. 1 root root 636 5月 30 03:07 scgi_params -rw-r--r--. 1 root root 664 5月 30 03:07 uwsgi_params [root@localhost nginx]# find . -mindepth 2 ./conf.d/default.conf.rpmsave ./conf.d/default.conf #就相當(dāng)于是 目錄以下的文件列出來(lái)
3、grep命令
即按照內(nèi)容查找
grep 參數(shù) "內(nèi)容" 查找目錄或文件
參數(shù):
- -r 若是目錄,則可以遞歸查找
- -n:可以顯示該查找內(nèi)容所在的行號(hào)
- -i:可以忽略大小寫(xiě)進(jìn)行查找
- -v:不顯示含有某字符串
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何搭建并配置HTTPD文件服務(wù)及訪問(wèn)權(quán)限控制
這篇文章主要介紹了如何搭建并配置HTTPD文件服務(wù)及訪問(wèn)權(quán)限控制的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
Nginx 0.7.x + PHP 5.2.6(FastCGI)+ MySQL 5.1 在128M小內(nèi)存VPS服務(wù)器上的
VPS(全稱(chēng)Virtual Private Server)是利用最新虛擬化技術(shù)在一臺(tái)物理服務(wù)器上創(chuàng)建多個(gè)相互隔離的虛擬私有主機(jī)。它們以最大化的效率共享硬件、軟件許可證以及管理資源。2008-12-12
阿里云服務(wù)器實(shí)現(xiàn)域名解析步驟(小白教程)
這篇文章主要介紹了阿里云服務(wù)器實(shí)現(xiàn)域名解析步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Linux網(wǎng)絡(luò)搭建基礎(chǔ)實(shí)驗(yàn)(1)
這篇文章主要為大家詳細(xì)介紹了Linux網(wǎng)絡(luò)搭建基礎(chǔ)實(shí)驗(yàn)過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
APACHE 配置文件中文版 httpd.conf FOR Apache 2.2.13
APACHE配置文件中文版 httpd.conf FOR Apache 2.2.13 ,綜合網(wǎng)上2.0版本的翻譯,加入自己的理解,補(bǔ)充完善。2009-11-11

