• Linux文件搜索命令find、which和whereis应用


    记录:349

    场景:在CentOS 7.9操作系统上,使用find命令在指定目录搜索文件,支持精确搜索和模糊搜索;使用which命令搜索一个命令所在的目录和别名信息。使用whereis命令搜索命令所在的目录和man帮助手册等相关的文件路径。

    版本:

    操作系统:CentOS 7.9

    1.find命令应用

    使用find命令在指定目录搜索文件,支持精确搜索和模糊搜索。

    符号说明:+/-符号表示大于/小于,不加则表示等于。

    (1)指定文件名称精确搜索(-name选项)

    格式:find 目录 -name 文件名称

    命令:find / -name ceph

    解析:在根目录/下,搜索文件名为ceph的内容。后面既包括文件名称也包括目录名称。

     (2)指定文件名称模糊搜索(-name选项)

    格式:find 目录 -name 文件名称

    命令:find / -name cep*

    命令:find / -name *cep*

    解析:在根目录/下,只要包含cep的都会搜索到。-name,后面既包括文件名称也包括目录名称。

    (3)指定用户名搜索(-user选项)

    格式:find 目录 -user 用户名称

    命令:find / -user learn

    解析:在根目录下,搜索用户learn拥有的目录和文件。

    (4)指定用户组搜索(-group选项)

    格式:find 目录 -group 用户名称

    命令:find / -group learn

    解析:在根目录下,搜索用户组learn拥有的目录和文件。

    (5)查找指定时间内被访问的文件(-amin选项)

    格式:find 目录 -amin 时间(单位分钟)

    命令:find / -amin -8

    解析:查出8分钟内被访问的文件。

    命令:find / -amin +8

    解析:查出8分钟前被访问的文件。

    (6)查找指定时间内文件属性改变的文件(-cmin选项)

    格式:find 目录 -cmin 时间(单位分钟)

    命令:find / -cmin -8

    解析:查出8分钟内文件属性被改变的目录和文件。

    命令:find / -cmin +8

    解析:查出8分钟前文件属性被改变的目录和文件。

    (7)查找指定时间内文件内容改变的文件(-mmin选项)

    格式:find 目录 -mmin 时间(单位分钟)

    命令:find / -mmin -8

    解析:查出8分钟内文件内容被改变的目录和文件。

    命令:find / -mmin +8

    解析:查出8分钟前文件内容被改变的目录和文件。

    (8)查找指定文件大小的文件(-size选项)

    单位说明:查找文件的默认单位是块(1块=512字节=0.5KB);单位有c(字节)、k(字节)、M(M字节)、G(G字节)。

    格式:find 目录 -size 文件大小

    命令:find / -size -8

    解析:查找小于8*0.5Kb字节的文件。

    命令:find / -size +8M

    解析:查找大于8M的文件。

    (9)查找满足多个条件的文件(-a选项,and)

    命令:find /home/apps/work/ -size +2k -a -size -6k

    解析:查找/home/apps/software/目录下,文件大小大于2k且小于6k的文件。

    (10)查找满足多个条件中的一个的文件(-o选项,or)

    命令:find /home/apps/work/ -size -3k -o -size +5k

    解析:查找/home/apps/software/目录下,文件大小大于2k且小于6k的文件。

    (11)查找指定文件类型的文件(-type选项)

    文件类型说明:f、d、l分别代表文件类型、目录类型和软链接文件。

    命令:find /home/apps/work/ -type f

    命令:find /home/apps/work/ -type d

    命令:find /home/apps/work/ -type l

    解析:-type f,查找文件类型;-type d,查找目录类型;-type l,查找软链接;

    (12)查找可执行的文件(-executable选项)

    命令:find /home/apps/work/  -executable

    解析:查找/home/apps/work/目录下可执行的文件。

    2.which命令应用

    使用which命令搜索一个命令所在的目录和别名信息。比如常用的ls命令,别名是ll。

    (1)查找命令完整路径

    命令:which cat

    解析:精准搜索cat命令所在完整路径,打印信息:/usr/bin/cat。

    (2)打印which命令版本

    命令:which --version

    解析:打印出which命令本身的版本。本例:GNU which v2.20。

    3.whereis命令应用

    使用whereis命令搜索命令所在的目录和man帮助手册等相关的文件路径。

    (1)查找命令全路径和man帮助手册

    命令:whereis cat

    解析:精准搜索cat命令所在完整路径。

    打印信息:cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz。

    (2)打印查找路径信息

    命令:whereis -l cat

    解析:会打印出查找cat命令时,搜索过的路径。

    (3)只搜索命令的man手册

    命令:whereis -m cat

    解析:只查找出cat命令的man手册。打印信息:cat: /usr/share/man/man1/cat.1.gz。

    4.命令帮助手册

    (1)find命令帮助手册

    命令:find --help

    解析:查看find支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

    1. Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
    2. default path is the current directory; default expression is -print
    3. expression may consist of: operators, options, tests, and actions:
    4. operators (decreasing precedence; -and is implicit where no others are given):
    5. ( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
    6. EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
    7. positional options (always true): -daystart -follow -regextype
    8. normal options (always true, specified before other expressions):
    9. -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
    10. --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race
    11. tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
    12. -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
    13. -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
    14. -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
    15. -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
    16. -readable -writable -executable
    17. -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
    18. -used N -user NAME -xtype [bcdpfls]
    19. -context CONTEXT
    20. actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
    21. -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
    22. -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
    23. -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;
    24. Report (and track progress on fixing) bugs via the findutils bug-reporting
    25. page at http://savannah.gnu.org/ or, if you have no web access, by sending
    26. email to .

    (2)which命令帮助手册

    命令:which --help

    解析:查看which支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

    1. Usage: /usr/bin/which [options] [--] COMMAND [...]
    2. Write the full path of COMMAND(s) to standard output.
    3. --version, -[vV] Print version and exit successfully.
    4. --help, Print this help and exit successfully.
    5. --skip-dot Skip directories in PATH that start with a dot.
    6. --skip-tilde Skip directories in PATH that start with a tilde.
    7. --show-dot Don't expand a dot to current directory in output.
    8. --show-tilde Output a tilde for HOME directory for non-root.
    9. --tty-only Stop processing options on the right if not on tty.
    10. --all, -a Print all matches in PATH, not just the first
    11. --read-alias, -i Read list of aliases from stdin.
    12. --skip-alias Ignore option --read-alias; don't read stdin.
    13. --read-functions Read shell functions from stdin.
    14. --skip-functions Ignore option --read-functions; don't read stdin.
    15. Recommended use is to write the output of (alias; declare -f) to standard
    16. input, so that which can show aliases and shell functions. See which(1) for
    17. examples.
    18. If the options --read-alias and/or --read-functions are specified then the
    19. output can be a full alias or function definition, optionally followed by
    20. the full path of each command used inside of those.
    21. Report bugs to .

    (3)whereis命令帮助手册

    命令:whereis --help

    解析:查看whereis支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

    1. Usage:
    2. whereis [options] file
    3. Options:
    4. -b search only for binaries
    5. -B <dirs> define binaries lookup path
    6. -m search only for manuals
    7. -M <dirs> define man lookup path
    8. -s search only for sources
    9. -S <dirs> define sources lookup path
    10. -f terminate <dirs> argument list
    11. -u search for unusual entries
    12. -l output effective lookup paths
    13. For more details see whereis(1).

    以上,感谢。

    2022年11月28日

  • 相关阅读:
    MATLAB2016笔记(六):数据可视化
    Java集合面试题&知识点总结(上篇)
    简单版 git快速上手和使用clone项目 新建/切换分支 提交修改
    Kafka入门到精通-阶段三(调优简介)
    【云原生之Docker实战】使用Docker部署mBlog微博系统
    AndroidStudio连不上adb报错ADB Connection Error
    第三篇 Ubuntu 20.04 搭建AI开发环境
    OC-消息转发
    【MyBatis-Plus】简介 | 入门案例
    博途PLC增量式PID(支持正反作用和归一化输出)
  • 原文地址:https://blog.csdn.net/zhangbeizhen18/article/details/128085951