linux如何用locate和find命令查找文件?

1 locate 查找文件 使用updatedb 建立mlocate.db 整個磁盤文件搜索建立索引 影響磁盤性能。

locate -i 不區(qū)分大小寫
locate -n 只列出前n個
locate -r " .(jpg|png)$"   查找以jpg或png結(jié)尾的文件  支持j基本的正則表達式
2 find 實時搜索 默認遞歸搜索
  find  [OPTION] ...[查找路徑] [查找條件][處理動作]
/etc/passwd  ----  maxdepth 指定搜索層級 本例 /etc/下1層目錄中搜索passwd.
find /etc/ -maxdepth 2 -mindepth 2 -name filename 搜索最多到第二層 最少搜索第二層只搜索第二層
[23:54:08 root@localhost ~]$find /data -depth  先列出文件再列出文件夾 排列有變化
/data/scripts37/diskcheck1026.sh
/data/scripts37/creatuseradd.sh
/data/scripts37/read23.sh
。。。。。。。。。。。
/data/f1.txt
/data/dir/f1
/data/dir
/data/f2
/data
-iname "文件名稱":不區(qū)分大小寫
[00:02:27 root@localhost data]$ll -i
total 68
100663360 drwxr-xr-x. 2 root root    16 Feb 16 23:51 dir
69 -rw-r--r--. 1 root root     0 Feb 16 20:51 f1.txt
85 -rw-r--r--. 1 root root   595 Feb 16 23:51 f2
68 drwxr-xr-x. 2 root root  4096 Feb 16 23:13 scripts37
67 -rw-r--r--. 1 root root 61440 Feb 16 08:17 scripts37.tar
33554496 drwxr-xr-x. 3 root root    19 Feb 16 15:28 test
[00:02:33 root@localhost data]$find /data/ -inum 70
/data/scripts37/diskcheck1026.sh
[00:06:25 root@localhost data]$ll -i
total 72
100663360 drwxr-xr-x. 2 root root    16 Feb 16 23:51 dir
69 -rw-r--r--. 1 root root     0 Feb 16 20:51 f1.txt
85 -rw-r--r--. 2 root root   595 Feb 16 23:51 f2
85 -rw-r--r--. 2 root root   595 Feb 16 23:51 f2.link
68 drwxr-xr-x. 2 root root  4096 Feb 16 23:13 scripts37
67 -rw-r--r--. 1 root root 61440 Feb 16 08:17 scripts37.tar
33554496 drwxr-xr-x. 3 root root    19 Feb 16 15:28 test
[00:06:35 root@localhost data]$find /data/ -samefile /data/f2
/data/f2
/data/f2.link
[00:07:08 root@localhost data]$
— regex "PSTTERN" :以PATTERN匹配整個文件路徑。而非文件名稱
00:10:50 root@localhost data]$find /usr/share/pixmaps/ -regex ".*.jpg$"  ---匹配完成路徑
find /etc -type d 搜索、etc下的文件夾
find 、dev -type b 搜索塊文件
   find /data -empty -ls   搜索空文件 大寫為零
   [00:22:44 root@localhost data]$find /data/ -empty -ls
69    0 -rw-r--r--   1 root     root            0 Feb 16 20:51 /data/f1.txt
   find /data  ! -empty -ls 搜索非空文件                !邏輯取反
       find /data  -not  -empty -ls 搜索非空文件        
與 :-a
或 : -o
非: -not !
#find /data  ! -empty -ls -----查非空文件及文件夾
#find /data  -not  -empty -ls -----查非空文件及文件夾
find /data -name "f"  -a -type f 查找以f開頭并且是普通文件 -a 可省略 默認就是并且模式
find /data -name "f

