Home > php教程 > PHP开发 > body text

Detailed explanation of Linux search command and find command

高洛峰
Release: 2016-12-14 17:13:24
Original
1503 people have browsed it

1. The locate command for file search

locate:
Non-real-time, the search is based on the whole system file database, fuzzy search,
update manually generates the file database
Fast

Depends on the updatedb database

#手动更新locate数据库
[root@lovelace scripts]# updatedb
#使用locate查找文件 (速度好快的说)
[root@lovelace scripts]# locate *.py
/home/scripts/factorial.py
/home/scripts/input.py
/usr/lib/python2.4/fileinput.py
/usr/lib/python2.4/fileinput.pyc
/usr/lib/python2.4/fileinput.pyo
/usr/lib/python2.4/site-packages/dogtail/rawinput.py
/usr/lib/python2.4/site-packages/dogtail/rawinput.pyc
/usr/lib/python2.4/site-packages/dogtail/rawinput.pyo
/usr/lib/python2.4/test/pyclbr_input.py
/usr/lib/python2.4/test/pyclbr_input.pyc
/usr/lib/python2.4/test/pyclbr_input.pyo
/usr/lib/python2.4/test/test_fileinput.py
/usr/lib/python2.4/test/test_fileinput.pyc
/usr/lib/python2.4/test/test_fileinput.pyo
Copy after login

2. Find command for file search

find:
Real-time
Accurate
Supports many search rules
Traverses all files in the specified directory to complete the search, slow speed

find Search path Search criteria Processing operations after finding
Search path: Default is Current directory
Search criteria: Default is all files under the specified path
Processing operations after finding: Default is display

Matching criteria:
-name 'filename': Exactly match the file name
File name wildcard:
* Any character of any length
? Any single character
[] Characters in the option
-iname 'filename': File name matching is not case-sensitive
-regex pattern Matches file names based on regular expressions

-user username : Search based on owner
- group groupname: Search according to the group

-gid gid: Search according to gid
-uid uid: Search according to udi

-nouser Find files without owners
-nogroup Files without groups

example: find /tmp -nouser

[root@lovelace scripts]# find /tmp -name test
/tmp/sources/httpd-2.2.17/srclib/apr-util/test
/tmp/sources/httpd-2.2.17/srclib/apr/test
/tmp/sources/httpd-2.2.17/modules/test
/tmp/sources/httpd-2.2.17/test
/tmp/test
Copy after login

Find by file type

-type
f: normal file
d: directory
c: character
b: block device
l: link
p: pipe
s: socket

example :find /etc -type d

#查找/tmp目录下名字为test 而且文件格式为目录的
[root@lovelace scripts]# find /tmp -type d -a -name test
/tmp/sources/httpd-2.2.17/srclib/apr-util/test
/tmp/sources/httpd-2.2.17/srclib/apr/test
/tmp/sources/httpd-2.2.17/modules/test
/tmp/sources/httpd-2.2.17/test
/tmp/test
Copy after login

Find according to the file size

-size
[+|-]#k Without + and -, it means an exact match
[+|-]#m
[+|-]# G

example:find /tmp -size +10M Find files larger than 10M in the /etc directory

Combination conditions: This requires understanding Morgan’s Law
-a: and Default
-o: or
-not: non
example :find /tmp -not -user user1 -o -not -type d

#查找/tmp目录下文件格式为目录,而且大小在26k到32k之间的目录
[root@lovelace scripts]# find /tmp -type d -a -size +16k -a -size -32k
/tmp/sources/httpd-2.2.17/docs/manual/mod
/tmp/sources/php-5.2.13/ext/reflection/tests
/tmp/sources/php-5.2.13/ext/date/tests
/tmp/sources/php-5.2.13/ext/spl/tests
/tmp/sources/php-5.2.13/tests/classes
/tmp/sources/php-5.2.13/Zend/tests
Copy after login

Find based on file timestamp:
In days:

Change time: -mtime
Modification time: -ctime
View time: -atime
[+|-]# The default timestamp is 5 days
-5: Visited within 5 days
+5: Not visited for at least 5 days
5: Visited exactly 5 days ago

In minutes of:
-mmin:
-cmin:
-amin:

find /tmp -amin –5 Files that have been accessed within 5 minutes

If they have not been accessed for at least a long time, and the file size exceeds a certain amount, delete them Operation
find /tmp -atime +30 -a –size +100M -exec 'rm -rf *' ;

