linux - 如何根据文件类型去检索文件?
阿神
阿神 2017-04-17 13:26:19
[Linux讨论组]
阿神
阿神

闭关修行中......

全部回复(3)
阿神

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)
PHP中文网

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

其他的 来看看这里 戳这里

怪我咯

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号