Linux系统下文件查找与检索 ——CentOS7

##需要掌握的命令内容

  • 掌握 which命令的用法
  • 掌握whereis命令的用法
  • 掌握locate命令的用法
  • 掌握grep命令的用法
  • 掌握find命令的用法

#使用的设备

  • CentOS 7

##文件查找与检索相关命令

which

which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

命令格式:which [-a] command

选项参数:

-a:将所有由PATH目录中找到的命令均列出,而不止第一个被找到的命令名称。

whereis

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

whereis命令会查找一个记录系统内文件信息的数据库文件,所以速度会很快。缺点是数据库文件并不是实时更新,默认情况一星期更新一次,因此如果最近删除的文件或者创建的文件是有可能错误的显示的。为了防止这种情况,可以手动的强制更新数据库文件。

命令格式:whereis [options] 文件或目录名

选项与参数说明
-l列出whereis会查询的几个主要目录
-b定位binary二进制格式的文件
-m定位帮助文件
-s定位源代码source文件
-u搜索默认路径下除可执行文件、源代码文件、帮助文件以外的其它文件

locate

locate可以快速的找到文件的位置,因为locate查找的数据是由已建立的数据库的文件来确定文件的位置,而并不是深入各个文件系统查找。一般情况下,数据库文件通过corntab自动更新,通常每天更新一次。

updatedb:根据/etc/updatedb.conf的设置去查找系统内的文件,并更新/var/lib/mlocate/内的数据库文件mlocate.db。

locate:依据/var/lib/mlocate/内的数据库记录,找出用户所输入关键词的文件名。

命令格式:locate [options] keyword

选项与参数说明
-i忽略大小写的差异
-c不输出文件名,仅计算找到的文件数量
-l限制输出的行数
-S输出locate所使用的数据库文件的相关信息,包括该数据库记录的文件或目录数量等。
-r使用正则表达式作为查找条件

命令格式:updatedb

功能:updatedb 命令用来创建或手动更新 locate 命令所必需的数据库文件。

updatedb 命令的执行过程较长,因为在执行时它会遍历整个系统的目录树,并将所有的文件信息写入 locate 数据库文件中。

grep

grep命令是用来过滤文件内容的命令,它能够使用正则表达式来搜索文本,并把结果显示出来

命令格式:grep [options] PATTERN 文件名或目录

选项与参数说明
-i忽略大小写的差异
-n对过滤的内容加上行号
-s不显示不存在或无匹配文本的错误信息
-v显示不包含匹配文本的所有行
^#以#开头
#$以#结尾
^$空行
|表示或者

find

Linux下find命令在目录结构中搜索文件,并执行指定的操作。即使系统中含有网络文件系统( NFS),只你具有相应的权限,find命令在该文件系统中同样有效。 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)

命令格式:find [PATH] [options] [action]

选项与参数说明
与文件名称及权限有关的参数:
-name FILENAME查找文件名称为FILENAME的文件
-size [+-]SIZE查找比SIZE大(+)或小(-)的文件。SIZE的规格有:c代表Bytes;k代表KB(1024Bytes)
-type TYPE查找TYPE类型的文件,类型主要有: f-普通文件;b-块设备文件;c-字符设备文件;d-目录文件;l-链接文件;s-socket文件;p-FIFO管道文件
-perm按照文件权限来查找文件
与时间有关的选项:
-amin n查找系统中最后N分钟访问的文件
-atime n查找系统中最后n*24小时访问的文件
-cmin n查找系统中最后N分钟被改变文件状态的文件
-ctime n查找系统中最后n*24小时被改变文件状态的文件
-mmin n查找系统中最后N分钟被改变文件数据的文件
-mtime n查找系统中最后n*24小时被改变文件数据的文件
与使用者或用户组名有关的参数:
-user按照文件的拥有者(属主)来查找文件
-group按照文件所属的组来查找文件
-nouser查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-nogroup查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

#尝试操作

1.请使用which命令在PATH变量指定的位置搜索命令grep的位置。

# bash
[admin@localhost bin]$ which grep #使用命令
alias grep='grep --color=auto'
        /usr/bin/grep

2.请查找passwd命令的可执行文件、配置文件以及帮助文件。

# bash
[admin@local ~]$ whereis passwd # 输入命令
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz

3.请使用updatedb手动更新文件数据库,列出locate查询所使用的数据库文件中记录的文件和目录数量,并使用locate命令查找/etc目录下以sh开头的文件。

因为文件数据库的更新有延迟,此方法一般不常使用,使用时候需要手动updatedb更新数据库.

# bash
[root@local admin]# updatedb # 手动更新数据库
[root@local admin]# locate -S # 列出locate查询所使用的数据库文件中记录的文件和目录数量
Database /var/lib/mlocate/mlocate.db:
        9,373 directories
        131,225 files
        6,327,542 bytes in file names
        2,902,826 bytes used to store database