find /data -name "f" -o -type f 查找以f開頭或者是普通文件的文件
#find /etc -path '/etc/sane.d'  -a -prune -o -name ".conf"  --- -a并且 -prune剪切  剪切etc/sane.d/目錄后去搜索 /etc下的  .conf 文件。
*find  /etc (  -path "/etc/sane.d" -o "/etc/fonts"  ) -a -prune -o -name ".conf"搜索 /etc下 除了、etc/sane.d文件夾及、etc/fonts文件夾里的以 .conf結(jié)尾的文件**
10240+0 records in
10240+0 records out
10240 bytes (10 kB) copied, 0.140448 s, 72.9 kB/s
[03:23:35 root@localhost data]$ll f1
-rw-r--r--. 1 root root 10240 Feb 17 03:23 f1
[03:23:41 root@localhost data]$find -size 10k
./f1
[03:24:06 root@localhost data]$
#find /etc/  -szie -6k{查找0-5K之間的文件}
#find /etc/ -size +6k {查找大于6k的文件}
find -atime(默認是以天為單位) +10 10天以前的文件
find -atime 10 查找第十天至第十一天之間的文件 不寫路徑默認當前路徑
find -perm 644 查找當前644的文件權(quán)限精確匹配
[04:17:32 root@localhost data]$find -perm -222
[04:17:49 root@localhost data]$ll
total 80
drwxr-xr-x. 2 root root    16 Feb 16 23:51 dir
drwxr-xr-x. 2 root root     6 Feb 17 00:23 dir2
----------. 1 root root 10240 Feb 17 03:23 f1
-rw-r--r--. 1 root root     0 Feb 16 20:51 f1.txt
-rw-r--r--. 1 root root   595 Feb 16 23:51 f2
lrwxrwxrwx. 1 root root     8 Feb 17 00:08 f2.link -> /data/f2
等價命令
[04:38:24 root@localhost data]$find -perm -001 > /data/ls.log
[04:42:48 root@localhost data]$find -perm /001 -ok chmod o+w {} \;  查找文件 并做權(quán)限修改  "{}"獲取查找結(jié)果 -ok 必須以 "\;"結(jié)束 語法要求
< chmod ... ./scripts37 > ? y
< chmod ... ./scripts37/diskcheck1026.sh > ? y
< chmod ... ./scripts37/creatuseradd.sh > ? y
< chmod ... ./scripts37/age.sh > ? y
< chmod ... ./scripts37/yesorno_case23.sh > ? y
< chmod ... ./scripts37/count.sh > ? y
< chmod ... ./scripts37/YESORNO23.sh > ? y
< chmod ... ./scripts37/systeminfo23.sh > ? y
< chmod ... ./scripts37/yesorno23.sh > ? y
< chmod ... ./scripts37/checkdisk.sh > ? y
< chmod ... ./scripts37/useDISK.sh > ? y
< chmod ... ./scripts37/set.sh > ? y
< chmod ... ./test > ? y
< chmod ... ./test/test1 > ? y
< chmod ... ./dir > ? y
< chmod ... ./dir2 > ? y
< chmod ... ./fid > ? y
drwxr-xrwx. 2 root root    16 Feb 16 23:51 dir
drwxr-xrwx. 2 root root     6 Feb 17 00:23 dir2
-rw-r--r--. 1 root root     0 Feb 16 20:51 f1.txt
drwxr-xrwx. 2 root root     6 Feb 17 00:35 fid
-rw-r--r--. 1 root root  1540 Feb 17 04:38 flog2.log
-rw-r--r--. 1 root root   325 Feb 17 04:42 ls.log
drwxr-xrwx. 2 root root  4096 Feb 16 23:13 scripts37
-rw-r--r--. 1 root root 61440 Feb 16 08:17 scripts37.tar
drwxr-xrwx. 3 root root    19 Feb 16 15:28 test
-rw-------. 1 root root 28M Dec 14 08:31 /boot/initramfs-3.10.0-957.el7.x86_64.img
-rw-------. 1 root root 11M Dec 14 08:32 /boot/initramfs-3.10.0-957.el7.x86_64kdump.img

創(chuàng)新互聯(lián)專注于宜川網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供宜川營銷型網(wǎng)站建設(shè),宜川網(wǎng)站制作、宜川網(wǎng)頁設(shè)計、宜川網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造宜川網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供宜川網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

find  /etc  -name passwd ---find +l路徑 + 指定文件名

[23:42:14 root@localhost scripts37]$find /etc/ -maxdepth 1 -name passwd

/data/scripts37.tar

-name 支持通配符  如: -name "*.jpg"

-inum n 按照inode號查找

-samefile name 相同的inode號的文件

-link n 鏈接數(shù)為n的文件

[00:13:17 root@localhost data]$find / -user zhonghua  -ls  --按用戶搜索zhonghua的文件 ls把文件屬性列出。

find / -nouser  ----查找無主文件

67160128    0 drwxr-xr-x   2 root     root            6 Feb 16 15:28 /data/test/test1

支持邏輯條件

find /data ( -name "f*" -o -type f ) -a ls -- -a默認是有的 邏輯與優(yōu)先級高 為了不出錯 用括號把優(yōu)先執(zhí)行的命令括起來先執(zhí)行 需要轉(zhuǎn)義。

~# find /etc  -name "*.conf" ---""避免出錯

#排除搜索  

[03:22:12 root@localhost data]$dd if=/dev/zero of=f1 bs=1 count=10240  --創(chuàng)建文件f1 大寫10K bs=1 (1個字節(jié)) 1024字節(jié)是1K ,10240字節(jié)是10k

#find /etc/ -size 6k { 查找5到6K之間大寫的文件}

#find -atime -10 10天以內(nèi)的文件

[04:14:25 root@localhost data]$find /data/scripts37 -perm /622 -ls  模糊匹配 /622 是所有者所屬組與other 只要有一個匹配就查出來。

./f2.link                                      ------------------ -222 中“”-“”意思是且 所有者 所屬組 和other都要有寫權(quán)限

[04:37:54 root@localhost data]$find -perm -001 -fls /data/flog2.log

< chmod ... . > ? y

[04:48:55 root@localhost data]$find -perm /002 -exec ls -l {} \;  exec代替OK 不再提示直接出結(jié)果

total 72

[04:52:44 root@localhost data]$find / -size +10M -exec ls -lh {} \;

-rw-------. 1 root root 71M Dec 14 08:29 /boot/initramfs-0-rescue-c8f47886da6d4ec687a38cc0be7425b1.img

分享題目:linux如何用locate和find命令查找文件?
URL標題:http://bm7419.com/article14/gocsge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、外貿(mào)建站網(wǎng)站維護、網(wǎng)站建設(shè)、微信公眾號企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計