Summary of use of file lookup commands

Source: Internet
Author: User

File lookup is widely used in Linux system operation and maintenance. This article expounds the use of file lookup in Linxu from the following aspects

1. Find commands and overview

In Linux, file lookups use commands that have locate and find two commands. Where the locate command is to find the system's file database. The speed is fast, but sometimes it may be inaccurate and needs to be used in conjunction with UpdateDB. The Find command is found one after the other based on the matching pattern. Slow, but more accurate.

2. The format used by the command

Locate FILENAME

[email protected] ~]# Touch upfile

[Email protected] ~]# UpdateDB

[Email protected] ~]# Locate Upfile

/opt/lampp/modules/mod_authz_groupfile.so

/root/upfile

# Note: Locate to be used in conjunction with UpdateDB

# Locate is not found in the file under/tmp


find [path] standard processing action

Path: can be omitted, if omitted, then find the current file

Standard: Can be omitted, if omitted, displays all files under the lookup path

Handling actions: Default is Display action

3. How to use and examples

-name: Indicates search by file name

[[email protected] testdir]# find. -name ABC

./abc

#注意:-name ABC is exactly the exact match of ABC. If you need to use fuzzy matching, you need to use regular expressions: The current directory-iname represents the root file name lookup, and the case of the file name is ignored


[[email protected] testdir]# find. -name abc\*

./abc123

./abc

# Find files that start with ABC in the current directory


-type: Search by file type

[[email protected] testdir]# find. -type D

.

./dir2

./02-04

./sh

# D: Indicates folder; l: Link file; C: Character device file; B: block device file; p: Pipeline file; s: socket file;-F for normal file


-size [+|-]#: Search by file size. Unit is K M G

[Email protected]:~# find/etc-size +1m

/etc/brltty/zh-tw.ctb

#注意, + represents greater than,-represents less than, no representation equals


-user USERNAME Search by user name

[[email protected] testdir]# find. -user user1

./abc

#同理,-group GROUPNAME Search by group name

-uid UID Lookup based on UID

-gid GID Search by GID


-nouser to find files that are not owned by the master

-nogroup to find the file with the genus Group

[Email protected] testdir]# Userdel-r user1

[[email protected] testdir]# find. -nouser

./abc

In the case of a document that is not a master or a group, it is a dangerous document and should be changed to root as soon as possible.

-amin-atime-cmin-ctime

-amin [+|-]# means the file is most recently accessed + #表示 # before,-#表示在 # minutes later

-atime similar to-amin but in days.


-perm [+|-] mode: Find files based on permissions

[[email protected] testdir]# man find

[[email protected] testdir]# find. -perm U-x

./abc

[[email protected] testdir]# find. -perm O-x

./abc

4. The processing action of find

[[email protected] testdir]# find. -nouser-exec chown root {} \;

[email protected] testdir]# LL

#查找到没有属主的文件, change its owner to root, pay attention to the wording. {} is a reference to the found result


[Email protected] testdir]# find-size +1k-a-type f-exec mv {} {}.new \;

[email protected] testdir]# LL

-rw-r--r--. 1 root root 1717 Oct 19:57 passwd.new

#-A indicates that the condition is with-O, or-not indicates that the condition is not.

# You need to understand the command format for the Find path condition operation. {} is the result of the lookup. The above command adds the name of the found result file to the. New


4. Summary

The Find command has many query conditions, you can also use regular expressions, you can combine conditions, and you can use commands to find the results.

Summary of use of file lookup commands

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.