Linux file Lookup

Source: Internet
Author: User
Tags syslog

General statement: files on the server such as the vast starry sky, it is difficult to find a designated star. Fortunately, Linux provides us with two search commands. Search: Locate, find

First, locate:

The Locate command query file requires a database that relies on the system itself, and the database is routinely executed once a day. When we have this database, we can quickly search for files.

The locate relies on four components: the 1./usr/bin/updatedb #更新数据库文件, automatically runs 2./usr/bin/locate #查询功能3 every day through crontab./etc/updatedb.conf #配置查询的条件4. Var/lib/mlocate/mlocate.db #存放文件信息的数据库

How to use: [[email protected] ~]# Locate passwd/etc/passwd/etc/passwd-/etc/pam.d/passwd/etc/security/opasswd/lib64/ Security/pam_passwdqc.so/lib64/security/pam_unix_passwd.so .....

Locate queries are fast, but the lookup criteria are too singular and not real-time. For example, to find the system modified files within 10 minutes, locate can do nothing.

Second, find:

Find searches for a file in the directory structure specified by the condition and performs the specified action. Many search conditions can be customized, the function is very powerful.

Command format: Find [options] [Find path] [find condition] [processing action]

1. Default settings:

Find path: Default current directory;

Search criteria: The default is to find all the files under the path;

Processing action: The default is display, which is echo.

2. Search criteria:

-name "file name" , support the use of globbing;

Ps:file globbing: An asterisk (*) is used to match any character in a file name; Matches a single character in the file, and the square brackets ([several characters]) match any one by one characters in the parentheses. Note: File globbing is not a strict regular expression.

[Email protected] ~]# Find/etc/passwd-name passwd/etc/passwd

-iname "file name", looking for case-insensitive characters;

[[email protected] ~]# Touch Abcd[[email protected] ~]# touch Abcd[[email protected] ~]# LSABCD ABCD Anaconda-ks.cfg in Stall.log Install.log.syslog[[email protected] ~]# find. -name Abcd./abcd[[email protected] ~]# find. -iname ABCD./ABCD./ABCD

-user UserName: Based on owner lookup

-group GroupName: Search by genus Group

-uid UID Lookup based on user ID

-gid GID lookup based on group ID

-nouser: Find a file without a master (PS: If the current user Tom has created a file A, Tom user is now deleted.) File A is not in the main group)

-nogroup: Finding files that are not owned by a group

[Email protected] ~]# useradd tom[[email protected] ~]# passwd tom changing password for user Tom. New Password:bad Password:it is the too Shortbad password:is too simpleretype new Password:passwd:all authentication Tokens updated successfully. [[email protected] ~]# Touch A[[email protected] ~]# chown tom.tom a[[email protected] ~]# Ls-la a-rw-r--r--. 1 tom Tom 0 Dec 01:54 a[[email protected] ~]# userdel-r tom[[email protected] ~]# Ls-la a-rw-r--r--. 1 0 Dec 01:54 a[[email protected] ~]# Find-nouser./a[[email protected] ~]#

3. Combination conditions:

-A: With, while satisfying-o: or-not,!: Non-inverting

[[email protected] ~]# Touch 1 ab[[email protected] ~]# LS1 a ab abcd abcd anaconda-ks.cfg install.log Insta Ll.log.syslog[[email protected] ~]# find-name a-o-name 1./A./1

4. Search by file type or size:

1)-type: Search by File type:

F: Normal file

D: Catalogue

B: Block file

C: Character file

L: Symbolic Link device

P: Named pipe device

S: Socket

[Email protected] ~]# LS1 a AB ABC abcd ABCD anaconda-ks.cfg install.log install.log.syslog test[[email Prot Ected] ~]# find-type d.. /abc./test

2)-size: Search by File size

-size [+|-] #Unit

Eg.-size +2m find files larger than 2M, common units: k,m,g

5. Search by Time stamp:

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "border=" 0 "alt=" image "src=" http ://s3.51cto.com/wyfs02/m00/78/bc/wkiol1acx-7hlue1aaapqhdruhs237.png "height="/>

Find can be looked up based on the timestamp of the file:-amin N finds the last n minutes of access to the file in the system-atime n Find the last n hours of access to the file in the system-cmin N find the file in the last n minutes of the system changed file status-ctime N Find the last n days of the system changed the text Document Status file-mmin n Find the last n minutes of the system the file data is changed file-mtime n Find the last n days in the system file data changed file #min after the time is minutes, time followed by the number of days

6. Search by file permissions:

-perm [+|-] mode mode: Exact match +mode: any one of the user's permissions matches, often used to find out whether a particular permission for a class exists-mode: The specified permission bits for each type of user to be checked match
[[Email protected] ~]# ls -ltotal 52-rw-r--r--.  1 root root      0 dec 30 02:01 1-rw-r--r--.  1 root root      0 Dec 30 02:01 22-rwxrwxrwx. 1  500  500      0 dec 30 01:54 a-rw-r--r--.  1 root root      0 dec 30 02:01 abdrwxr-xr-x. 2 root root  4096  dec 30 02:04 abc-rw-r--r--.  1 root root     0  dec 30 01:48 abcd-rw-r--r--.  1 root root     0  DEC 30 01:48 ABCD-RW-------.  1 root root  1417 dec 29  18:56 anaconda-ks.cfg-rw-r--r--.  1 root root 27312 dec 29 18:55  install.log-rw-r--r--. 1 root root  7764 dec 29 18:54 install.log.syslogdrwxr-xr-x.  2 root root  4096 Dec 30 02:04 test[[email protected]  ~]# find -perm 777./a

7. Handling Actions:

Files found, we often have to do such as delete, copy, modify and so on operation

-print: Print on standard output-ls: Output each file information in a long format-exec command {} \;: Executes the specified command on the found file,-ok command {} \;: Interactive-exec find finds the A file is passed to-exec for one time to execute find | Xargs COMMAND
[[email protected] ~]# find /etc -name passwd - print -exec cat  {} \;/etc/pam.d/passwd#%pam-1.0auth        includesystem-authaccount    includesystem-authpassword    substacksystem-auth-password   optionalpam_gnome_keyring.so/etc/passwdroot:x:0:0:root:/root:/ bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/ Nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/sync 
[Email protected] ~]# Find/etc-name passwd-print-ls/etc/pam.d/passwd785290 4-rw-r--r--1 root root 146 2012/etc/pam.d/passwd/etc/passwd786527 4-rw-r--r--1 root root 1338 Dec 01:55/etc/pass Wd

8.find has a lot of commands, which are often used

-name,-iname,-user,-group,-uid,-gid,-nouser,-nogroup,-size,-type,-atime,-perm,-exec,-ok,-ls
| Xargs COMMAND. If you are not looking for it, you can find it man.

Linux file Lookup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.