Searching is resource-intensive and is not recommended.
The server planning should be done well, and the storage locations of various data software should be planned well, so that it is easy to find without searching.
1.7.1.find
find [ search scope ] [ match criteria ]
file search
1.7.1.1. Filename Exact Search
-name Search by file name, exact search, only the file names are equal to be searched
[root@localhost cn]# find /etc -name init
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
[root@localhost cn]#
1.7.1.2. Asterisk wildcard
The above precise search, many names are not easy to remember, so a fuzzy search is required
Wildcards: asterisk * matches any number of any characters
Find all files starting with init
[root@localhost cn]# find /etc -name init*
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/sysconfig/init
/etc/init.d
/etc/rc.d/init.d
/etc/inittab
/etc/selinux/targeted/active/modules/100/init
/etc/selinux/targeted/contexts/initrc_context
[root@localhost cn]#
1.7.1.3. Question mark wildcard
Wildcards? , which matches a single arbitrary character
starts with init and is followed by 3 characters
[root@localhost cn]# find /etc -name init???
/etc/inittab
[root@localhost cn]#
1.7.1.4. Case insensitive
-iname
[root@localhost cn]# find /etc -iname init???
/etc/inittab
[root@localhost cn]#
1.7.1.5. File size lookup
-size Find by file size
The unit of size is block
How much is 100MB?
1 data block = 512 bytes = 0.5KB
100MB=102400KB=204800 blocks
1KB=2 blocks
+n is greater than -n is less than =n is equal to
Find files larger than 100MB
[root@localhost cn]# find / -size +204800
/proc/kcore
find: ‘/proc/1503/task/1503/fd/6’: No such file or directory
find: ‘/proc/1503/task/1503/fdinfo/6’: No such file or directory
find: ‘/proc/1503/fd/6’: No such file or directory
find: ‘/proc/1503/fdinfo/6’: No such file or directory
/usr/lib/locale/locale-archive
1.7.1.6. Find by owner
-user Find by owner
[root@localhost cn]# find ./ -user root
./
./yum.log
./test
./test/log.log
./test1
./test2.log
./test3
[root@localhost cn]#
1.7.1.7. Time attribute lookup
-amin access time access
-cmin file attribute change
-mmin file content modify
find /etc –cmin 5
Files and directories whose attributes have been modified within 5 minutes
1.7.1.8. Compound conditions and or
-a means and both conditions are met
Files larger than 50MB and smaller than 100MB
find /etc –size +102400 –a –size 204800
-o means that or two conditions meet either one
1.7.1.9. File Types
-type Find by file type
f file d directory l soft link file
[root@localhost cn]# find ./ -type d
./
./test
./test1
./test3
[root@localhost cn]# find ./ -type f
./yum.log
./test/log.log
./test2.log
[root@localhost cn]#
1.7.1.10. Execute command reprocessing on search results
-exec command {} \;
{} represents the result of the previous search
\ escape
; means end
It seems that the search results are incorrect?
[root@localhost cn]# find /tmp/cn -iname 'test*' -exec ls -l {} \;
total 0
-rw-r--r--. 1 root root 0 Apr 24 20:44 log.log
total 0
-rw-r--r--. 1 root root 0 Apr 24 21:10 /tmp/cn/test2.log
total 0
[root@localhost cn]#
-ok command {} \;
This will confirm the prompt
It seems that the search results are incorrect?
[root@localhost cn]# find /tmp/cn -iname 'test*' -ok ls -l {} \;
< ls ... /tmp/cn/test > ? y
total 0
-rw-r--r--. 1 root root 0 Apr 24 20:44 log.log
< ls ... /tmp/cn/test1 > ? y
total 0
< ls ... /tmp/cn/test2.log > ? y
-rw-r--r--. 1 root root 0 Apr 24 21:10 /tmp/cn/test2.log
< ls ... /tmp/cn/test3 > ? y
total 0
[root@localhost cn]#
When searching for multiple files, the parameters need to be enclosed in single quotes:
[root@localhost cn]# find /tmp/cn -iname test*
find: paths must precede expression: test1
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@localhost cn]# find /tmp/cn -iname 'test*'
/tmp/cn/test
/tmp/cn/test1
/tmp/cn/test2.log
/tmp/cn/test3
[root@localhost cn]#
1.7.1.11.i node lookup
-inum lookup based on i - node
[root@localhost cn]# ls -i
5598 test 16777673 test1 201 test2.log 33575346 test3 5597 yum.log
[root@localhost cn]# ls -il
total 4
5598 drwxrwxrwx. 2 root root 21 Apr 24 20:44 test
16777673 drwxr-xr-x. 2 root root 6 Apr 24 21:10 test1
201 -rw-r--r--. 1 root root 0 Apr 24 21:10 test2.log
33575346 drwxr-xr--. 2 root root 6 Apr 24 21:17 test3
5597 -rw-rwx--x. 1 root root 27 Apr 23 21:58 yum.log
[root@localhost cn]# find . -inum 201
./test2.log
[root@localhost cn]# find . -inum 201 -exec ls -l {} \;
-rw-r--r--. 1 root root 0 Apr 24 21:10 ./test2.log
[root@localhost cn]#
1.8. Other file search commands
1.8.1.locate
install mlocate
The locate command is in the installation of mlocate , so you need to install mlocate
[root@localhost ~]# locate init
-bash: locate: command not found
[root@localhost ~]# yum list mlocate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
mlocate.x86_64 0.26-6.el7 base
[root@localhost ~]# yun install mlocate
-bash: yun: command not found
[root@localhost ~]# yum install mlocate
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Omitted ………………………………………………………….
Complete!
[root@localhost ~]# locate init
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
After the installation is complete, the command still cannot search the content, because the database needs to be updated:
[root@localhost ~]# updatedb
[root@localhost ~]# locate init
/boot/initramfs-0-rescue-ee8e847af1004327a6a44a0a5a957248.img
/boot/initramfs-3.10.0-693.el7.x86_64.img
/boot/initrd-plymouth.img
/dev/initctl
locate search speed is very fast, he is looking for files in the file database, so the speed is very fast.
Syntax: locate filename
Locate the database maintained by itself: /var/lib/mlocate/mlocate.db
All files in the system are regularly updated to this file repository.
The existing problem is that the time when the newly created file has not been included in the database cannot be searched.
The command to update the file repository:
updatedb
-i is size insensitive
locate –i init
[root@localhost opt]# locate china
/opt/china.log
[root@localhost opt]# locate -i china
/opt/China.log
/opt/china.log
[root@localhost opt]#
1.8.2.which
The path where the command is located: /usr/bin/which
Search command directory and alias information
[root@localhost opt]# which ls
alias ls = 'ls --color = auto'
/usr/bin/ls
[root@localhost opt]# which locate
/usr/bin/locate
[root@localhost opt]#
Find command aliases
rm is an alias for rm -i
When we delete files, there is usually a prompt because -i works.
-i prompt before every removal
[root@localhost opt]# which rm
alias rm = 'rm -i'
/usr/bin/rm
1.8.3.whereis
Find the directory where the command is located and the help file path, similar to which
The red part is the document path
[root@localhost opt]# whereis rm
rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz
1.8.4.grep
Search the file content for a line matching the string and output
-i is case insensitive
-v exclude the specified string
Syntax: grep –iv [ specified string ] [ file ]
[root@localhost opt]# grep to /etc/inittab
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
not case sensitive
[root@localhost opt]# grep -i to /etc/inittab
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
# To view current default target, run:
# To set a default target, run:
[root@localhost opt]#
Find the configuration information in the file, remove the comments, the lines starting with # are all comments, and remove them all
[root@localhost etc]# grep -v ^# yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache = 0
debuglevel=2
logfile=/var/log/yum.log
exactfile=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
[root@localhost etc]#