系统中查找文件

使用locate

locate
。查询系统中已编译的数据库路径的文件
    。数据库必须由管理员更新
    。查询完整路径,而不是文件名
。只能查询用户有读写权限的目录
locate示例:
。locate foo   //查找文件名或路径为‘foo’
。locate -r ‘.foo$'    //使用正则表达式查找以“.foo"结尾的文件
有用的选项:
。-i执行不区分大小写的查询
。-nX列出首先匹配的X个的 

使用find

find
。find 【目录...】【条件...】
。实时的查询目录树
    。速度慢但比locate更精确
    。如果没有给定查询的开始的路径将使用当前路径
    。如果没有给定条件所有文件都匹配
。可以对找到的文件执行命令
。只可以查询用户具有读写权限的目录
基础find示例
。find -name snow.png    //查询snow.png文件
。find -iname snow.png      //忽略大小写查询方式
。find -user joe -group joe    //查询属于用户joe和组joe的文件
以上命令结果等同于#find ./ -user joe -a -group joe   //-a参数是并且的意思
#find /etc /tmp /usr -iname "passwd" //从以上目录查找passwd文件
find和逻辑操作
。缺省条件之间相”与“逻辑
。可以使用-o和-not为”或“和”反“
。括号可以决定逻辑次序,但必须在bash中转义
    find -user joe -not -group joe
    find -user joe -o -user jane
    find -not (-user joe -o -user jane)
find和权限
。可以通过名字或id匹配所有者
    。find / -user joe -o -uid 500
。可以匹配十进制或符号权限
    #find -perm 755     //匹配权限正好为755文件
    #find -perm +222    //+或者的意思,匹配三个权限为ow|gw|otherw
    #find -perm -222    //-并且的意思,匹配三个权限为ow&gw&otherw
    #find -perm -002    //匹配其他人可以写权限的文件
临时创建一些文件:

[root@eurm tmp]# touch {4,5,6,7}_{4,5,6,7}_{4,5,6,7,0}
[root@eurm tmp]# ls
4_4_0 4_5_0 4_6_0 4_7_0 5_4_0 5_5_0 5_6_0 5_7_0 6_4_0 6_5_0 6_6_0 6_7_0 7_4_0 7_5_0 7_6_0 7_7_0
4_4_4 4_5_4 4_6_4 4_7_4 5_4_4 5_5_4 5_6_4 5_7_4 6_4_4 6_5_4 6_6_4 6_7_4 7_4_4 7_5_4 7_6_4 7_7_4
4_4_5 4_5_5 4_6_5 4_7_5 5_4_5 5_5_5 5_6_5 5_7_5 6_4_5 6_5_5 6_6_5 6_7_5 7_4_5 7_5_5 7_6_5 7_7_5
4_4_6 4_5_6 4_6_6 4_7_6 5_4_6 5_5_6 5_6_6 5_7_6 6_4_6 6_5_6 6_6_6 6_7_6 7_4_6 7_5_6 7_6_6 7_7_6
4_4_7 4_5_7 4_6_7 4_7_7 5_4_7 5_5_7 5_6_7 5_7_7 6_4_7 6_5_7 6_6_7 6_7_7 7_4_7 7_5_7 7_6_7 7_7_7

find和数字条件
。可以查找数值条件
    find -size 1M      //查找文件大小正好1M的  
。find -size +1024K    //文件大小超过1M
。find -size -1024K     //文件大小小于1M的
dd if=/dev/zero   of=test.txt
以上的if为input file输入文件,of为output file,每台机器都有/dev/zero设备,并且一直输入数据。我们可以利用这个原理克隆光盘如,dd if=/dev/cdrom of=winxp.iso
[root@eurm tmp]# dd if=/dev/zero of=./33.txt bs=1M count=33
记录了33+0 的读入
记录了33+0 的写出
34603008字节(35 MB)已复制,0.289918 秒,119 MB/秒
[root@eurm tmp]# ll
总用量 33792
-rw-r--r--. 1 root root 34603008 6月   2 08:50 33.txt
find和访问时间
。find可以节电的时间戳
    -atime文件是何时最后被访问|access time
    -mtime文件数据何时最后被修改|modify time
    -ctime文件数据或属性何时最后被修改|change time
