曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5288|回复: 4
打印 上一主题 下一主题

find 查找

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
跳转到指定楼层
楼主
发表于 2009-1-6 19:41:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
格式:
find [PATH] [option] [action]

選項與參數:

1. 與時間有關的選項:共有 -atime, -ctime 與 -mtime ,以 -mtime 說明
   -mtime  n :n 為數字,意義為在 n 天之前的『一天之內』被更動過內容的檔案;

   -mtime +n :列出在 n 天之前(不含 n 天本身)被更動過內容的檔案檔名;

   -mtime -n :列出在 n 天之內(含 n 天本身)被更動過內容的檔案檔名。

   -newer file :file 為一個存在的檔案,列出比 file 還要新的檔案檔名
                           -newer file1 !file2    查找比 file1 新但比 file2 旧的文件




-mtime 参数示例
[beyes@localhost ~]$ find ./ -mtime 0     # 0 表示从现在到 24 小时前
./
./pass.txt
./.lesshst
./.bash_history


找出 比 pass.txt 日期还新的文件

[beyes@localhost ~]$ find ./ -newer pass.txt
./
./.bash_history

[beyes@localhost ~]$ ls -l .bash_history
-rw------- 1 beyes beyes 15805 01-06 01:48 .bash_history
[beyes@localhost ~]$ ls -l pass.txt
-rwxr-xr-x 1 beyes beyes 161 01-06 01:21 pass.txt

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
沙发
 楼主| 发表于 2009-1-7 02:20:19 | 只看该作者
4. 額外可進行的動作:
   -exec command :command 為其他指令,-exec 後面可再接額外的指令來處理搜尋到
                                     的結果。
             命令形式如:find / -perm 7000 -exec ls -l {} \\;

   -print        :將結果列印到螢幕上,這個動作是預設動作! 

   -ok           :-ok 和 -exec 的作用相同,但以一种更安全的模式来执行该参数所给出的 shell 命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行

关于 -exec 在 man 手册中的说法:

 All following arguments to find are taken to be arguments to the command until  an  argument  consisting of ‘;’ is encountered.
 The string ‘{}’is replaced by the current file name being processed  everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.
:所有在 find 命令后面的参数都被当作是 command 的参数,直到遇到一个由 ; 组成的参数(如 \\; )为止。
        在每一个从参数到命令的转换中,字符串 '{}' 由当前正在被处理到的文件名所代替,而不仅仅是单独替换一个参数( {} ) 。


 -xargs
  在使用 find 命令的 -exec 选项处理匹配到的文件时,find 命令将所有匹配到的文件一起传递给 exec。不幸的是,有些系统对能够传递给 exec 的命令长度有限制,这样在 find 命令运行几分钟后,就会出现溢出的错误。错误信息通常是“参数列太长”或“参数列溢出”。这时候,选择 xargs 命令便可以解决这样的问题。特别是与 find 命令一起使用。 exec 会发起多个进程,而 xargs 不会多个,只有一个。





-exec 参数举例
[root@localhost ~]# find /home/beyes/Func_Test/ -type f -exec ls -l {} \\;
-rw-rw-r-- 1 beyes beyes 113 12-21 20:06 /home/beyes/Func_Test/F/fgetc/fgetc.c


-ok 参数举例
[root@localhost ~]# find . -name "*.log" -mtime +5 -ok rm {} \\;      #删除 5 天前的 Log 文件
< rm ... ./mplayer/configure.log > ?                                                      #这里提示是否要删除


-xargs 参数举例1

[root@localhost basic_test]# ll
总计 56
-rwxrwxr-x  1 beyes beyes   12 12-27 11:07 add_tst.txt
drwxr-xr-x 13 root  root  4096 01-02 01:05 evince
-rw-rw-r--  1 beyes beyes   29 12-27 12:33 lserr.txt
-rw-rw-r--  1 beyes beyes  152 01-01 22:19 pass.txt
-rw-rw-r--  1 beyes beyes  825 12-27 11:05 play_again2.c
-rw-rw-r--  1 beyes beyes  467 12-27 11:05 play_again3.c
-rw-rw-r--  1 beyes beyes   63 01-03 03:15 zhengze.txt
[root@localhost basic_test]# find ./ -perm -644 -print | xargs chmod g-w     # 这些文件的所属组没有写权限
[root@localhost basic_test]# ll
总计 56
-rwxr-xr-x  1 beyes beyes   12 12-27 11:07 add_tst.txt
drwxr-xr-x 13 root  root  4096 01-02 01:05 evince
-rw-r--r--  1 beyes beyes   29 12-27 12:33 lserr.txt
-rw-r--r--  1 beyes beyes  152 01-01 22:19 pass.txt
-rw-r--r--  1 beyes beyes  825 12-27 11:05 play_again2.c
-rw-r--r--  1 beyes beyes  467 12-27 11:05 play_again3.c
-rw-r--r--  1 beyes beyes   63 01-03 03:15 zhengze.txt


-xargs 参数举例2(查找并查看文件类型)
 [root@localhost shell]# find ./ -type f |xargs file
