File search commands (which, whereis, locate, find)

which command

Search for the location of a system command in the path specified by the PATH variable, and return the first search result. In other words,Using the which command, you can see whether a certain system command exists and where the command is being executed.

用法:
which  可执行文件名称 
实例:
[root@localhost ~]# which pwd
/bin/pwd

whereis command

You can search for binary files, source files and help files of the specified command.

[root@localhost ~]# whereis whereis
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz
需要注意的是,输出的第一个路径才是你想要的结果。
使用 whereis 命令,同时也会显示帮助页面和源码所在路径。
(如果能找到的情况下会显示,但是在这一例中没有找到)
所以你在输出中看见的第二个路径就是帮助页面文件所在位置。
-b Only find executable files (binary files)
-m Only find help files
-s Source code file
实例:
[root@localhost ~]# whereis -b whereis
whereis: /usr/bin/whereis
[root@localhost ~]# whereis -s whereis
whereis:
[root@localhost ~]# whereis -m whereis
whereis: /usr/share/man/man1/whereis.1.gz

locate command

First create a database that includes all file names and paths in the system, and then only need to query this database when searching.(In general distribution, the establishment of the database is automatically executed in crontab)
Execute updatedb before using locate
Working characteristics:

  • Find fast
  • Fuzzy search
  • Non-real-time search
    The command needs to be installed: yum install mlocate -y
-b Match only base names in the path
-c Count the total number of eligible files
实例:
[root@localhost ~]# updatedb
[root@localhost ~]# locate -c /etc/sh
3
[root@localhost ~]# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells

find command

The find command is a real-time search tool,Complete the file search by traversing the specified path; When using this command,If the parameter is not selected, search for subdirectories and files in the current directory and display them; In addition,Any string before the parameter will be regarded as the name of the directory to be searched.
Working characteristics:

  • Accurate real-time
  • Slow
  • It may only search directories where the user has read and execute permissions.
用法:
find [OPTION]... [查找路径] [查找条件] [处理动作]
查找的起始路径: 指定具体的搜索目录,默认为当前目录
查找条件: 指定查找的标准,大小,文件名,类型。默认是指定路径下的所有文件
处理动作: 符合查找条件的文件做出的操作,例如cp,rm,默认为输出到标准输出

1). Search conditions:

.Search by logical conditions

-a versus
-O or
-not non-
non-

.Search by file name

1.-name "pattern" quotes need to be added
2. Support wildcard *? [] [^]
3.-iname “pattern” is not case sensitive
4.-regex “pattern” matches the entire path instead of the file name

实例:
[root@localhost var]# find -name  "fil*"
./lib/yum/rpmdb-indexes/file-requires
[root@localhost var]# find -iname  "Fil*"
./lib/yum/rpmdb-indexes/file-requires
./lib/rpm/Filedigests
[root@localhost var]# find /  -regex  /t.*/f.*
/tmp/file

.Search based on the file's affiliation

-user Owner
-group Belong to group
-uid GID Owner ID
-gid GID Group ID
-nouser File without owner
-nogroup No files belonging to the group
实例:
查找/var目录下属主为root,且属组为mail的所有文件
[root@localhost ~]#  find /var  -user root -group mail
/var/spool/mail
查找/var目录下不属于root
[root@localhost ~]#  find /var ! -user root
/var/lib/dav
/var/lib/postfix
/var/lib/postfix/master.lock
/var/cache/mod_proxy
/var/spool/mail/fedore
/var/spool/mail/gentooo
/var/spool/mail/gentoo
/var/spool/postfix/active
/var/spool/postfix/defer
/var/spool/postfix/hold
/var/spool/postfix/flush
/var/spool/postfix/saved
/var/spool/postfix/trace
/var/spool/postfix/public

.Search by file type

-type TYPE
TYPE:
f Normal file
d table of Contents
b Block device file
l Link file
c Character device file
p Pipeline file
s(socket) Socket file

.Find according to file size

         -size  [+|-] #UNIT
#UNIT:(#-1,#]
-#UNIT:[0,#-1]
+#UNIT:(#,OO)
实例: 
find / -size 10M 10M前面什么都不加,查找的就是文件大小为9M-10M的文件(不包括9M,包括10M)
find / -size -10M,查找的文件大小为0M-9M
find / -size +10M,查找的文件大小为>10M       

.Find by timestamp

以天/分钟为单位
-atime/amin   [+|-]# 文件最后访问时间
-mtime/mmin	 [+|-]# 文件最后修改时间
-ctime/cmin   [+|-]# 文件最后改变时间
#:[#,#-1): 10   大于等于10天前,小于9天前
-#:(#,0]    -10  最后的访问时间10天以内
+#:(oo,#-1] +10  最后的访问时间在9天以前,包括9天

.Find according to file permissions

-perm [-| /]mode

Authority bit: read (4) write (2) execute (1)

mode 9 permission bits, all permission bits are consistent
/mode As long as it contains, and one of the 9 permissions is satisfied, or
-mode As long as it contains, and each of the 9 permissions must meet and

Insert picture description here

 实例:
-rwxr-xr-x 1 root root 0 617 11:05 testl 
-r--r--r-- 1 root root 0 6月 17 11:05 test2
-rw------- 1 root root 0 6月 17 11:05 test3
-w------- 1 root root 0 6月 17 11:05 test4
[root@localhost test]#find.-perm 444(什么都不加代表要权限位一样才行)
./test2
[root@localhost test]#find.-perm 200
./test4
[root@localhost test]#find .-perm -200
./test4 <-此文件权限为200
./test3 <-此文件权限为600
./testl <-此文件权限为755
#搜索文件的权限包含200的文件,不会找到test2文件,因为test2的权限为444,不包含200权限。
因为 test4 的权限 200(-w-------),test3 的权限 600(-rw-------)和test1 的权限 755(-rwxr-xr-x) 都包含 200(--w-------) 权限,所以可以找到;而 test2 的权限是 444 (-r--r--r--),不包含 200 (--w-------)权限。
-mode是九位权限都要包含(与),/mode只要九位权限有三位与之相符即可(或)

.Directory level

-path is based on directory hierarchy

.Deep search based on directory

-maxdepth maximum depth
-mindepth minimum depth

实例:
查找/tmp目录下第三层目录中的文件
[root@nebula tmp]# find /tmp/ -maxdepth 3   -mindepth 3 -name "file*"
/tmp/test/test/file_test
查找/tmp目录下第四层目录中的文件
[root@nebula tmp]# find /tmp/ -maxdepth 4   -mindepth 4 -name "file*"
/tmp/test/test/test/file_new

.Processing actions

-print Default action, output to standard output as
-fls /path/to/somefile Write the long format information of the found file to the specified file
-ok commond {} \ ; Perform commond operations on each file executed, and each operation requires user confirmation
-exec commond {}\ ; Do commond operations on each file executed
-xargs -0 commond {}\ ; Do commond operations on each file executed

Guess you like

Origin blog.csdn.net/qq_44944641/article/details/104728653