文件查找:(grep,find,locate,whereis,file,whatis,which)
1. grep查找中符合条件的字符串
例1:在文件testfile中搜索匹配字符“test"
#grep ‘test’ testfile
例2:显示所有以t开头的文件中包含“test”的行数据内容
#grep ‘test’ t*
例3:显示在d1,d2文件中匹配“test”的行数据内容。
#grep ‘test’ d1 d2
例:在文件testfile中显示所有包含至少有5个连续小写字符的行数据内 容。
# grep ‘[a-z]\[5\]’ testfile
2. find:列出文件系统中符合条件的文件或目录,可以指定文件的 名称、类别、时间、大小以及权限等不同信息的组合,只有完全相 符的文件才会被列出来。
参数说明:
-name:按照文件名查找文件
-perm:按照文件权限来查找文件
-user:按照文件属主来查找文件
-group:按照文件所属组来查找文件
-cmin n:在过去n分钟内被修改过的文件
-ctime n:在过去n天内被修改过的文件
-size n:大小为n的文件
-type:查找某一类型的文件,在查找时可以定义不同的文件类型
(b 表块设备文件; d表目录; c表字符设备文件; p管道文件; l符号链接文件; f普通文件)。
例1:查找/boot 目录下的启动菜单配置文件grub.conf
# find /boot –name grub.conf ;
root@jyh-virtual-machine:/mytmp# find /boot –name grub.conf ;
/boot
/boot/System.map-5.19.0-41-generic
/boot/grub
/boot/grub/gfxblacklist.txt
/boot/grub/x86_64-efi
/boot/grub/x86_64-efi/acpi.mod
/boot/grub/x86_64-efi/help.mod
/boot/grub/x86_64-efi/cbmemc.mod
…………
运行结果可以看到文件grub.conf 文件的正确路径/boot/grub/grub.conf
例2:列出当前目录及其子目录下所有最近20天内更新过的文件。 #find .-ctime -20
root@jyh-virtual-machine:/mytmp# find . -ctime -20
.
./test0
./xiaoce1
./xiaoce3
./xiaoce2
./xiaoce4
./bashrc
例:查找根目录下的字符设备文件 #find / -type ‘c’
root@jyh-virtual-machine:/mytmp# find / -type 'c'
/dev/vcsa6
/dev/vcsu6
/dev/vcs6
/dev/vcsa5
/dev/vcsu5
/dev/vcs5
/dev/vcsa4
/dev/vcsu4
…………
3. locate在数据库中查找文件
例:查找etc目录下所有以sh开头的文件 #locate /etc/sh
root@jyh-virtual-machine:/mytmp# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
/snap/core20/1950/etc/shadow
/snap/core20/1950/etc/shells
/snap/core20/1974/etc/shadow
/snap/core20/1974/etc/shells
/snap/core22/750/etc/shadow
/snap/core22/750/etc/shells
/snap/core22/817/etc/shadow
/snap/core22/817/etc/shells
root@jyh-virtual-machine:/mytmp#
为避免locate找不到新创建的文件,可以使用updatedb立刻更新数据库(需要root用户)
4. whereis:查找指定文件、命令和手册页位置
命令选项含义:
-b:搜索文件的二进制部分;
-m:搜索文件的手册部分;
-s:搜索文件的源部分;
-u:搜索不寻常文件。
例:查找mv命令的二进制文件在什么目录下。
#whereis –b mv
例:查找mv的主要文件在什么目录下。
#whereis –m mv
root@jyh-virtual-machine:/mytmp# whereis -b mv
mv: /usr/bin/mv
root@jyh-virtual-machine:/mytmp# whereis -m mv
mv: /usr/share/man/man1/mv.1.gz
5. file查询文件类型
使用file能识别的文件类型有目录、shell脚本、英文文本、二进制 可执行文件、C语言源文件、文本文件、DOS的可执行文件。
参数说明:
-v:在标准输出后显示版本信息,并且退出;
-z:探测压缩过的文件类型;
-L:允许符号链接;
-f:从文件中读取要分析的文件名列表。
如查看当前目录下所有文件的文件类型 #file /mytmp/*
root@jyh-virtual-machine:/mytmp# file /mytmp/*
/mytmp/bashrc: ASCII text
/mytmp/test0: Unicode text, UTF-8 text
/mytmp/xiaoce1.txt: Unicode text, UTF-8 text
/mytmp/xiaoce2.c: C source, Unicode text, UTF-8 text
/mytmp/xiaoce3: ASCII text
/mytmp/xiaoce4: empty
root@jyh-virtual-machine:/mytmp#
6. whatis 查询命令功能
使用whatis命令可以查询命令的命令功能
如,查询ls命令的功能 #whatis ls
root@jyh-virtual-machine:/mytmp# whatis ls
ls (1) - list directory contents
root@jyh-virtual-machine:/mytmp# whatis cat
cat (1) - concatenate files and print on the stan...
root@jyh-virtual-machine:/mytmp# whatis whatis
whatis (1) - 显示在线手册页说明
root@jyh-virtual-machine:/mytmp# whatis file
FILE (3) - overview of system data types
file (1) - determine file type
7. which:显示可执行命令路径
使用which命令可以显示可执行命令的路径和它的别名。
如:显示ls命令的可执行命令的路径和它的别名。
root@jyh-virtual-machine:/mytmp# which ls
/usr/bin/ls
root@jyh-virtual-machine:/mytmp# which which
/usr/bin/which
root@jyh-virtual-machine:/mytmp# which whatis
/usr/bin/whatis
例题:
(1) 查看文件/root/.bashrc的类型
(2) 查询ifconfig和cd命令的完整文件名
(3) 只找出跟passwd有关的“说明文件”文件名
(4) 找出系统中所有与passwd相关的文件名
(5) 将过去系统上面3天前的24小时内有改动(mtime)的文件列出
(6) 使用find查找当前目录下比man.config新的文件,并存储到man.config.new文件。
(7) 列出/etc下比/etc/man.config新的文件
(8) 查找系统中所有属于root的文件及不属于任何人的文件
find / -user root
find / -nouser
(9) 使用find查找/下面所有名称为man.config的文件。
(10) 找出文件名为man.config的文件
(11) 使用locate查找所有名称为man.config的文件。
(12) 11步和12步的结果一样么?为什么?
答:结果不一样,locate更详细地列出了与manpath.config相关的文件
(13) 找出/etc下文件类型为f的文件
(14) 找出系统中大于1M的文件