#查找home目录下至少5天没被访问过的文件
[root@lovelace scripts]# find /home/ -ctime -5
/home/scripts
/home/scripts/list
/home/scripts/for
/home/scripts/for/dir.sh
/home/scripts/for/three.sh
/home/scripts/for/checkbash.sh
/home/scripts/for/sorsum.sh
/home/scripts/while
/home/scripts/while/readpasswd.sh
/home/scripts/while/catwhile.sh
/home/scripts/case
/home/scripts/case/showmenu.sh
/home/scripts/case/showmenu
/home/scripts/case/2showmenu.sh
/home/scripts/if
/home/scripts/if/grade.sh
/home/scripts/51cto
/home/scripts/51cto/info.tt
/home/scripts/51cto/info.sh
/home/scripts/51cto/1.sh
/home/scripts/51cto/argument.sh
/home/scripts/51cto/sum.sh
Copy after login

Search based on permissions:
-perm mode: Exact match
-perm -mode: Every permission is required Exact matching: File permissions that completely include this mode are all in compliance with the standard
-perm /mode: Any one of the 9 permissions meets the conditions

example: find /tmp -perm –001 Find files that other users have write permissions for


find action:
-print Default
-ls: Displays every information of the file in a form similar to ls -l

-ok command {} ; Will perform a query operation every time, requiring user confirmation
-exec command {} ; It will not wake up and ask for the operation
Quote the original name, use {}

example: find /tmp -perm -020 -exec mv {} {}.new ;

#找出home中大小为16k到32k之间的文件,然后传递给exec 并显示出来
[root@lovelace scripts]# find /home/ -size +16k -a -size -32k -exec ls -lh {} \;
-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/gconf.xml.defaults/%gconf-tree-li.xml
-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/gconf.xml.defaults/%gconf-tree-ug.xml
-rw-r--r-- 1 root root 25K 05-03 03:04 /home/nick/etc/gconf/schemas/drivemount.schemas
-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/schemas/gnome-volume-control.schemas
-rw-r--r-- 1 root root 21K 05-03 03:04 /home/nick/etc/gconf/schemas/system_smb.schemas
-rw-r--r-- 1 root root 22K 05-03 03:04 /home/nick/etc/gconf/schemas/desktop_gnome_thumbnailers.schemas
-rw-r--r-- 1 root root 17K 05-03 03:04 /home/nick/etc/gconf/schemas/apps_gnome_settings_daemon_default_editor.schemas
Copy after login

find and xargs commands

xargs : The function is to convert the parameter list into small pieces and pass them to other commands in segments to avoid the problem of too long parameter lists.

Compared with -exec command, this command has more powerful functions. When used with find, it is usually passed to xargs through a pipeline

find /tmp –size +100M | Displays the alias of the command

#使用which命令查找ls命令的完整路径
[root@lovelace scripts]# which ls
alias ls='ls --color=tty'
/bin/ls
#使用ldd命令查看ls所依赖的库文件(这里需要用到ls的完整路径)
[root@lovelace scripts]# ldd /bin/ls
linux-gate.so.1 =>  (0x00cf8000)
librt.so.1 => /lib/librt.so.1 (0x00d8d000)
libacl.so.1 => /lib/libacl.so.1 (0x00d62000)
libselinux.so.1 => /lib/libselinux.so.1 (0x00de0000)
libc.so.6 => /lib/libc.so.6 (0x00110000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00d71000)
/lib/ld-linux.so.2 (0x00baf000)
libattr.so.1 => /lib/libattr.so.1 (0x003a9000)
libdl.so.2 => /lib/libdl.so.2 (0x00d55000)
libsepol.so.1 => /lib/libsepol.so.1 (0x00d98000)
Copy after login

whereis: Similar to which, and will additionally give the full path to the man page that gives the command

#使用whereis查看命令的完整路径和相应的man文件
[root@lovelace scripts]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
#man 查看对应的文件
[root@lovelace scripts]# man 1 ls
#man 查看对应的文件
[root@lovelace scripts]#
[root@lovelace scripts]# man 1p ls
Copy after login

whatis: Will query the file in the whatis database when you want to confirm the system command When working with important configuration files, this command is very important and can be used as a simple man command.

[root@lovelace scripts]# whatis ls
ls                   (1)  - list directory contents
ls                   (1p)  - list directory contents
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template