Linux 學習:文件查找的使用

本次記錄以下命令如locate, find的使用格式、常用選項及它們有哪些使用實例等。

wlocate與find命令的特性

locate與find命令都用來查找文件或目錄的。但明顯locate查找速度要比find快得多,原因在於locate不需要搜索具體目錄,而是搜索一個數據庫文件。一般來說,Linux系統會將系統內的所有文件都記錄在一個數據庫文件裏面,Linux系統自動創建這個數據庫,且每天自動更新一次,所以有時你還發現使用locate,會找到已經被刪掉的文件!也會查不到最新變動過的文件。

find命令是直接查找硬盤,比較耗費時間。

1. Locate命令

用來查找文件或目錄。它是非實時查找工具,依賴於事先構建好的索引,而不是在文件系統上直接搜索的。查找速度快,模糊查找。

Locate尋找的數據是由已創建的數據庫/var/lib/mlocate裏面的數據所查找到的,此數據庫每天更新一次,所以當你新建文件後查找該文件,那麼locate會告訴你“找不到”!因爲必須要更新數據庫。

手動通過updatedb命令讀取/etc/updated.conf的設置去查找系統硬盤內的文件名,並更新/var/lib/mlocate內的數據庫文件。

Locate依據/var/lib/mlocate內數據庫記載,找出用戶輸入的關鍵字文件名。

查找文件與目錄Locate命令格式:

locate [OPTION]... PATTERN...

OPTION:

-i 忽略大小寫

-r 可接正則表達式的顯示方式

[root@www tmp]# locate -r 'pwd$' |grep -n 'pwd$'

1:/bin/pwd

2:/sbin/unix_chkpwd

3:/usr/lib64/cracklib_dict.pwd

4:/usr/share/cracklib/cracklib-small.pwd

5:/usr/share/cracklib/pw_dict.pwd

-n# 至多顯示n個輸出

[root@www tmp]# locate -n10 'pass'

/etc/passwd

/etc/passwd-

/etc/openldap/certs/password

/etc/pam.d/gdm-password

/etc/pam.d/passwd

/etc/pam.d/password-auth

/etc/pam.d/password-auth-ac

/etc/profile.d/gnome-ssh-askpass.csh

/etc/profile.d/gnome-ssh-askpass.sh

/etc/security/opasswd

2. find命令

用來在指定的目錄下查找文件。它是實時查找工具,精確匹配查找

在指定的目錄下查找文件的命令格式:

find [OPTION]...[查找路徑][查找條件][處理動作]

查找路徑:如果未指定,則默認爲當前路徑

組合查找條件:

與條件: -a 可以省略

或條件: -o 優先級最低

非條件: -not, ! 優先級高

!A -o !B = !(A -a B)

!A -a !B = !(A -o B)

查找條件:

w 根據文件名進行查找

-name filename: 查找文件名爲filename的文件(支持glob)

[root@www lab]# find /etc -name "*.conf" |tail -n 10|grep -n 'conf'

1:/etc/request-key.conf

2:/etc/prelink.conf

3:/etc/mke2fs.conf

4:/etc/logrotate.conf

5:/etc/ant.conf

6:/etc/mtools.conf

7:/etc/udev/udev.conf

8:/etc/dracut.conf

9:/etc/yp.conf

10:/etc/sgml/sgml.conf

-iname filename 不區分字符大小寫

-regex “PATTERN”指定字符串作爲尋找文件或目錄的範本樣式

[root@www lab]# find /lab -regex '.*\(\.txt\)$'

/lab/readme.txt

/lab/regular_express.txt

/lab/character.txt

w 根據屬主、屬組查找

-user USERNAME 查找屬主爲指定用戶的文件

[root@www lab]# find /lab -user Allen -ls

430089 4 -rw-r--r-- 1 Allen asdgrp 21 9月 11 13:33 /lab/readme.txt

-group GROUPNAME 查找屬組爲指定組的文件

[root@www ~]# find /lab -group asdgrp

/lab/readme.txt

-uid UserID 查找文件的屬主爲指定uid的文件

-gid GroupID 查找文件的屬組爲指定gid的文件

-nouser 查找沒有屬主的文件

-nogroup 查找沒有屬組的文件

[root@www ~]# ll /lab

總用量 52

drwxr-xr-x. 3 root root 4096 9月 6 09:54 123

-rw-r--r--. 1 root root 71 9月 7 12:50 character.txt

-rw-r--r--. 1 root root 899 8月 17 19:30 fstab

-rw-r--r--. 1 root root 19846 9月 9 17:26 functions

-rw-r--r--. 1 505 asdgrp 21 9月 11 13:33 readme.txt