[root@local admin]# locate /etc/sh # 使用locate命令查找/etc目录下以sh开头的文件
/etc/shadow
/etc/shadow-
/etc/shells

4.使用grep命令查找/etc目录下以shell开头且与shell大小写无关的文件及相应行和行号。

# bash
# 可以使用命令 
man grep #更全面
# 查看帮助文档
# bash
[root@local admin]# grep -i -n "^shell" /etc/* # 使用命令grep -i 忽略大小写 -n 显示行号 "^shell" 使用开头匹配shell 在etc文件下 
# 现在你会发现 此方法会输出许多的错误无用信息
grep: /etc/abrt: Is a directory
grep: /etc/alsa: Is a directory
grep: /etc/alternatives: Is a directory
/etc/anacrontab:5:SHELL=/bin/sh
grep: /etc/audisp: Is a directory
grep: /etc/audit: Is a directory
grep: /etc/auto.master.d: Is a directory
grep: /etc/avahi: Is a directory
grep: /etc/bash_completion.d: Is a directory
# 省略许多行

# 所以我们一般加上参数"-s"过滤无用的信息
# 示例
[root@local admin]# grep -i -n -s "^shell" /etc/*
/etc/anacrontab:5:SHELL=/bin/sh
/etc/crontab:1:SHELL=/bin/bash
/etc/services:274:shell           514/tcp         cmd             # no passwords used

