• Linux-查找-find-whereis-which


    3.4 文件查找

    3.4.1 命令查找

    1.which - shows the full path of (shell) commands

    (1)which说明

    which 命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录, which 命令会在环境变量$PATH 设置的目录里查找符合条件的文件。也就是说,使用which命令就可以看到某个系统指令是否存在,以及执行的命令位置。

    (2)which使用

    [root@localhost tmp]# which date   #列出找到的第一个结果

    /bin/date

    [root@localhost 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

    [root@localhost tmp]# whereis date

    date: /usr/bin/date /usr/share/man/man1/date.1.gz /usr/share/man/man1p/date.1p.gz

    [root@localhost tmp]# whereis -l     #显示whereis查找的位置

    [root@localhost tmp]# whereis -b date    #只显示二进制文件

    date: /usr/bin/date

    [root@localhost 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工作其实是由四部分组成的:

    l  /usr/bin/updatedb   主要用来更新数据库,通过crontab自动完成的

    l  /usr/bin/locate         查询文件位置

    l  /etc/updatedb.conf   updatedb的配置文件

    l  /var/lib/mlocate/mlocate.db  存放文件信息的文件

    (2)locate用法

    [root@localhost tmp]# locate date

    [root@localhost tmp]# locate -c date      #只显示结果数量

    1643

    [root@localhost 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

    [root@localhost 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的数据库

    [root@localhost tmp]# updatedb

    (4)缺陷

    [root@localhost ~]# touch aaaaaa      #新建文件

    [root@localhost ~]# locate aaaaaa      #找不到

    [root@localhost ~]# updatedb             #更新数据库

    [root@localhost ~]# 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)用法

    [root@localhost tmp]# touch xxxxx            #新创建一个文件

    [root@localhost tmp]# find /tmp/ -mtime -1  

    /tmp/

    /tmp/xxxxx

    #查找/tmp目录下1天内修改过的文件

    [root@localhost tmp]# ls

    passwd  root  test  xxxxx

    [root@localhost tmp]# find /tmp/ -mtime +1   

    /tmp/root                            #结果已去掉隐藏文件

    /tmp/test

    /tmp/passwd

    #查找/tmp目录下1天前修改过的文件

    [root@localhost tmp]# timedatectl set-time "2020-3-18 19:56:30" 

    #修改系统时间

    [root@localhost tmp]# date     #当前时间为“明天”

    Wed Mar 18 19:56:33 CST 2020

    [root@localhost tmp]# find . -mtime 1   

    .

    ./xxxxx

    #查找在当前目录下昨天修改过的文件

    [root@localhost tmp]# touch xxx

    [root@localhost tmp]# find . -newer xxxxx

    .

    ./xxx

    #查找在当前目录下比xxxxx要新的文件

    2.按用户查找

    (1)与使用者或组名有关的参数:

    l  -uid n :n 为数字,这个数字是用户的账号 ID,亦即 UID ,这个 UID 是记录在 /etc/passwd 里面与账号名称对应的数字。

    l  -gid n :n 为数字,这个数字是组名的 ID,亦即 GID,这个 GID 记录在 /etc/group

    l  -user name :name 为使用者账号名称喔!例如dmtsai

    l  -group name:name 为组名喔,例如 users ;

    l  -nouser:寻找档案的拥有者不存在 /etc/passwd 的人!

    l  -nogroup:寻找档案的拥有群组不存在于 /etc/group 的档案!

    (2)用法

    [root@localhost 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

    [root@localhost tmp]# find . -uid 1000   

    #在当前目录下查找uid为1000的文件

    ./calf

    [root@localhost tmp]# find . -gid 1000    

    #在当前目录下查找gid为1000的文件

    ./calf

    [root@localhost tmp]# find . -user calf     

    #在当前目录下查找用户名为calf的文件

    ./calf

    [root@localhost tmp]# find . -group calf    

    #在当前目录下查找组名为calf的文件

    ./calf

    [root@localhost tmp]# userdel stu      

    #删除用户和用户组stu

    [root@localhost tmp]# find . -nouser    

    #在当前目录下查找无主文件

    ./stu

    [root@localhost tmp]# find . -nogroup  

    #在当前目录下查找无组文件

    ./stu

    3.按文件大小查找

    (1) 按文件大小查找

    -size [+-]SIZE:搜寻比 SIZE 还要大(+)或小(-)的档案。

    `c'    for bytes

    `w'    for two-byte words

    `k'    for Kilobytes (units of 1024 bytes)

    `M'    for Megabytes

    `G'    for Gigabytes

    -empty  查找空文件

    (2)用法

    [root@localhost 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

    [root@localhost tmp]# find . -size 2003c   

    #查找大小为2003bytes的文件

    ./passwd

    [root@localhost tmp]# find . -size -100c    

    #查找小于100bytes的文件

    ./root

    ./test

    ./calf

    ./stu

    [root@localhost tmp]# find . -size +100c    

    #查找大于100bytes的文件

    .

    ./passwd

    [root@localhost tmp]# find . -empty      #查找空文件

    ./root

    ./calf

    ./stu

    #注:以上答案均已去除隐藏文件。

    4.按文件类型查找

    (1)按文件类型查找

     -type TYPE:搜寻档案的类型为 TYPE 的,类型主要有:一般正规档案 (f), 装置档案 (b, c),目录 (d), 连结档 (l), socket (s), 及 FIFO (p) 等属性。

    (2)用法

    [root@localhost 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

    [root@localhost tmp]# find . -type f

    ./passwd

    ./calf

    [root@localhost tmp]# find . -type d

    .

    ./dir

    5.按权限查找

    (1)按文件权限查找

    l  -perm mode  :搜寻档案权限『刚好等于』 mode 的档案!

    l  -perm -mode :搜寻档案权限『必须要全部囊括 mode 的权限』的档案,举例来说,我们要搜寻 -rwxr--r-- ,亦即 0744 的档案,使用 -perm -0744,当一个档案的权限为 -rwsr-xr-x ,亦即 4755 时,也会被列出来,因为 -rwsr-xr-x 的属性已经囊括了 -rwxr--r-- 的属性了。

    l  -perm /mode :搜寻档案权限『包含任一 mode 的权限』的档案,举例来说,我们搜寻-rwxr-xr-x ,亦即 -perm /755 时,但一个文件属性为 -rw-------也会被列出来,因为他有 -rw.... 的属性存在!

    (2)用法

    [root@localhost 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

    [root@localhost tmp]# find . -perm 664  

    #权限为664即可

    ./calf

    ./stu

    [root@localhost tmp]# find . -perm -664   

    #权限大于664即可

    .

    ./root

    ./calf

    ./stu

    [root@localhost tmp]# find . -perm -020    

    #同组用户可写即可

    ./root

    ./calf

    ./stu

    [root@localhost tmp]# find . -perm -030   

    #要求同组用户可写可执行

    [root@localhost tmp]# find . -perm /040  

    #同组用户可读即可

    ./root

    ./test

    ./passwd

    ./calf

    ./stu

    [root@localhost tmp]# find . -perm /030  

    #同组用户可写或者可执行即可

    ./root

    ./calf

    ./stu

    [root@localhost tmp]# find . -perm /007    

    #其他用户可读或者可写或者可执行即可

    ./root

    ./test

    ./passwd

    ./calf

    6.按文件名查找

    (1)按文件名查找

    -name filename:搜寻文件名为 filename 的档案;

    (2)用法

    [root@localhost tmp]# ls

    calf  passwd  root  stu  test

    [root@localhost tmp]# find . -name calf    #查找名为calf的文件

    ./calf

    [root@localhost tmp]# find . -name c*     #查找以c开头的文件

    ./calf

    [root@localhost tmp]# find . -name "????"    

    #查找名字为4个字符的文件

    ./root

    ./test

    ./calf

    [root@localhost tmp]# find . -name "*"     #查找所有文件

    .

    ./root

    ./test

    ./passwd

    ./calf

    ./stu

    [root@localhost tmp]# find . -name "*oo*"   

    ./root

    #查找名字中含有oo的文件

    7.对查找出来的文件做二次处理

    (1)二次处理

    l  -exec command :command 为其他指令,-exec 后面可再接额外的指令来处理搜寻到的结果。

    l  -print        :将结果打印到屏幕上,这个动作是预设动作!

    (2)用法

    [root@localhost 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

    [root@localhost tmp]# find . -name calf -exec ls -l {} ; 

    -rw-rw-r--. 1 calf calf 0 Mar 18 20:06 ./calf

     #查找calf,然后列出详细信息

    [root@localhost tmp]# find . -nouser -exec rm -f {} ;

    #查找孤儿文件,然后删除

    [root@localhost tmp]# ls

    calf  passwd  root  test

    3.5 权限与指令间的关系

    1.让用户能进入某目录成为『可工作目录』的基本权限为何:

    n  可使用的指令:例如 cd 等变换工作目录的指令;

    n  目录所需权限:用户对这个目录至少需要具有 x 的权限

    n  额外需求:如果用户想要在这个目录内利用 ls 查阅文件名,则用户对此目录还需要 r 的权限。

    2.用户在某个目录内读取一个档案的基本权限为何?

    n  可使用的指令:例如本章谈到的 cat, more, less等等

    n  目录所需权限:用户对这个目录至少需要具有 x 权限;

    n  档案所需权限:使用者对档案至少需要具有 r 的权限才行!

    3.让使用者可以修改一个档案的基本权限为何?

    n  可使用的指令:例如 nano 或未来要介绍的 vi 编辑器等;

    n  目录所需权限:用户在该档案所在的目录至少要有 x 权限;

    n  档案所需权限:使用者对该档案至少要有 r, w 权限

    4.让一个使用者可以建立一个档案的基本权限为何?

    n  目录所需权限:用户在该目录要具有w,x的权限,重点在 w 啦!

    5.让用户进入某目录并执行该目录下的某个指令之基本权限为何?

    n  目录所需权限:用户在该目录至少要有 x 的权限;

    n  档案所需权限:使用者在该档案至少需要有 x 的权限

    3.6 总结

    l  绝对路径:『一定由根目录 / 写起』;相对路径:『不由 / 写起,而是由相对当前目录写起』

    l  特殊目录有:., .., -, ~, ~account需要注意;

    l  与目录相关的指令有:cd, mkdir, rmdir, pwd 等重要指令;

    l  用户能使用的指令是依据 PATH 变量所规定的目录去搜寻的;

    l  ls 可以检视档案的属性,尤其 -d, -a, -l 等选项特别重要!

    l  档案的复制、删除、移动可以分别使用:cp, rm , mv等指令来操作;

    l  检查档案的内容(读文件)可使用的指令包括有:cat, tac, nl, more, less, head, tail, od 等

    l  观察档案的类型可以使用 file 指令来观察;

    l  touch 的目的在修改档案的时间参数,但亦可用来建立空档案;

    l  一个档案记录的时间参数有三种,分别是 access time(atime), status time (ctime), modification time(mtime),ls 默认显示的是 mtime。

    l  chattr与lsattr设定及观察隐藏属性。

    l  档案具有SUID的特殊权限时,代表当用户执行此一binary程序时,在执行过程中用户会暂时具有程序拥有者的权限。

    l  目录具有SGID的特殊权限时,代表用户在这个目录底下新建的档案之群组都会与该目录的组名相同。

    l  目录具有SBIT的特殊权限时,代表在该目录下用户建立的档案只有自己与root能够删除!

    l  利用 find 可以加入许多选项来直接查询文件系统,以获得自己想要知道的文档。

  • 相关阅读:
    SendMessage 关闭外部程序
    ShellApi 列举正在运行的程序
    SendMessage 关闭显示器
    ShellAPI 自定义系统的关于对话框 about
    if 条件语句
    操作INI文件cpp
    ShellAPI 取得可执行文件的图标
    For 循环 语句
    选择结构语句IF
    SendMessage 关闭计算器
  • 原文地址:https://www.cnblogs.com/aiaitie/p/12773328.html
Copyright © 2020-2023  润新知