。给定的值以天为单位
    find -ctime -10
    。十天内被修改的文件
#status filename    //查看文件属性
[root@eurm tmp]# stat 33.txt 
  File: "33.txt"
  Size: 34603008  	Blocks: 67584      IO Block: 4096   普通文件
Device: fd00h/64768d	Inode: 524291      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-06-02 08:50:32.016052524 +0800
Modify: 2020-06-02 08:50:32.299052075 +0800
Change: 2020-06-02 08:50:32.299052075 +0800

以上的Access的时间随访问时间变化而变化,使用vim最开始打开的时间也记录access时间
modify:仅仅使用vim编辑修改文件内容才会变化
Change:使用chown,chgrp等会发生变化,使用vim编辑保存的时间  
find执行命令
。在找到文件后可以执行命令
    。命令之前必须存在-exec或-ok
        。-ok操作每个文件之前提示
    。命令必须以空格;结尾
    。可以使用{}括号代表文件名
    。find -size +102400k -ok gzip{};

以上截图是找到nouser属性的文件,然后执行删除操作;当使用-ok的时候会弹出交互方式,-exec会直接执行删除操作。 

find执行实例
。find -name ”*.conf" -exec cp {} {}.orig ;
    备份配置文件,加上.orig后缀
。find /tmp -ctime +3 -user joe -ok rm {} ;
    提示溢出超过3天joe的临时文件
。find ~ -perm -002 -exec chmod o-w {} ;
    修复在你的用户主目录中其他人可以写的文件

[root@eurm tmp]# cd config/
[root@eurm config]# ll
总用量 196
-rw-r--r--. 1 root root   148 6月   2 11:41 asound.conf
-rw-------. 1 root root   232 6月   2 11:41 autofs_ldap_auth.conf
-rw-r--r--. 1 root root  1780 6月   2 11:41 cas.conf
-rw-r--r--. 1 root root   812 6月   2 11:41 cgconfig.conf
-rw-r--r--. 1 root root  1705 6月   2 11:41 cgrules.conf
-rw-r--r--. 1 root root   161 6月   2 11:41 cgsnapshot_blacklist.conf
-rw-r--r--. 1 root root   519 6月   2 11:41 dracut.conf
-rw-r--r--. 1 root root  1093 6月   2 11:41 elinks.conf
-rw-r--r--. 1 root root    20 6月   2 11:41 fprintd.conf

[root@eurm config]# find ./ -name "*.conf" -exec echo {} {}.$(date +%F) ;  //可以把echo改成cp,实现自动备份功能
./elinks.conf ./elinks.conf.2020-06-02
./cgsnapshot_blacklist.conf ./cgsnapshot_blacklist.conf.2020-06-02
./logrotate.conf ./logrotate.conf.2020-06-02
./updatedb.conf ./updatedb.conf.2020-06-02
./nsswitch.conf ./nsswitch.conf.2020-06-02
./sudo.conf ./sudo.conf.2020-06-02
./rsyslog.conf ./rsyslog.conf.2020-06-02
./kdump.conf ./kdump.conf.2020-06-02
./sudo-ldap.conf ./sudo-ldap.conf.2020-06-02  

使用#man find查找搜索深度

[root@server0 ~]# find  /  -maxdepth   2  -name  '*.conf'

find ./ -nouser使用场景:

如有一个用户test1创建了一个文件test1.txt,然后这个用户被删除了,会发现这个文件的属性显示的是test1的uid,但是如果我们删除了很多这种类型的用户,我们可以通过

#find ./ -uid 3000(test1用户的uid)  //只能查找到test1用户的

#find  ./ -nouser可以查看出所有被删除用户的权限   

  

  

使用图形查询工具

原文地址:https://www.cnblogs.com/hongjinping/p/13056479.html