文件查找

3.文件管理
本章同步视频:https://edu.51cto.com/sd/e4874
3.4 文件查找
3.4.1 命令查找
1.which - shows the full path of (shell) commands
(1)which 说明
which 命令用于查找并显示给定命令的绝对路径,环境变量 PATH 中保
存了查找命令时需要遍历的目录, which 命令会在环境变量$PATH 设
置的目录里查找符合条件的文件。也就是说,使用 which 命令就可以看
到某个系统指令是否存在,以及执行的命令位置。
(2)which 使用
[ tmp]# which date #列出找到的第一个结果
/bin/date
[ tmp]# which -a date #列出全部结果
/bin/date
/usr/bin/date
3.4.2 文档查找
1.whereis - locate the binary, source, and manual page files for
a command
[ tmp]# whereis date
date: /usr/bin/date /usr/share/man/man1/date.1.gz
/usr/share/man/man1p/date.1p.gz
[ tmp]# whereis -l #显示 whereis 查找的位置
[ tmp]# whereis -b date #只显示二进制文件
date: /usr/bin/date
[ tmp]# whereis -m date #只显示帮助文档
date: /usr/share/man/man1/date.1.gz
/usr/share/man/man1p/date.1p.gz
2.locate - find files by name
(1)locate 说明
locate(locate) 命令用来查找文件或目录。 locate 命令要比 find 快得
多,原因在于它不搜索具体目录,而是搜索一个数据库
/var/lib/mlocate/mlocate.db 。这个数据库中含有本地所有文件信息。
Linux 系统自动创建这个数据库,并且每天自动更新一次,因此,我们
在用 whereis 和 locate 查找文件时,有时会找到已经被删除的数据,
或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更
新。为了避免这种情况,可以在使用 locate 之前,先使用 updatedb
命令,手动更新数据库。整个 locate 工作其实是由四部分组成的:
? /usr/bin/updatedb 主要用来更新数据库,通过 crontab 自动完成的
? /usr/bin/locate 查询文件位置
? /etc/updatedb.conf updatedb 的配置文件
? /var/lib/mlocate/mlocate.db 存放文件信息的文件
(2)locate 用法
[ tmp]# locate date
[ tmp]# locate -c date #只显示结果数量
1643
[ tmp]# locate -S #显示 locate 数据库信息
Database /var/lib/mlocate/mlocate.db:
13,931 directories
152,748 files
7,576,250 bytes in file names
3,412,156 bytes used to store database
[ tmp]# locate -l 5 date #只显示前 5 条结果
/boot/grub2/i386-pc/date.mod
/boot/grub2/i386-pc/datehook.mod
/boot/grub2/i386-pc/datetime.mod
/etc/updatedb.conf
/etc/dbus-1/system.d/org.freedesktop.timedate1.conf
(3)更新 locate 的数据库
[ tmp]# updatedb
(4)缺陷
[ ~]# touch aaaaaa #新建文件
[ ~]# locate aaaaaa #找不到
[ ~]# updatedb #更新数据库
[ ~]# locate aaaaaa #找到了
/root/aaaaaa
3.4.3 find - search for files in a directory hierarchy
1.按时间查找
(1)与时间有关的用法:共有 -atime, -ctime 与 -mtime,以 -mtime 说

