File lookup commands in Linux

Source: Internet
Author: User
Tags file size mail linux

In Linux, file lookups are commonly used with two commands, namely locate and find.

#locate based on locate database lookup, not real-time lookup, not exact lookup. Faster.

#find do not search according to the database, real-time lookup, traverse directory lookup, accurate search, slow speed.

# Locate the keyword to look for

-N Displays only the first 10 rows that are matched to.

-c statistics are matched to the total number of results.

Locate is based on the locate database to find, so faster, if a file has just been stored in the Linux system, and then use locate to find, it may not find, because the locate database, there is no relevant records of the file, this time, We need to manually update the locate database, using the command

UpdateDB will manually generate the database that the locate command relies on

Find [dir,...] [Criteria,...] [Action ...]

If [dir] is omitted, it is the current path. If find is not followed by any parameters, it will display all the files and directories in the current directory, including hidden files

The default action for the action is displayed on the screen.

Criteria Lookup criterion

-NAME Specifies the file name. also supports globbing way file name wildcard

find/etc/-name "passwd?"

find/etc/-name "*passwd*"

-iname ignores file name capitalization in the file name.

-regex "pattern" finds files in the file name that conform to pattern and supports regular.

-user USERNAME Lookup based on user (find files belonging to a user)

-group group_name

Find/tmp-user Redhat | Ls-l This is not possible, because find transmission mechanism and ordinary command is not the same

ll ' Find/tmp-user Redhat ' This is OK, refer to the command.

-uid UID lookup based on user ID number

The-gid GID is based on the group ID number lookup.

-nouser find all files that are not owned by the owner

-nogroup find all files that have no group

-type the specified type to find

F Normal File

D Directory

L Soft Connection File

B Block Equipment

C Character device

P Pipeline File

s socket file

Combination condition:

-A

-O

-not

-size Specify File size

10M size is 10M (9-10m is compliant, 10M above and 9M below does not conform)

-10m is less than 10M

+10m is larger than 10M

-atime According to file access time (in days)

-mtime depending on how long the file was modified (in days)

-ctime Depending on the time the file was changed (in per day)

-amin According to file access time (in minutes)

-mmin According to file modification time (in minutes)

-cmim according to the file change time (in minutes)

Time defaults to days, and Min's default unit is minutes.

Cases:

-atime 3 Distance Now, just 3 days without access to the file

-atime-3 files accessed within 3 days

-atime +3 files that have not been accessed for more than 3 days

-perm 755 Searches According to the permissions of the file.

+755|/755 any of these users can satisfy their permissions. /444 Any type of user has Read permissions can be.

-755, each type of user must meet the conditions.

CTRL + a jumps to the header of the command.

Ctrl+e jumps to the tail of the command.

Action

-print prints the resulting results to the screen (the default action, which can be omitted.)

-ls display file information in long format

-ok Comandn {} \; After the file is found, execute the specified command (reminder confirmation)

-exec COMMAND {} \; After the file is found, execute the specified command (no reminder confirmation)

-exec rm {} \; Deletes the found file {} to refer to the previously found file, \; Indicates that the command has ended

-exec mv {} {}.txt \; Rename the file you find and add the. txt extension after the original name

Do some exercises below

Practice:

1, find all the files that are subordinate to the/var/directory root and belong to group mail;

[Root@honway scripts]# find/var-user root-group Mail

/var/spool/mqueue

/var/spool/mail

2, look for documents that do not belong to Root,bin or student under the/USR directory;

[Root@honway scripts] #find/usr/-not-user root-not-user bin-not-user Student

/usr/local/apache2/logs/cgisock.3302

/usr/local/apache2/logs/cgisock.32138

/usr/local/apache2/logs/cgisock.3327

3, look for files that have been modified in the last week of the/etc/directory and that do not belong to root and Apache

[Root@honway scripts]# find/etc-not (-user root-o-user apache \)-mtime-7

4, to find the current system does not belong to the owner or group, and the last 1 days have been visited files, and its owner group are modified to root;

[Root@honway scripts]# Find/\ (-nouser-o-nogroup \)-atime-1-exec chown root:root {} \;

5, look for files larger than 1M in the/etc directory and write their file names into/tmp/etc/largefiles files;

[Root@honway scripts] #for i in ' find/etc/-size +1m-exec basename {} \; ';d o echo $i >>/tmp/etc.largefiles;d One

[Root@honway ~]# find/etc/-size +1m-exec basename {} >/tmp/find.out \;

6, find the/etc/directory, all users do not have Write permission files, display their detailed information;

[Root@honway scripts]# ll ' find/etc/-not-perm +222 '

-R--------1 root root 1225 02-11 10:00/etc/gshadow

-R--------1 root root 1213 02-11 00:19/etc/gshadow-

-r--r--r--1 root root 41286 2006-11-28/etc/mail/submit.cf

-r--r--r--1 root root 628 2010-07-21/etc/selinux/config,v

-R--------1 root root 3159 02-11 10:00/etc/shadow

-R--------1 root root 3129 02-11 00:29/etc/shadow-

This article comes from "Acridine a Pooh" blog, please be sure to keep this source http://gm100861.blog.51cto.com/1930562/788831

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

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.