开发者社区> 问答> 正文

如何根据文件类型去检索文件?

比如说一个目录下使用find检索文件,不检索二进制文件,或者只检索某一类型的文件,该使用什么命令?

展开
收起
a123456678 2016-06-22 14:32:16 1993 0
1 条回答
写回答
取消 提交回答
  • find -type 可以指定文件的类型;比如查找当前文件夹下的所有以a打头的目录(目录也是文件)

    find -type d -name "a*"
    find支持的文件检索类型可以区分普通文件和符号链接、目录等,但是二进制文件和文本文件无法直接通过find的类型区分出来;
    file命令可以检查文件具体类型(二进制或文本):
    
        $file redis-cli  # 二进制文件
        redis-cli: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
        $file redis.pid  # 文本文件
    redis.pid: ASCII text
    可以用以下命令组合来实现查找本地目录下的所有二进制文件:
    
    ls -lrt | awk '{print $9}'|xargs file|grep  ELF| awk '{print $1}'|tr -d ':'
    而文本文件可以这样查找:
    
    ls -lrt | awk '{print $9}'|xargs file|grep  ASCII| awk '{print $1}'|tr -d ':'
    可以到这里查看find更多的常见用法:
    http://linuxtools-rst.readthedocs.org/zh_CN/latest/base/03_text_proces...
    
    附:通过man find可以查看到find支持的文件检索类型:
    
     -type c
                  File is of type c:
    
                  b      block (buffered) special
    
                  c      character (unbuffered) special
    
                  d      directory
    
                  p      named pipe (FIFO)
    
                  f      regular file
    
                  l      symbolic link; this is never true if the -L option or the -follow option is in effect, unless the
                         symbolic link is broken.  If you want to search for symbolic links when  -L  is  in  effect,  use
                         -xtype.
    
                  s      socket
    
                  D      door (Solaris)
    2019-07-17 19:45:32
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载