-mtime n :n 为数字,意义为在 n 天之前的『一天之内』被更动过内
容的档案;
-mtime +n :列出在 n 天之前(不含 n 天本身)被更动过内容的档案档名;
-mtime -n :列出在 n 天之内(含 n 天本身)被更动过内容的档案档名。
-newer file :file 为一个存在的档案,列出比 file 还要新的档案档名
(2)用法
[ tmp]# touch xxxxx #新创建一个文件
[ tmp]# find /tmp/ -mtime -1
/tmp/
/tmp/xxxxx
#查找/tmp 目录下 1 天内修改过的文件
[ tmp]# ls
passwd root test xxxxx
[ tmp]# find /tmp/ -mtime +1
/tmp/root #结果已去掉隐藏文件
/tmp/test
/tmp/passwd
#查找/tmp 目录下 1 天前修改过的文件
[ tmp]# timedatectl set-time "2020-3-18 19:56:30"
#修改系统时间
[ tmp]# date #当前时间为“明天”
Wed Mar 18 19:56:33 CST 2020
[ tmp]# find . -mtime 1
.
./xxxxx
#查找在当前目录下昨天修改过的文件
[ tmp]# touch xxx
[ tmp]# find . -newer xxxxx
.
./xxx
#查找在当前目录下比 xxxxx 要新的文件
2.按用户查找
(1)与使用者或组名有关的参数:
? -uid n :n 为数字,这个数字是用户的账号 ID,亦即 UID ,这个
UID 是记录在 /etc/passwd 里面与账号名称对应的数字。
? -gid n :n 为数字,这个数字是组名的 ID,亦即 GID,这个 GID
记录在 /etc/group
? -user name :name 为使用者账号名称喔!例如 dmtsai
? -group name:name 为组名喔,例如 users ;
? -nouser:寻找档案的拥有者不存在 /etc/passwd 的人!
? -nogroup:寻找档案的拥有群组不存在于 /etc/group 的档案!
(2)用法
[ tmp]# ll
total 8
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
-rwxrw-r--. 1 root root 0 Mar 5 10:32 root
-rw-rw-r--. 1 stu stu 0 Mar 18 20:10 stu
-rw-r--r--. 1 root root 65 Mar 12 11:04 test
[ tmp]# find . -uid 1000
#在当前目录下查找 uid 为 1000 的文件
./calf
[ tmp]# find . -gid 1000
#在当前目录下查找 gid 为 1000 的文件
./calf
[ tmp]# find . -user calf
#在当前目录下查找用户名为 calf 的文件
./calf
[ tmp]# find . -group calf
#在当前目录下查找组名为 calf 的文件
./calf
[ tmp]# userdel stu
#删除用户和用户组 stu
[ tmp]# find . -nouser
#在当前目录下查找无主文件
./stu
[ tmp]# find . -nogroup
#在当前目录下查找无组文件
./stu
3.按文件大小查找
(1) 按文件大小查找
-size [+-]SIZE:搜寻比 SIZE 还要大(+)或小(-)的档案。
c‘ for bytes<br/>w‘ for two-byte words
k‘ for Kilobytes (units of 1024 bytes)<br/>M‘ for Megabytes
`G‘ for Gigabytes
-empty 查找空文件
(2)用法
[ tmp]# ll
total 8
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
-rwxrw-r--. 1 root root 0 Mar 5 10:32 root
-rw-rw-r--. 1 1001 1001 0 Mar 18 20:10 stu
-rw-r--r--. 1 root root 65 Mar 12 11:04 test
[ tmp]# find . -size 2003c
#查找大小为 2003bytes 的文件
./passwd
[ tmp]# find . -size -100c
#查找小于 100bytes 的文件
./root
./test
./calf
./stu
[ tmp]# find . -size +100c
#查找大于 100bytes 的文件
.
./passwd
[ tmp]# find . -empty #查找空文件
./root
./calf
./stu
#注:以上答案均已去除隐藏文件。
4.按文件类型查找
(1)按文件类型查找
-type TYPE:搜寻档案的类型为 TYPE 的,类型主要有:一般正规档案
(f), 装置档案 (b, c),目录 (d), 连结档 (l), socket (s), 及 FIFO (p) 等属性。
(2)用法
[ tmp]# ll
total 8
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
drwxr-xr-x. 2 root root 6 Mar 18 20:27 dir
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
[ tmp]# find . -type f
./passwd
./calf
[ tmp]# find . -type d
.
./dir
5.按权限查找
(1)按文件权限查找
? -perm mode :搜寻档案权限『刚好等于』 mode 的档案!
? -perm -mode :搜寻档案权限『必须要全部囊括 mode 的权限』的档
案,举例来说,我们要搜寻 -rwxr--r-- ,亦即 0744 的档案,使用 -
perm -0744,当一个档案的权限为 -rwsr-xr-x ,亦即 4755 时,也会
被列出来,因为 -rwsr-xr-x 的属性已经囊括了 -rwxr--r-- 的属性了。
? -perm /mode :搜寻档案权限『包含任一 mode 的权限』的档案,举
例来说,我们搜寻-rwxr-xr-x ,亦即 -perm /755 时,但一个文件属性
为 -rw-------也会被列出来,因为他有 -rw.... 的属性存在!
(2)用法
[ tmp]# ll
total 8
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
-rwxrw-r--. 1 root root 0 Mar 5 10:32 root
-rw-rw----. 1 1001 1001 0 Mar 18 20:10 stu
-rw-r--r--. 1 root root 65 Mar 12 11:04 test
[ tmp]# find . -perm 664
#权限为 664 即可
./calf
./stu
[ tmp]# find . -perm -664
#权限大于 664 即可
.
./root
./calf
./stu
[ tmp]# find . -perm -020
#同组用户可写即可
./root
./calf
./stu
[ tmp]# find . -perm -030
#要求同组用户可写可执行
[ tmp]# find . -perm /040
#同组用户可读即可
./root
./test
./passwd
./calf
./stu
[ tmp]# find . -perm /030
#同组用户可写或者可执行即可
./root
./calf
./stu
[ tmp]# find . -perm /007
#其他用户可读或者可写或者可执行即可
./root
./test
./passwd
./calf
6.按文件名查找
(1)按文件名查找
-name filename:搜寻文件名为 filename 的档案;
(2)用法
[ tmp]# ls
calf passwd root stu test
[ tmp]# find . -name calf #查找名为 calf 的文件
./calf
[ tmp]# find . -name c #查找以 c 开头的文件
./calf
[ tmp]# find . -name "????"
#查找名字为 4 个字符的文件
./root
./test
./calf
[ tmp]# find . -name "
" #查找所有文件
.
./root
./test
./passwd
./calf
./stu
[ tmp]# find . -name "oo"
./root
#查找名字中含有 oo 的文件
7.对查找出来的文件做二次处理
(1)二次处理
? -exec command :command 为其他指令,-exec 后面可再接额外的
指令来处理搜寻到的结果。
? -print :将结果打印到屏幕上,这个动作是预设动作!
(2)用法
[ tmp]# ll
total 8
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
-rwxrw-r--. 1 root root 0 Mar 5 10:32 root
-rw-rw----. 1 1001 1001 0 Mar 18 20:10 stu
-rw-r--r--. 1 root root 65 Mar 12 11:04 test
[ tmp]# find . -name calf -exec ls -l {} \;
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 ./calf
#查找 calf,然后列出详细信息
[ tmp]# find . -nouser -exec rm -f {} \;
#查找孤儿文件,然后删除
[ tmp]# ls
calf passwd root test
3.5 权限与指令间的关系
1.让用户能进入某目录成为『可工作目录』的基本权限为何:
? 可使用的指令:例如 cd 等变换工作目录的指令;
? 目录所需权限:用户对这个目录至少需要具有 x 的权限
? 额外需求:如果用户想要在这个目录内利用 ls 查阅文件名,则用户对此目录还需
要 r 的权限。
2.用户在某个目录内读取一个档案的基本权限为何?
? 可使用的指令:例如本章谈到的 cat, more, less 等等
? 目录所需权限:用户对这个目录至少需要具有 x 权限;
? 档案所需权限:使用者对档案至少需要具有 r 的权限才行!
3.让使用者可以修改一个档案的基本权限为何?
? 可使用的指令:例如 nano 或未来要介绍的 vi 编辑器等;
? 目录所需权限:用户在该档案所在的目录至少要有 x 权限;
? 档案所需权限:使用者对该档案至少要有 r, w 权限
4.让一个使用者可以建立一个档案的基本权限为何?
? 目录所需权限:用户在该目录要具有 w,x 的权限,重点在 w 啦!
5.让用户进入某目录并执行该目录下的某个指令之基本权限为何?
? 目录所需权限:用户在该目录至少要有 x 的权限;
? 档案所需权限:使用者在该档案至少需要有 x 的权限

相关推荐