find -mtime -type -iname -dev -perm

find的基本语法格式:

find  [查找位置]  [查找标准]  [处理动作]

查找位置:默认为当前目录,可以指定多个目录,多个之间用空格

查找标准:默认为查找指定目录下的所有文件

处理动作:显示到标准输出,默认为print

1)按文件修改时间查找文件

---(+n)----------|----------(n)----------|----------(-n)---
      (n+1)*24H前|   (n+1)*24H~n*24H间   |n*24H内

-ctime -n    查找距现在 n*24H 内修改过的文件
-ctime  n    查找距现在 n*24H 前, (n+1)*24H 内修改过的文件
-ctime +n    查找距现在 (n+1)*24H 前修改过的文件

[a|c|m]min     [最后访问|最后状态修改|最后内容修改]min
[a|c|m]time    [最后访问|最后状态修改|最后内容修改]time

linux 文件的三种时间(以 find 为例):
atime (Access time)访问时间,     如 cat more 等, 但 chmod, chown, ls, stat 等不会修改些时间, 使用 ls -utl 可以按此时间顺序查看;
ctime (Change time)状态修改时间, 如 chmod, chown 等状态时间改变但修改时间不会改变, 使用 stat file 可以查看;
mtime (Modify time)内容修改时间, 如 vi 保存后等, 修改时间发生改变的话, atime 和 ctime 也相应跟着发生改变
-----------------------------------------------------------------------------------------------------------------------------------
stat test.tar   查看文件test.tar    Access time,  Change time,             Modify time
                                    访问时间      状态修改时间(权限修改)   内容修改时间
  File: `test.tar'
  Size: 90705920  	Blocks: 177160     IO Block: 4096   regular file
Device: ca10h/51728d	Inode: 12          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-11-05 16:10:10.785593041 +0800
Modify: 2015-10-29 18:53:58.208466891 +0800
Change: 2015-11-05 16:14:54.323927288 +0800

ls -lu test.tar     显示的是 Access time -rw-r--r-- 1 root root 90705920 Nov  5 16:10 test.tar
ls -l  test.tar     显示的是 Modify time -rw-r--r-- 1 root root 90705920 Oct 29 18:53 test.tar
ls -lc test.tat     显示的是 Change time -rw-r--r-- 1 root root 90705920 Nov  5 16:14 test.tar
-----------------------------------------------------------------------------------------------------------------------------------
find /bak -ctime +6 -type d -exec rm -rf {} \;   删除/bak下6天以前创建的文件夹
find /bak -ctime +6 -type d -empty -exec rmdir {} \; >/dev/null 2>&1  删除/bak下超过6天的空文件夹,同时输出正确和错误信息到空
find . -name '*.log' -mtime +6       查找当前目录6天前log, -name后加单引号
find . -name '*' -type f -size 0c -exec rm -f {} \; 删除0字节文件

1.当前时间24小时—当前时间(昨天-今天)
#find . -mtime 0
2.当前时间48小时前–当前时间24小时前(前天-昨天)
#find . -mtime 1
3.当前时间48小时前(2天前)
#find . -mtime +1
4.当前时间24小时–当前时间(昨天-今天)
#find . -mtime -1

2)-iname "文件名称",根据文件名查找,不区分大小写
find -iname '*.jpg'  不区分大小写查找

3)文件的属主、属组
-user     "USERNAME"    :    根据属主查找
-group    "GROUP"       :    根据属组查找
-uid      "UID"         :    根据UID查找
-gid      "GID"         :    根据GID查找
-nouser                 :    查找没有属主的文件
-nogroup                :    查找没有属组的文件

find /dev/ -user root -a -group disk -ls   在/dev/目录下查找用户名为root 组为disk的文件,-ls表示用长列表显示出来
[root@owl /]# find / -nouser -nogroup                    # 没有属主,也没有属组 
/usr/local/src/nginx-1.9.2/man
[root@owl /]# ls -ld /usr/local/src/nginx-1.9.2/man      # 验证查找的文件是否符合要求,文件没有用户表示属主和属组,但会以ID号来表示 
drwxr-xr-x 2 1001 1001 4096 Jul 27 00:28 /usr/local/src/nginx-1.9.2/man

find / -user root -gid 500 #查找用户为root,gid为500

4)文件的类型 -type
f  文件
d  目录
b  块设备
c  字符设备
l  符号链接文件
p  命令管道文件
s  套接字文件

ls -l `find /bin/ -type l` 在/bin目录下查找文件类型为符号链接的文件,找到的结果用ls -l显示出来
find /usr/local -maxdepth 1 -type d  在/usr/local目录下查找文件夹,不查找子目录  但是结果包含/usr/local

5)文件的大小 -size
 #[k|M|G]: #表示数字,匹配的时候为模糊匹配
+#[k|M|G]: +表示大于,匹配的结果大于#
-#[k|M|G]: -表示小于,匹配的结果小于#

ls -lS `find /root -size +1M`在/root下查找大于1M的文件

6)文件权限
-perm [+|-] MODE
不带[+|-]表示精确权限匹配
+表示任何一类用户的任何一位权限匹配
-表示每类用户的每位权限都匹配

find /opt/ -perm  755 -ls  查找条件同时满足属主权限为7 属组权限为5 other的权限为5
find /opt/ -perm +764 -ls  用+后跟权限,匹配的原则是文件满足所有权限位的一个位就可以,权限为400的文件,满足属主为r,因为属主匹配条件为rwx

find在查找标准时支持组合查找条件,在使用组合查找条件时要考虑优先级,优先级为顺序为非→与→或,在组合参数是想改变优先级可以用()来提升优先级。

-a      (and)     与默认是与
-o      (or)      或
-not,!            非

find的处理动作可以是:

-print            默认为输出

-ls                显示查找到的文件的详细信息
-ok   COMMAND \;   要确认
-exec COMMAND \;   其中COMMAND中有对查找到的文件进行操作时,用{}来替代查找到的     文件,\;表示使用-exec的结束符,是固定格式; 使用-exec来出来查找的文件时,还可以通过管道送给xargs命令来解决,xargs在执行命令时,如果有对文件进行操作则用-i选项,{}也表示要操作的文件。

1、查找/var目录下属主为root并且属组为mail的所有文件; 
find /var -user root -group mail 
2、查找/usr目录下不属于root,bin,或student的文件; 
find /usr -not \( -user root -o -user bin -o -user student \) 
find /usr -not -user root -a -not -user bin -a -not -user student 
3、查找/etc目录下最近一周内内容修改过且不属于root及student用户的文件; 
find /etc -mtime -7 -a -not -user root -a -not -user student 
find /etc -mtime -7 -a -not \( -user root -o -user student \) 
4、查找当前系统上没有属主或属组且最近1天内曾被访问过的文件,并将其属主属组均修改为root; 
find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \; 
5、查找/etc目录下大于1M的文件,并将其文件名写入/tmp/etc.largefiles文件中; 
find /etc -size +1M -exec echo {} >> /tmp/etc.largefiles \; 
find /etc -size +1M >> /tmp/etc.largefiles 
6、查找/etc目录下所有用户都没有写权限的文件,显示出其详细信息; 
find /etc -not -perm +222 
7、删除类型不是目录,而且没有属主的文件; 
find / -not -type d -a -nouser -exec rm -f {} \; 
find / -not -type d -a -nouser | xargs -i rm -f {} 

假设当前时间是2013年1月5日00:00:00分,则:
1、寻找2013年1月5日00:00:00到2013年1月5日23:59:59被访问过的文件:时间单位是day,查找单位时间内的不带符号,当前无往前偏移,因此查询语句为:
find . -atime 0

2、寻找2013年1月4日23:50:00到2013年1月4日23:55:00被修改过的文件:时间单位是min,下限从当前往前偏移10min,上限从当前往前偏移5min,因此查询语句为:
find . -mmin -10 -mmin +5

 

转载于:https://my.oschina.net/direnjie/blog/542217

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值