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

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

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


find -type f -name "*.txt"
其中-type为文件类型,f:文件,d:文件夹,l:链接文件


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)

后缀可以这样做find ./ -name ".txt" -o ".jpg"

其他的 来看看这里 戳这里

【热门文章】
【热门文章】