• Linux之find命令


    一、介绍
    Linux下find命令在目录结构中搜索文件,并执行指定操作。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。


    二、使用方法
    1.命令格式:
    find pathname -options [-print ] [-exec -ok command ] {};
    2.命令参数:
    pathname:find命令所查找的目录路径。.来表示当前目录,/用来表示系统根目录
    -print:find命令将匹配的文件输出到标准输出
    -exec command {}: 将查到的文件执行command操作,{}和;之间有空格
    -ok 和 -exec 相同,只不过在操作前要询用户
    
    -name filename:查找名为filename的文件
    -perm:按执行权限来查找
    -user:按文件属主来查找
    -group:按组来查找
    -mtime -n +n:按文件更改时间来查找文件,-n指yn天以内,+n指n天以前
    -atime -n +n:按文件访问时间来查看
    -ctime -n +n:按文件创建时间来查找文件,-n 指n天以内,+n指n天以前
    -nogroup:查无有效属组的文件,即文件的属组在/etc/groups中不存在
    -nouser:查无有效属主的文件,即文件的属主在/etc/passwd中不存在
    -type: b/d/c/p/l/f:查找是块设备、目录、字符设备、管道、符号链接、普通文件
    -size:查找文件长度为n字节的文件
    -mount:查文件时不跨越文件系统mount点
    
    三、案例
    在查看root目录下文件名为txt结尾的文件并输出
    [root@ping ~]# find ~ -name "*.txt" -print
    /root/ping.txt
    

       在查找当前目录下文件名为txt结尾的文件并输出

    [root@ping ~]# find . -name "*.txt" -print
    ./ping.txt
    

       在查找当前目录下文件名以a到Z开头的文件并输出

    [root@ping ~]# find . -name "[a-Z]*" -print
    ./fqqltxt.zipv
    

       在查找/etc/目录下以host开头的文件并输出

    [root@ping ~]# find /etc -name "host*" -print
    /etc/hosts
    /etc/hosts.allow
    /etc/hosts.deny
    /etc/host.conf
    

       在查找rootl目录下查找以一个字母和一个数字开头的文件并输出

    [root@ping ~]# find ~ -name "[a-z][0-9].conf" -print
    /root/a9.conf
    

       在当前目录下查找权限为755的文件并输出

    [root@ping ~]# find . -perm 755 -print | tail -1
    ./ping.txt
    [root@ping ~]# ls -ld ping.txt/
    drwxr-xr-x. 2 root root 4096 12月 26 11:12 ping.txt/
    [root@ping ~]# find . -perm 755 -print -exec ls -l {} ;
    -rwxr-xr-x. 1 root root 981093 12月 18 15:13 ./nginx-1.12.1.tar.gz
    

       在当前目录下查找目录并输出

    [root@ping ~]# find . -type d -print
    ./.ssh
    

       在根目录下查找符号链接文件并输出

    [root@ping ~]# find / -type l -print
    /etc/httpd/run
    

     查看当前目录下大小为5kB的文件并输出

    [root@ping ~]# find . -size +10c -print -exec ls -lh {} ; | tail -1
    -rw-------. 1 root root 1.9K 12月 19 22:30 ./.mysql_history
    

     搜索以cron名开头的文件名,并以md5sum输出 

    [root@ping ~]# find /etc/cron* -type f -exec md5sum {} ; 
    

    搜索当前目录的第一层是普通文件,权限位是644的文件删除

    pingzimo@pingzhe:~$ find ./ -maxdepth 1 -type f -perm  664 | xargs ls -lh 
    -rw-rw-r-- 1 pingzimo pingzimo 28K 3月  30 20:35 ./ping
    pingzimo@pingzhe:~$ find ./ -maxdepth 1 -type f -perm  664 | xargs rm -f 
    

      

      



  • 相关阅读:
    IIS和ASP.NET2.0
    VS.NET里关于不能够使用向导的问题
    CodeFile
    判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)
    托管和非托管资源
    ASP.NET 2.0页面框架的几处变化
    导出QQWry.Dat中IP地址到文件[C#]
    面向对象在数据库应用程序中的应用(dotNet)
    如何取得IP/用户名等信息
    验证Email是否真正存在(上)
  • 原文地址:https://www.cnblogs.com/pingzhe/p/8117431.html
Copyright © 2020-2023  润新知