-rw-r--r--. 1 root root 650 9月 6 09:55 regular_express.txt

-rw-r--r--. 1 root root 115 9月 6 09:57 t1

-rw-r--r--. 1 root root 112 9月 6 09:57 t2

-rw-r--r--. 1 root root 170 9月 6 09:58 t3

[root@www ~]# find /lab -uid 505 -ls

430089 4 -rw-r--r-- 1 505 asdgrp 21 9月 11 13:33 /lab/readme.txt

[root@www ~]# find /lab -nouser -ls

430089 4 -rw-r--r-- 1 505 asdgrp 21 9月 11 13:33 /lab/readme.txt

[root@www ~]# find /lab -nogroup -ls

430089 4 -rw-r--r-- 1 505 505 21 9月 11 13:33 /lab/readme.txt

w 根據文件類型進行查找

-type TYPE

[root@www lab]# find -type f -ls

430082 4 -rw-r--r-- 1 root root 112 9月 6 09:57 ./t2

430089 4 -rw-r--r-- 1 505 505 21 9月 11 13:33 ./readme.txt

430077 4 -rw-r--r-- 1 root root 650 9月 6 09:55 ./regular_express.txt

430092 20 -rw-r--r-- 1 root root 19846 9月 9 17:26 ./functions

430073 4 -rw-r--r-- 1 root root 899 8月 17 19:30 ./fstab

430081 4 -rw-r--r-- 1 root root 115 9月 6 09:57 ./t1

430083 4 -rw-r--r-- 1 root root 170 9月 6 09:58 ./t3

389380 4 -rw-r--r-- 1 root root 71 9月 7 12:50 ./character.txt

w 使用組合條件進行查找

[root@www ~]# find /lab -type f -a -nouser -ls

430089 4 -rw-r--r-- 1 505 505 21 9月 11 13:33 /lab/readme.txt

[root@www ~]# find /etc ! -name '*.conf' |tail -n 10

/etc/logrotate.d/yum

/etc/logrotate.d/dracut

/etc/logrotate.d/iscsiuiolog

/etc/logrotate.d/ppp

/etc/logrotate.d/libvirtd.lxc

/etc/logrotate.d/wpa_supplicant

/etc/logrotate.d/glusterfs

/etc/logrotate.d/sssd

/etc/logrotate.d/syslog

/etc/logrotate.d/psacct

[root@www ~]# find /etc -not -name "*.txt" -a -not -type l -ls |head -n 3

[root@www ~]# find /etc ! \( -name "*.txt" -o -type l \) -ls |head -n 3

389381 12 drwxr-xr-x 125 root root 12288 9月 11 15:49 /etc

414087 16 -rw-r--r-- 1 root root 12623 7月 24 09:34 /etc/autofs.conf

408995 4 drwxr-xr-x 3 root root 4096 8月 17 19:42 /etc/sound

w 根據文件大小來查找

-size: [+|-]#UNIT

#UNIT (#-1,#]