./case_select.sh:   Bourne-Again shell script text executable
./whilefilm.sh:     Bourne-Again shell script text executable
./dfuntil.sh:       Bourne-Again shell script text executable
./file_exec.sh:     Bourne-Again shell script text executable
./name.txt:         ASCII text
./for_test.sh:      Bourne-Again shell script text executable
./breakout.sh:      Bourne-Again shell script text executable
./ifcp.sh:          Bourne-Again shell script text executable
./salutation.sh:    Bourne shell script text executable
./whilereadline.sh: Bourne-Again shell script text executable
./val_quote.sh:     ASCII text
./readname.sh:      a /usr/bash script text executable
./iftest.sh:        Bourne-Again shell script text executable
./breakout2.sh:     Bourne-Again shell script text executable

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
板凳
 楼主| 发表于 2009-1-6 21:52:02 | 只看该作者
3. 與檔案權限及名稱有關的參數:
   -name filename:搜尋檔案名稱為 filename 的檔案;

   -size [+-]SIZE:搜尋比 SIZE 還要大(+)或小(-)的檔案。這個 SIZE 的規格有:
                   c: 代表 byte, k: 代表 1024bytes。所以,要找比 50KB
                   還要大的檔案,就是『 -size +50k 』

   -type TYPE    :搜尋檔案的類型為 TYPE 的,類型主要有:一般正規檔案 (f),
                   裝置檔案 (b, c), 目錄 (d), 連結檔 (l), socket (s),
                   及 FIFO (p) 等屬性(管道文件)。

   -perm mode  :搜尋檔案權限『剛好等於』 mode 的檔案,這個 mode 為類似 chmod
                 的屬性值,舉例來說, -rwsr-xr-x 的屬性為 4755 !

   -perm -mode :搜尋檔案權限『必須要全部囊括 mode 的權限』的檔案,舉例來說,
                 我們要搜尋 -rwxr--r-- ,亦即 0744 的檔案,使用 -perm -0744,
                 當一個檔案的權限為 -rwsr-xr-x ,亦即 4755 時,也會被列出來,
                 因為 -rwsr-xr-x 的屬性已經囊括了 -rwxr--r-- 的屬性了。

   -perm +mode :搜尋檔案權限『包含任一 mode 的權限』的檔案,舉例來說,我們搜尋
                 -rwxr-xr-x ,亦即 -perm +755 時,但一個檔案屬性為 -rw-------
                 也會被列出來,因為他有 -rw.... 的屬性存在!



-type 参数示例

[root@localhost ~]# find /var -type s        #查找 socket  文件
/var/lib/xend/relocation-socket
/var/lib/xend/xend-socket
......
[root@localhost ~]# find /var -type p        #查找 fifo 类型文件
/var/gdm/.gdmfifo
/var/run/autofs.fifo-net
/var/run/autofs.fifo-misc
.....


perm +mode 示例

[root@localhost ~]# find / -perm +7000
/usr/kerberos/bin/ksu
...
[root@localhost ~]# ls -l /usr/kerberos/bin/ksu
-rwsr-xr-x 1 root root 147731 10-06 01:00 /usr/kerberos/bin/ksu       #---s--s--t 中只要满足含有 s 或 t 就 列出

又如
find /bin /sbin -perm +6000              #------s--t 中只要满足含有 s 或 t 就 列出


-name 参数示例

[beyes@localhost ~]$ find ./ -name "[A-Z]*" -print     #在当前目录找出以大写字母开头的文件
./Demo/Demo_1
./.adobe/Flash_Player
.....

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
地板
 楼主| 发表于 2009-1-6 19:59:07 | 只看该作者
選項與參數:
2. 與使用者或群組名稱有關的參數:
   -uid n :n 為數字,這個數字是使用者的帳號 ID,亦即 UID ,這個 UID 是記錄在
            /etc/passwd 裡面與帳號名稱對應的數字。

   -gid n :n 為數字,這個數字是群組名稱的 ID,亦即 GID,這個 GID 記錄在
            /etc/group  

   -user name  :name 為使用者帳號名稱!

  -group name:name 為群組名稱喔,例如 users ;

   -nouser         :尋找檔案的擁有者不存在 /etc/passwd 的人!

   -nogroup       :尋找檔案的擁有群組不存在於 /etc/group 的檔案!
                當你自行安裝軟體時,很可能該軟體的屬性當中並沒有檔案擁有者,
                這是可能的!在這個時候,就可以使用 -nouser 與 -nogroup 搜尋。





-user 参数举例

find `pwd`  -user beyes       # 列出当前目录下属于 beyes 这个用户的所有文件;注意这里使用的 pwd 命令  


-nouser 参数说明
find  / -nouser        #如果删除了某个用户,但这个用户之前建立的文件还没删除的话,就会为 nouser 属性

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
5#
 楼主| 发表于 2009-1-6 19:54:04 | 只看该作者

mtime 时间表示法的区别

+find /var -mtime +4
find /var -mtime -4
/var -mtime 4

上面,4 , +4 , -4 区别见下图:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-6-18 07:32 , Processed in 0.067100 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表