zoukankan      html  css  js  c++  java
  • day 10 find文件查找练习题

    day 10 find文件查找练习题

    find文件查找

    1.find应用场景

    根据文件或目录名进行查找
    根据文件大小进行查找
    根据文件或目录修改时间进行查找
    根据文件或目录所属用户和用户组进行查找
    根据文件或目录权限进行查找

    2.find命令格式

    find [查找路径] [选项参数] [限定条件] [执行动作]

    3.find关键选项

    选项参数
    -maxdepth N #限制查找目录的层级

    限定条件
    -type f|d #查找的类型f表示文件,d表示目录
    -name #按名字查找
    -size -+kM #按文件大小查找,-表示小于+表示大于,小k大M
    -mtime -+N #按照文件修改时间进行查找

    4.find练习题

    测试环境:
    mkdir /opt/dir{1..4} -p
    touch -d "2021-09-22 01:00" /opt/dir1/111.txt
    touch -d "2021-09-23 01:00" /opt/dir1/112.txt
    touch -d "2021-09-24 01:00" /opt/dir1/113.txt
    touch -d "2021-09-25 01:00" /opt/dir1/114.txt
    touch -d "2021-09-26 01:00" /opt/dir1/115.txt
    touch -d "2021-09-27 01:00" /opt/dir1/116.jpg
    touch -d "2021-09-28 01:00" /opt/dir2/222.txt
    touch -d "2021-09-29 01:00" /opt/dir2/222.jpg

    dd if=/dev/zero of=/opt/dir1/111.data bs=1K count=100
    dd if=/dev/zero of=/opt/dir2/222.data bs=1K count=300
    dd if=/dev/zero of=/opt/dir2/555.data bs=1K count=500
    dd if=/dev/zero of=/opt/dir3/333.data bs=1M count=10
    dd if=/dev/zero of=/opt/dir4/444.data bs=1M count=100

    echo "DB_NAME=oldboy" >> /opt/dir4/444.conf
    echo "DB_PASSWD=123456" >> /opt/dir4/444.conf
    echo "www.baidu.com" >> /opt/dir2/222.html
    echo "www.oldboyedu.com" >> /opt/dir3/333.html

    useradd www
    chown www:www /opt/dir2/222.html
    chown www:www /opt/dir3/333.html

    4.1 按名称查找

    find /etc -type f -name 'ifcfg-eth0'
    find /etc -type f -name 'hostname'
    find /etc -type f -name 'eth0'
    find /etc -type f -name 'ifc
    '
    find /etc -type f -name 'eth0'
    find /etc -type f -name 'host*'

    4.2 按大小查找

    find /opt/ -type f -size +10M
    find /opt/ -type f -size -10M
    find /opt/ -type f -size +150k -size -500k
    find /opt/ -type f -size +300k -size -10M -name "555*"

    4.3 按类型查找

    find /etc/ -type l

    4.4 按时间查找
    find /opt -mtime 5 #查找第5天
    find /opt -mtime -5 #查找5天内
    find /opt -mtime +5 #查找5天前
    find /opt -type f -size -10k -mtime -3 -name "*.txt"

    4.5 限制查找目录的层级

    find /opt/dir1/ -maxdepth 2 -type f -name "*jpg"

    find /etc -maxdepth 1 -type f -name "host*"

    [root@centos7-100 opt]# cd /etc
    [root@centos7-100 etc]# find . -maxdepth 1 -type f -name "eth0"
    [root@centos7-100 etc]# find . -maxdepth 2 -type f -name "eth0"
    [root@centos7-100 etc]# find . -maxdepth 3 -type f -name "eth0"
    ./sysconfig/network-scripts/ifcfg-eth0

    5.find查找后执行动作

    5.1 -exec执行动作

    find . -type f -name ".txt" -exec ls -l {} ;
    find . -type f -name "
    .txt" -exec mv {} /tmp/ ;
    find . -type f -mtime +3 -exec rm -rf {} ;

    5.2 xargs执行动作

    find . -type f -name ".txt"|xargs ls -l
    find . -type f -name "
    .txt"|xargs mv -t /tmp/
    find . -type f -name ".txt"|xargs cp -t /tmp/
    find . -type f -mtime +3|xargs rm -rf
    find dir1 -type f -name "
    .txt" |xargs tar zcvf /opt/all.tar.gz

    5.3 exec和xargs区别

    exec是逐行处理,每个查找的结果都会处理一次
    xargs是打包处理,查找的内容一次性执行,只执行1次

    -exec执行的效果
    tar zcvf /opt/all.tar.gz 1.txt
    tar zcvf /opt/all.tar.gz 2.txt
    tar zcvf /opt/all.tar.gz 3.txt
    tar zcvf /opt/all.tar.gz 4.txt

    |xargs
    tar zcvf /opt/all.tar.gz 1.txt 2.txt 3.txt 4.txt

    6.find练习题汇总

    1.找出/opt目录下所有以txt结尾的文件
    find /opt -type f -name "*.txt"
    
    #2.找出/opt目录下所有以txt结尾的文件但是排除掉文件名包含222的文件
    find /opt -type f -name "*.txt" ! -name "*222*"
    find /opt -type f -name "*.txt"|grep -v "222"
    
    3.找出/opt/目录下所有的conf文件
    find /opt/ -type f -name "*conf*"
    
    4.找出/opt目录下大于500K的文件
    find /opt -type f -size +500k
    
    5.找出/opt目录下大于10M的文件
    find /opt -type f -size +10M
    
    6.找出/opt目录下大于200K但是小于50M的文件
    find /opt -type f -size  +200k -size —50M
    
    7.找出/opt目录下最近2天的文件
    find /opt -type f -mtime -2
    
    8.找出/opt的txt文件并且只保留5天以内的
    find /opt -type f -mtime +5 -exec rm {} ;
    find /opt -type f -mtime +5 -delete
    find /opt -type f -mtime +5 |xargs rm -rf {} ;=find /opt -type f -mtime +5 |xargs rm -rf
    
    9.找出/opt/dir2目录下以jpg结尾的文件
    find /opt/dir2 -type f -name "*jpg"
    
    #10.找出所有文件属于www用户的文件
    find /opt -type f -user "www"
    
    11.找出/opt/目录下文件内容包含baidu的文件
    find /opt -type f |xargs grep "baidu" =find /opt -type f |xargs grep {} "baidu" ;
    find /opt -type f -exec grep {} "baidu" ;
    
    #12.找出/opt目录下属于www用户并且文件内容包含oldboy的文件
    find /opt  -type f  -user "www" |xargs grep "oldboy"
    
    
    13.用一条命令找出/opt目录下所有的data文件并显示详细信息
    find /opt -type f -name "data" -exec ls -l {} ;
    find /opt -type f -name "data" |xargs ls -l
    
    14.用一条命令找出/opt目录下大于1M的文件并删除
    find /opt -type f -size +1M -delete
    find /opt -type f -size +1M -exec rm {} ;
    find /opt -type f -size +1M |xargs rm -rf 
    
    
  • 相关阅读:
    自定义view 画圆
    自定义view imageviw
    XML Stream
    Pull刷新加载
    Fragment事物
    Viewpager小圆点
    单个水波纹扩散
    多个彩色水波纹扩散效果
    自定义view
    获取sd的音乐文件
  • 原文地址:https://www.cnblogs.com/zhaocheng690/p/15358173.html
Copyright © 2011-2022 走看看