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支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

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

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context CONTEXT


actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print 
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.

(2)which命令帮助手册

命令:which --help

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

Usage: /usr/bin/which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.

  --version, -[vV] Print version and exit successfully.
  --help,          Print this help and exit successfully.
  --skip-dot       Skip directories in PATH that start with a dot.
  --skip-tilde     Skip directories in PATH that start with a tilde.
  --show-dot       Don't expand a dot to current directory in output.
  --show-tilde     Output a tilde for HOME directory for non-root.
  --tty-only       Stop processing options on the right if not on tty.
  --all, -a        Print all matches in PATH, not just the first
  --read-alias, -i Read list of aliases from stdin.
  --skip-alias     Ignore option --read-alias; don't read stdin.
  --read-functions Read shell functions from stdin.
  --skip-functions Ignore option --read-functions; don't read stdin.

Recommended use is to write the output of (alias; declare -f) to standard
input, so that which can show aliases and shell functions. See which(1) for
examples.

If the options --read-alias and/or --read-functions are specified then the
output can be a full alias or function definition, optionally followed by
the full path of each command used inside of those.

Report bugs to <which-bugs@gnu.org>.

(3)whereis命令帮助手册

命令:whereis --help

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

Usage:
 whereis [options] file

Options:
 -b         search only for binaries
 -B <dirs>  define binaries lookup path
 -m         search only for manuals
 -M <dirs>  define man lookup path
 -s         search only for sources
 -S <dirs>  define sources lookup path
 -f         terminate <dirs> argument list
 -u         search for unusual entries
 -l         output effective lookup paths

For more details see whereis(1).

以上,感谢。

2022年11月28日

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中有多种用于查找文件和目录的指令,下面是对which、whereis、locate、find和grep指令的介绍以及示例: 1. which:用于查找可执行文件的路径。它会在环境变量$PATH指定的路径中搜索指定的命令,并返回第一个匹配到的完整路径。 示例:假设我们要查找命令ls的路径,可以使用以下命令: ``` which ls ``` 输出结果可能是:/bin/ls 2. whereis:用于查找二进制文件、源代码文件和帮助文档的路径。它会在特定的目录中搜索指定的文件,并返回相关信息。 示例:假设我们要查找命令ls的二进制文件、源代码文件和帮助文档的路径,可以使用以下命令: ``` whereis ls ``` 输出结果可能是:/bin/ls /usr/share/man/man1/ls.1.gz 3. locate:用于快速查找文件或目录。它会在系统预先建立的数据库中搜索匹配的文件或目录,并返回结果。 示例:假设我们要查找所有以.txt结尾的文件,可以使用以下命令: ``` locate *.txt ``` 输出结果可能是:/home/user/file1.txt /home/user/file2.txt /var/log/file3.txt 4. find:用于在指定目录下递归地搜索文件和目录。它可以根据不同的条件进行搜索,并返回匹配的结果。 示例:假设我们要在当前目录下查找所有以.txt结尾的文件,可以使用以下命令: ``` find . -name "*.txt" ``` 输出结果可能是:./file1.txt ./file2.txt ./dir1/file3.txt 5. grep:用于在文件搜索指定的模式。它可以根据正则表达式匹配文本,并返回匹配的行。 示例:假设我们要在文件file.txt中查找包含"hello"的行,可以使用以下命令: ``` grep "hello" file.txt ``` 输出结果可能是:This is a hello world example.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值