• Linux命令之find


    一、find条件查找

    1.find基本用法

    根据指定的条件在指定目录下搜索

    −用法:find [查找范围] [查找条件]

    −递归式查找,未指定范围时,默认从当前目录下查找

    2.按类型查找

    用法:-type 类型

    −普通文件f、目录d、快捷方式l

    −块设备b、字符设备c

    1. [root@hadoop opt]# ls -lh /boot/grub/menu.lst
    2. lrwxrwxrwx. 1 root root 11 Nov 16 19:35 /boot/grub/menu.lst -> ./grub.conf
    3. [root@hadoop opt]# find /boot -type d //查找目录
    4. /boot
    5. /boot/efi
    6. /boot/efi/EFI
    7. /boot/efi/EFI/redhat
    8. /boot/grub
    9. /boot/lost+found

    3.按名称查找

    用法:-name "名称"

    −允许*、?、[ ] 通配符,名称以双引号括起来

    组合多个条件时:-a:而且,为缺省项;-o:或者

    1. [root@hadoop opt]# find /etc -name "resol*.conf"
    2. /etc/resolv.conf
    3. [root@hadoop opt]# find /dev -type c -a -name "*y[1-3]"
    4. /dev/tty3
    5. /dev/tty2
    6. /dev/tty1

    4.按大小查找

    用法:-size [+|-]大小

    + 超过多大、-低于多大

    1. [root@hadoop opt]# find /boot -size +10M
    2. /boot/initramfs-2.6.32-431.el6.x86_64.img
    3. [root@hadoop opt]# ls -lh /boot/initramfs-2.6.32-431.el6.x86_64.img
    4. -rw-------. 1 root root 16M Nov 16 19:35 /boot/initramfs-2.6.32-431.el6.x86_64.img

    5.按修改时间查找

    用法:-mtime[+|-]天数

    + n天之前、-n天之内

    二、处理find找到的文档

    1.find输出的局限性

    输出的信息有限

    −仅列出文档所在的路径及名称

    −默认情况下,不会做进一步的处理操作

    1. [root@hadoop opt]# find /boot -size +3M
    2. /boot/initramfs-2.6.32-431.el6.x86_64.img
    3. /boot/vmlinuz-2.6.32-431.el6.x86_64

    2.常见的处理方式

     

    1. [root@hadoop opt]# find /boot -size +3M | wc -l
    2. 2
    3. [root@hadoop opt]# ls -lh $(find /boot -size +3M)
    4. -rw-------. 1 root root 16M Nov 16 19:35 /boot/initramfs-2.6.32-431.el6.x86_64.img
    5. -rwxr-xr-x. 1 root root 4.0M Nov 22 2013 /boot/vmlinuz-2.6.32-431.el6.x86_64

    3.逐个处理结果文件

    避免因参数太多导致异常

    −用法:find .. .. -exec 处理命令{} \;

    −其中{} 替代每一个查找结果,\; 表示处理操作结束

    1. [root@hadoop opt]# find /boot -size +3M -exec ls -lh {} \;
    2. -rw-------. 1 root root 16M Nov 16 19:35 /boot/initramfs-2.6.32-431.el6.x86_64.img
    3. -rwxr-xr-x. 1 root root 4.0M Nov 22 2013 /boot/vmlinuz-2.6.32-431.el6.x86_64

    三、根据文件属性查找

    1.忽略文件名大小写

    根据名称查找,忽略大小写

    −-iname

    1. [root@hadoop opt]# find /etc/ -iname "network"
    2. /etc/sysconfig/network
    3. /etc/rc.d/init.d/network

    2.指定文档归属

    根据账号名称或ID查找

    −-uid、-gid、-user、-group

    −-nouser、-nogroup

    1. [root@hadoop opt]# find /dev/ -group cdrom
    2. /dev/sg0
    3. /dev/sr0
    4. [root@hadoop opt]# ls -lh /dev/{sg1,sr0}
    5. crw-rw----. 1 root disk 21, 1 Apr 20 11:50 /dev/sg1
    6. brw-rw----. 1 root cdrom 11, 0 Apr 20 11:50 /dev/sr0

    3.指定文档i节点编号

    同一个文件系统内文件的i节点编号具有唯一性

    −当文件名存在乱码等现象时,方便定位文档

    −-inum

    1. [root@hadoop opt]# ls -li install.log*
    2. 397986 -rw-r--r--. 1 root root 9458 Nov 16 19:35 install.log
    3. [root@hadoop opt]# find ./ -inum 397986
    4. ./install.log

    四、限制查找深度

    1.最多查到第几层目录

    从指定目录(第1层)查找,最多深入到第x层目录

    −-maxdepthx层

    1. [root@hadoop opt]# find /etc -maxdepth 2 -name "res*.conf"
    2. /etc/resolv.conf
    3. /etc/selinux/restorecond_user.conf
    4. /etc/selinux/restorecond.conf //第3层目录及往下子目录不会被查找

    2.最少从第几层目录查找

    从第x层目录开始查找,略过少于x层的目录

    −-mindepthx层

    1. [root@hadoop opt]# find /etc -mindepth 2 -name "res*.conf"
    2. /etc/selinux/restorecond_user.conf
    3. /etc/selinux/restorecond.conf //第1层目录不会被查找
  • 相关阅读:
    64.【冒泡排序与选择排序与malloc()函数】
    魔百盒M301H_JL代工_ADB可自由开关_强刷固件(可救砖)
    python_04
    java计算机毕业设计企业公开招聘系统源码+数据库+lw文档+系统
    老卫带你学---leetcode刷题(33. 搜索旋转排序数组)
    FastDFS学习(三)
    python pyqt5 计算下载文件的进度百分比
    二维码智慧门牌管理系统升级解决方案:流量监控引领服务卓越
    百度之星题目记录
    B. Paranoid String
  • 原文地址:https://blog.csdn.net/m0_55834564/article/details/126319399