+UNIT (#,+∞)

-UNIT [0, #-1]

[root@www ~]# find /etc -size 3k -exec ls -lh {} \;<-- 找出>2k3k的文件

-rw-r--r--. 1 root root 2.5K 11月 11 2010 /etc/udev/rules.d/97-bluetooth-serial.rules

-rw-r--r--. 1 root root 2.6K 8月 17 2010 /etc/mtools.conf

-rwxr-xr-x. 1 root root 2.6K 10月 16 2014 /etc/gdm/Init/Default

-rw-r--r--. 1 root root 2.9K 11月 23 2013 /etc/init/readahead-collector.conf

-rw-r--r--. 1 root root 2.3K 7月 24 09:38 /etc/selinux/semanage.conf

-rw-r--r--. 1 root root 2.9K 7月 24 21:05 /etc/selinux/targeted/contexts/x_contexts

-rw-r--r--. 1 root root 2.1K 11月 11 2010 /etc/bluetooth/main.conf

-rw-r--r--. 1 root root 2.1K 9月 7 23:36 /etc/passwd

-rw-r--r--. 1 root root 2.6K 11月 12 2010 /etc/latrace.d/signal.conf

-rw-r--r--. 1 root root 2.3K 11月 12 2010 /etc/latrace.d/resource.conf

w 根據文件時間戳查找

訪問時間(-atime/天,-amin/分鐘):用戶最近一次訪問時間

修改時間(-mtime/天,-mmin/分鐘):文件最後一次修改時間

變化時間(-ctime/天,-cmin/分鐘):文件元數據最後一次修改時間

以atime爲例,-atime 3表示訪問時間範圍爲≥3天<4天

[root@www lab]# find /lab -mtime -3 –ls<-- 找出3天內的文件

297972 0 -rw-rw-rw- 1 root root 0 9月 9 06:20 /lab/jjj    

[root@www lab]# date

2015年 09月 12日 星期六 05:41:29 CST

[root@www lab]# touch -m -t 201509080101 abc

[root@www lab]# find /lab -mtime +3 –ls <-- 找出4天前的文件

4770 4 -rw-r--r-- 1 root root 143 9月 7 23:05 /lab/testing

297967 4 -rw-r--r-- 1 root root 15 8月 28 21:04 /lab/inpub_txt

297966 4 -rw-r--r-- 1 root root 51 8月 28 20:29 /lab/var.err

297971 4 -rwxr-xr-x 1 Oracle agetest 113 9月 5 20:44 /lab/t2

297975 0 -rw-r--r-- 1 root root 0 9月 8 01:01 /lab/abc

297969 4 -rw-r--r-- 1 root root 51 9月 7 06:21 /lab/character.txt

297970 4 -rw-rw-rw- 1 Oracle agetest 170 9月 5 20:45 /lab/t3

297974 4 -rw-r--r-- 1 root root 114 9月 5 21:10 /lab/t1

w 根據權限查找

-perm [/|-]MODE

MODE:精確權限匹配

[root@www ~]# find /etc -perm 400 -ls

404218 4 -r-------- 1 root root 45 8月 17 19:37 /etc/openldap/certs/password

/MODE:任何一類對象(u,g,o)的任何一位權限符合條件即可

[root@www 123]# ll

總用量 4

drwxr-xr-x. 3 root root 4096 9月 6 09:54 456

-r--r--r--. 1 root root 0 9月 12 09:11 aa

-rw-r-----. 1 root root 0 9月 12 09:11 bb

-rw-rw----. 1 root root 0 9月 12 09:11 cc

-rwxrwxrwx. 1 root root 0 9月 12 09:11 dd

-rw-r--r--. 1 root root 0 9月 12 09:11 ee

[root@www 123]# find . -perm /020 -ls

554308 0 -rw-rw---- 1 root root 0 9月 12 09:11 ./cc

554309 0 -rwxrwxrwx 1 root root 0 9月 12 09:11 ./dd

-MODE:每一類對象指定的每位權限都必須同時存在

[root@www 123]# find . -perm -201 -ls

554303 4 drwxr-xr-x 3 root root 4096 9月 12 09:11 .

554304 4 drwxr-xr-x 3 root root 4096 9月 6 09:54 ./456

554305 4 drwxr-xr-x 2 root root 4096 9月 6 09:54 ./456/789

554309 0 -rwxrwxrwx 1 root root 0 9月 12 09:11 ./dd

處理動作:

-print:默認處理動作

-ls: 類似於對查找到的每個文件做"ls -l"操作

-delete: 刪除查找到的文件

-fls /path/to/somefile: 查找到的文件的詳細路徑信息保存至指定文件中

-ok COMMAND {} \;

對每個文件執行指定的命令之前需要用戶事先確認

-exec COMMAND {} \;

無需用戶確認

{} 用於與-exec選項結合使用來匹配所有文件,然後被替換爲相應的文件名

[root@www 123]# find . -perm -201 -ls

554303 4 drwxr-xr-x 3 root root 4096 9月 12 09:11 .

554304 4 drwxr-xr-x 3 root root 4096 9月 6 09:54 ./456

554305 4 drwxr-xr-x 2 root root 4096 9月 6 09:54 ./456/789

554309 0 -rwxrwxrwx 1 root root 0 9月 12 09:11 ./dd

[root@www 123]# find . -perm -201 -fls /lab/perm.txt

   
[root@www 123]# find . ! -perm /111 -exec ls -l {} \;

-rw-r--r--. 1 root root 0 9月 12 09:11 ./ee

-rw-rw----. 1 root root 0 9月 12 09:11 ./cc

-rw-r-----. 1 root root 0 9月 12 09:11 ./bb

-r--r--r--. 1 root root 0 9月 12 09:11 ./aa    

[root@www 123]# find . ! -perm /111 -exec mv {} {}.old \;

[root@www 123]# ll

總用量 4

drwxr-xr-x. 3 root root 4096 9月 6 09:54 456

-r--r--r--. 1 root root 0 9月 12 09:11 aa.old

-rw-r-----. 1 root root 0 9月 12 09:11 bb.old

-rw-rw----. 1 root root 0 9月 12 09:11 cc.old

-rwxrwxr-x. 1 root root 0 9月 12 09:11 dd

-rw-r--r--. 1 root root 0 9月 12 09:11 ee.old

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章