前端analysis | 3w & 1h

《Linux》- shell 搜索命令

2020-09-18

linux 命令

查看命令帮助

help info man or –help ,功能强度排序:
help(只能内置命令) < man < info

内置命令

man help 查看内置命令列表

1
2
3
4
5
6
bash,  :,  .,  [,  alias,  bg, bind, break, builtin, caller, cd, command,
compgen, complete, compopt, continue, declare,dirs, disown, echo,
enable, eval, exec, exit, export, false, fc, fg, getopts, hash,
help, history, jobs, kill, let, local, logout,
mapfile, popd, printf, pushd, pwd, read, readonly, return, set, shift,
shopt, source, suspend, test,imes, trap, true, type,typeset, ulimit, umask, unalias, unset, wait

搜索命令 - whereis

whereis命令,只能搜索系统命令, 不能搜索普通文件
位于: /usr/bin/whereis , 所有用户均可执行

1
2
3
4
5
6
7
8
9
10
  whereis [options] file
optioins:
-b search only for binaries
-m search only for manuals
-s search only for sources

# 查看到命令所在、帮助文档所在位置
> whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

  • 如果需要查找普通文件的内容,则需要使用find命令

搜索命令 - which

定位二进制文件、以及对于的别名
位于: /usr/bin/which, 所有用户均可执行

1
2
3
4
5
6
7
 # 语法格式
which [options] [--] COMMAND [...]

> which ls
alias ls='ls --color=auto'
/usr/bin/ls

文件搜索 - locate、find

locate
位于: /usr/bin/locate ,所有用户均可执行

1
2
locate [OPTION...] PATTERN

  • locate命令才是可以按照文件名搜索普通文件的命令。
  • 只能按照文件名来搜索文件,而不能执行更复杂的搜索,比如按照权限、大小、修改时间等搜索文件。
  • 如果要按照复杂条件执行搜索,则只能求助于功能更加强大的find命令
  • 因为locate命令不会直接搜索硬盘空间,而会搜索locate数据库,数据库在用户重新登录才更新。故新创建的文件,不会找到
    1
    2
    3
    # 手动更新数据库 更新物理机/var/lib/mlocate/mlocate.db
    # 云端服务器是不存在这个文件的
    $ updatedb
  • 如果执行updatdb还是搜索不到,就需要查看/etc/updatedb.conf配置文件,是否过滤了某些路径,文件

find
所在: /bin/find ,所有用户均可执行

  • 直接在硬盘中搜索,不推荐搜索范围过大,影响服务器性能

  • 搜索是精确匹配,非模糊匹配

    1
    2
    3
    find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

    find / -name test.conf
  • 文件大小搜索

    1
    2
    3
    4
    5
    6
    7
    # ll -h
    # 超过1k
    find . -size +1k
    # 不超过1M
    find . -size -1M
    # 按照字节搜索,默认512b
    find . -size 1027c
  • 文件按照时间搜索

    1
    2
    3
    4
    5
    6
    7
    8
    # n*24(2~3d)被访问的
    find . -atime 2

    # n*24小时以上(3d以前)新创建的
    find . -ctime +2

    # 3d内修改的
    find . -mtime -3
  • 权限搜索

    1
    2
    3
    4
    # 权限要求,逐渐减弱,一致→ 完全匹配 → 包含就行
    find . -perm 444
    find . -perm -444
    find . -perm +444
  • 按照所有者、所属组查询

    1
    2
    3
    4
    5
    6
    find . -user root 
    find . -group root
    find . -uid ...
    find . -gid ...
    find . -nouser
    find . -nogroup
  • 按照文件类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    find . -type d 

    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)

  • 逻辑运算符多条件查询

    1
    2
    3
    4
    5
    6
    -a and 
    -n not
    -o or

    find . -size +2k -a -type f
    find . -not -name test
  • find 结果查询 版本不同,语法不同

    1
    find . -perm +444 -exec ls -h
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