5.使用grep命令显示/etc/samba/smb.conf文件中不包含注释行(以“#”开头的表示注释)的文本内容。

我们也是用上面的方法

# bash
[root@local admin]# grep -i -n -v "^#" /etc/samba/smb.conf # 显示不包含的使用函数 "-f" 
5:
6:[global]
7:      workgroup = SAMBA
8:      security = user
9:
10:     passdb backend = tdbsam
11:
12:     printing = cups
13:     printcap name = cups
14:     load printers = yes
15:     cups options = raw
16:
17:[homes]
18:     comment = Home Directories
19:     valid users = %S, %D%w%S
20:     browseable = No
21:     read only = No
22:     inherit acls = Yes
23:
24:[printers]
25:     comment = All Printers
26:     path = /var/tmp
27:     printable = Yes
28:     create mask = 0600
29:     browseable = No
30:
31:[print$]
32:     comment = Printer Drivers
33:     path = /var/lib/samba/drivers
34:     write list = @printadmin root
35:     force group = @printadmin
36:     create mask = 0664
37:     directory mask = 0775

# "$"符号表示已什么东西结尾
[root@local admin]# grep -i -n "yes$" /etc/samba/smb.conf #示例如下
14:     load printers = yes
22:     inherit acls = Yes
27:     printable = Yes

6.请在/etc目录下查找文件名为hosts的文件。

我们在这里使用 find 方法,find 实在目录结构中查找指定的文件,并且可以执行操作

# bash
[root@local admin]# find /etc/ -name hosts # find方法查找 根etc目录下的 名字为 hosts 的目录
/etc/hosts
/etc/avahi/hosts

7.请在/var目录下查找所有符号链接文件,并查找后显示这些文件的信息。

我们在这里同样使用 find 方法

# bash
[root@local admin]# find /var -type l -ls # 使用 -type 参数 列出使用符号链接的文件 最后加上动作 ls
50331725    0 lrwxrwxrwx   1 root     root            6 Dec  4  2022 /var/run -> ../run
50331726    0 lrwxrwxrwx   1 root     root           11 Dec  4  2022 /var/lock -> ../run/lock
50397058    0 lrwxrwxrwx   1 root     root           10 Dec  4  2022 /var/mail -> spool/mail

8.请在/etc目录下查找以d开头的,并且大小在100~200字节之间的文件,并显示这些文件的详细信息,观察这些文件的大小是否在限制的100~200字节之间。

# bash
[root@local admin]# find /etc/ -type f  -size +100c -size -200c -name "d*" -ls
# 添加参数要在动作前 size的参数后面需要记得添加单位
35113703    4 -rw-r--r--   1 root     root          195 Nov  3  2018 /etc/selinux/targeted/contexts/dbus_contexts
35113705    4 -rw-r--r--   1 root     root          148 Nov  3  2018 /etc/selinux/targeted/contexts/default_type
34034684    4 -rw-r--r--   1 root     root          116 Oct 31  2018 /etc/depmod.d/dist.conf

# 或使用

[root@local admin]# find /etc/ -size +100c -size -200c -name "d*" -type f -exec ls -l {} \; # 动作前面添加了参数 -exec 表明了这是一个动作 然后使用 {} 占位 \ 换行执行
-rw-r--r--. 1 root root 195 Nov  3  2018 /etc/selinux/targeted/contexts/dbus_contexts
-rw-r--r--. 1 root root 148 Nov  3  2018 /etc/selinux/targeted/contexts/default_type
-rw-r--r--. 1 root root 116 Oct 31  2018 /etc/depmod.d/dist.conf

9.请在/etc目录下查找修改时间在一周以内的文件,并显示系统当前日期时间以及查找到的文件的完整日期时间信息以便比对是否为一周内。

# bash
[root@local admin]# find /etc/ -type f -ctime -7 -exec ls --full-time {} \; # -ctime 表面删除的时间 -7 表示七天以内
-rw-r--r--. 1 root root 74 2025-10-15 10:11:07.300992816 +0800 /etc/resolv.conf
-rw-r-----. 1 root lp 409 2025-10-15 10:11:20.402992231 +0800 /etc/cups/subscriptions.conf.O
-rw-r-----. 1 root lp 409 2025-10-15 11:09:40.461835881 +0800 /etc/cups/subscriptions.conf
-rw-r--r--. 1 root root 1 2025-10-15 10:06:28.108999302 +0800 /etc/resolv.conf.save
-rw-r--r--. 1 root root 14 2025-10-15 10:08:34.287999652 +0800 /etc/tuned/active_profile
-rw-r--r--. 1 root root 5 2025-10-15 10:08:34.287999652 +0800 /etc/tuned/profile_mode

10.在/tmp目录下创建10 个文件 fileX.txt,X 为 1 到 10;修改这file1.txt-file8.txt这8个文件的时间为2019-01-01 08:08;请查找/tmp目录下所有30天前创建的file*.txt并将其删除,为了安全起见,删除前请确认。

创建文件:

# bash
[root@local admin]# touch /tmp/file{1..10}.txt # 使用正则表达式批量创建文件一到十
[root@local admin]# ll /tmp/file*
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file10.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file1.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file2.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file3.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file4.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file5.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file6.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file7.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file8.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file9.txt

修改文件时间:

# bash
[root@local admin]# touch -t 201901010808 /tmp/file{1..8}.txt # 修改1-8文件时间为2019
[root@local admin]# ll /tmp/file*
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file10.txt
-rw-r--r--. 1 root root 0 Jan  1  2019 /tmp/file1.txt
-rw-r--r--. 1 root root 0 Jan  1  2019 /tmp/file2.txt
-rw-r--r--. 1 root root 0 Jan  1  2019 /tmp/file3.txt
-rw-r--r--. 1 root root 0 Jan  1  2019 /tmp/file4.txt
-rw-r--r--. 1 root root 0 Jan  1  2019 /tmp/file5.txt
-rw-r--r--. 1 root root 0 Jan  1  2019 /tmp/file6.txt
-rw-r--r--. 1 root root 0 Jan  1  2019 /tmp/file7.txt
-rw-r--r--. 1 root root 0 Jan  1  2019 /tmp/file8.txt
-rw-r--r--. 1 root root 0 Oct 15 11:41 /tmp/file9.txt

查找30天前的文件并删除,且删除前确认:

# bash
[root@40zyz admin]# find /tmp -name "file*.txt" -mtime +30 -ls
18190089    0 -rw-r--r--   1 root     root            0 Jan  1  2019 /tmp/file3.txt
18361969    0 -rw-r--r--   1 root     root            0 Jan  1  2019 /tmp/file5.txt
18361976    0 -rw-r--r--   1 root     root            0 Jan  1  2019 /tmp/file7.txt
16777285    0 -rw-r--r--   1 root     root            0 Jan  1  2019 /tmp/file1.txt
17739290    0 -rw-r--r--   1 root     root            0 Jan  1  2019 /tmp/file2.txt
18361955    0 -rw-r--r--   1 root     root            0 Jan  1  2019 /tmp/file4.txt
18361971    0 -rw-r--r--   1 root     root            0 Jan  1  2019 /tmp/file6.txt
18361978    0 -rw-r--r--   1 root     root            0 Jan  1  2019 /tmp/file8.txt
[root@local admin]# find /tmp -name "file*.txt" -mtime +30 -ok rm {} \;
< rm ... /tmp/file3.txt > ? y
< rm ... /tmp/file5.txt > ? y
< rm ... /tmp/file7.txt > ? y
< rm ... /tmp/file1.txt > ? y
< rm ... /tmp/file2.txt > ? y
< rm ... /tmp/file4.txt > ? y
< rm ... /tmp/file6.txt > ? y
< rm ... /tmp/file8.txt > ? y
[root@local admin]# find /tmp -name "file*.txt" -mtime +30 -ls # 确认删除
# 结果为空

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