[关闭]
@yanglt7 2018-01-28T13:55:15.000000Z 字数 3583 阅读 822

Linux笔记 9

Linux


指令与文件的搜寻

1. 指令档名的搜寻

which 寻找【执行文件】
根据 [PATH] 这个环境变量所规范的路径,去搜寻【执行文件】的完整文件名。


例1:搜寻 ifconfig 的完整文件名
[root@study ~]# which ifconfig
/sbin/ifconfig

例2:用 which 找 which
[root@study ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/bin/alias
/usr/bin/which

例3:搜寻 history 的完整文件名
[root@study ~]# which history
/usr/bin/which: no history in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

[root@study ~]# history --help
-bash: history: --: invalid option
history: usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]

2. 档案档名的搜寻

whereis (由一些特定目录中寻找档案档名)

选项 参数
-l 可以列出 whereis 会去查询的几个主要目录
-b 只找 binary 格式的文件
-m 只找在说明档 manual 路径下的文件
-s 只找 source 来源文件
-u 搜寻不在上述三个项目当中的其他的特殊文件


例1:找出 ifconfig 的完整文件名
[root@study ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz

例2:只找跟 passwd 有关的【说明文件】的完整文件名(man page)
[root@study ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

[root@study ~]# whereis -m passwd
passwd: /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

lacate updatedb

选项 参数
-i 忽略大小写的差异
-c 不输出档名,仅计算找到的文件数量
-l 仅输出几行的意思,例如输出5行即是 -i 5
-S 仅输出 locate 所使用的资料库文件的相关资讯,包括该资料库记录的文件/目录数量等
-r 后面可接正则表达式的显示方式


例1:找出系统中所有与 passwd 相关的文件名且只列出5个
[root@study ~]# locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd

例2:列出 locate 查询所使用的资料库档案之档名与各资料数量
[root@study ~]# locate -S
Database /var/lib/mlocate/mlocate.db:
14,154 directories
150,064 files
7,609,620 bytes in file names
3,435,811 bytes used to store database

find

a. 与时间有关的选项: -atime, -ctime 与 -mtime,以 -mtime 说明

选项 参数
-mtime n n 为数字,意义为在 n 天之前的【一天之内】被更动过内容的文件
-mtime +n 列出在 n 天之前(不含 n 天本身)被更动过内容的文件
-mtime -n 列出在 n 天之内(含 n 天本身)被更动过内容的文件
-newer file file 为一个存在的文件,列出比 file 更新的文件名


例1:将过去系统上24小时内更动过内容的文件列出
[root@study ~]# find / -mtime 0

例2:寻找 /etc 底下的文件,如果文件日期比 /etc/passwd 新就列出
[root@study ~]# find /etc -newer /etc/passwd

+4 4 -4
7-5 5-4 4-现在

b. 与使用者或群组名称有关的参数

选项 参数
-uid n n 为使用者帐号 ID,即UID。记录在 /etc/passwd 里面与帐号名称对应的数字
-gid n n 是群组名称的 ID,即GID。记录在 /etc/group
-user name name 为使用者帐号名称
-group name name 为群组名称
-nouser 寻找文件的拥有者不存在 /etc/passwd 的人
-nogroup 寻找文件的拥有群组不存在于 /etc/group 的文件


例3:搜寻 /home 下属于 ylt 的文件
[root@study ~]# find /home -user ylt

例4:搜寻系统中不属于任何人的文件

c. 与档案权限及名称有关的参数

选项 参数
-name filename 搜寻文件名为 filename 的文件
-size [+-]SIZE 搜寻比 SIZE 大(+)或小(-)的文件。SIZE 的规格:c:byte,k:1024bytes
-type TYPE 搜寻文件类型为 TYPE 的,类型主要有:一般正规文件(f),装置档案(b,c),目录(d),连结档(l),sockets(s),FIFO(p)
-perm mode 搜寻档案权限刚好等于 mode 的档案。例如,-rwsr-xr-x 的属性为4755
-perm -mode 搜寻档案权限囊括 mode 的档案。
-perm /mode 搜寻档案权限包含任一 mode 的档案


例5:找出档名为 passwd 这个档案
[root@study ~]# find / -name passwd

例5-1:找出档名包含了 passwd 这个关键字的档案
[root@study ~]# find / -name "星号passwd星号"

例6:找出 /run 目录下,档案类型为 Socket 的档案有哪些
[root@study ~]# find /run -type s

例7:搜寻档案中含有 SGID 或 SUID 或 SBIT 的属性
[root@study ~]# find / -perm /7000

例8:找出 /user/bin /user/sbin 这两个目录下,只要具有 SUID 或 SGID 就列出该档案
[root@study ~]# find /user/bin /user/sbin -perm /6000

d. 额外可进行的操作

选项 参数
-exec command command 为其他指令, -exec 后面可以再接额外的指令来处理搜寻到的结果
-print 将结果打印到屏幕上,这个动作是预设动作


例9:将例8找到的档案使用 ls -l 列出来
[root@study ~]# find /user/bin /user/sbin -perm /6000 -exec ls -l {} \;
(指令不支持命令别名,所以,不能用ll)

例10:找出系统中大于 1 MB 的档案
[root@study ~]# find / -size +1M

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注