Linux Find command

Source: Internet
Author: User

The Find command is a common file Finder tool for Linux systems, and can also be used to perform appropriate operations on files found.

Format

Find [path found] [find condition] [processing action]

Default behavior:

Find path: Default is not specified as current path

Find condition: defaults to all files under the current path

Handling actions: Default to Print

Note: If the search condition is a directory, you need to bring the



Search criteria:

1,-name "file name", support using file name wildcard.

There is another option for the file name-iname This option is similar to-name, but is not case-sensitive when searching

# Find. -name "*.sh"./useradd.sh./while.sh

2,-user UserName, according to the file owner search.

# Find. -user tom-ls523272 0-rw-rw-r--1 tom Tom 0 Mar 14:14./filetest

3,-group GroupName, according to the documents belong to the group search .

# Find. -group tom-ls523272 0-rw-rw-r--1 tom Tom 0 Mar 14:14./filetest

4,-uid uid, according to the user's UID to find ,

-gid GID, based on the user's GID to find ,

Sometimes the user is deleted, but the user's files are still in, find these files can only be based on UID or GID to find

[Email protected] tmp]# find/tmp-uid 503-ls523272 0-rw-rw-r--1 503 503 0 Mar 14:14/tmp/fi Letest


5,-type to find by file type

F: Normal file

D: Catalogue

L: Symbolic Link

B: Block device

P: Named Pipes

S: Socket

C: Block device

# Find/tmp-type d-ls523265 4 DRWXRWXRWT 4 root root 4096 Mar 14:25/tmp523274 4 Drwxr-xr-x 2 R Oot root 4096 Mar 14:25/tmp/dir1655081 4 DRWXRWXRWT 2 root root 4096 Feb 10:14/tmp/.i Ce-unix


6.-Size: Search by file size

-size [+|-] #Unit

Usage:

-size +2m \ \ Find files larger than 2M-size-2m \ \ Find files less than 2M (#-1m, find files below 1M)-size 2M \ \ Exact match 2M file



7, based on time stamp to find

(a), in days (time)

-Atime [+|-] #, #为天

+: Indicates (#+1) days have been visited;
-: Indicates that the # was visited within a day;
Unsigned: indicates shorter than (#+1) > x >= #天的时间段被访问过;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/75/wKioL1bpCqXwVH3FAAAaSLjn5ZU761.png "style=" float: Left; "title=" 999.png "alt=" Wkiol1bpcqxwvh3faaaasljn5zu761.png "/>








(b), in minutes (min)

-amin [+|-]#
-mmin
-cmin


8, according to the right to find,-perm [+|-] MODE

MODE: Exact Match

+mode: Often used to find out the existence of specific permissions for a particular class of users;

---------as long as the owner, group, other users, any permission bit matching can be

-mode: Each class of user specified to check the permission bits are matched;

If there is a 644-permission file

[Email protected] tmp]# ls-l-rw-r--r--. 1 root root 0 Mar 15:33 test1 \ 644-rw-------. 1 root root 0 Mar 15:40 test2 \ 600-rw-r-----. 1 root root 0 Mar 15:40 test3 \ 640

Exact match, all the bits must match to be able.

# find/tmp-perm 644-ls523272 0-rw-r--r--1 root root 0 Mar 15:33/tmp/test1

+mode, as long as you have a permission to match

[Email protected] tmp]# find/tmp-perm +006-type f-ls523272 0-rw-r--r--1 root root 0 Mar 16 15:3 3/tmp/test1
# find/tmp-perm +666-type f-ls523272 0-rw-r--r--1 root root 0 Mar 15:33/tmp/test1523273 0 -RW-------1 root root 0 Mar 15:40/tmp/test2523274 0-rw-r-----1 root root 0 Ma R-15:40/tmp/test3

-mode, each bit's permissions are checked and must be precisely matched to.

# Find/tmp-perm-240-type f-ls523272 0-rw-r--r--1 root root 0 Mar 15:33/tmp/test1523274 0    -rw-r-----1 root root 0 Mar 15:40/tmp/test3# find/tmp-perm-444-type f-ls523272 0-rw-r--r-- 1 root root 0 Mar 15:33/tmp/test1



Combination conditions:
-A: With, simultaneously satisfied, multiple conditions, default is-a option
-O: Or,
-not,!: Non, take counter

# find/tmp-name "*.doc"-a-perm 640-ls523274 0-rw-r-----1 root root 0 Mar 15:40/tmp/test.doc

Non-A, and non-B = Non (A or B)

# find/tmp-not \ (-user tom-o-name "*.txt" \)-ls523273 0-rw-------1 root root 0 Mar 15:40/t mp/test.xml# find/tmp-not-user tom-a-not-name "*.txt"-ls523273 0-rw-------1 root root 0 Mar 1 6 15:40/tmp/test.xml


Non-A, or non-B = Non (A and B)
Find/tmp-not-user hadoop-o-not-name "*.txt"
Find/tmp-not \ (-user hadoop-a-name "*.txt" \)


————

Handling actions:
-print: Printed on the standard output;
-ls: Output The file information in a long format;
-exec COMMAND {} \; : Executes the specified command on the found file
-ok COMMAND {} \; : Interactive-exec;

Find passes all found files to the command specified by-exec at once

Find | Xargs COMMAND
Find/tmp-perm-003-type F | Xargs chmod o-wx
Like this xargs is unable to completely replace the-exec true face file renaming operation
find/tmp/-iname *.doc-exec mv {} {}x \; Change the original doc ending file to the docx end



Practice:
1, find/var/directory is the owner of the root and belong to the group mail all files;
# find/var/-user root-a-group Mail

2. Find files that are not root, bin, or Hadoop in the/usr directory;
# find/usr/-not-user root-a-not-user bin-a-not-user Hadoop
# find/usr/-not \ (-user root-o-user bin-o-user hadoop \)


3, find the/etc/directory in the last week of its content has been modified, and does not belong to the root or Hadoop files;
# find/etc/-mtime-7-a-not-user root-a-not-user Hadoop
# find/etc/-mtime-7-a-not \ (-user root-o-user hadoop \)


4, find the current system does not belong to the main or belong to the group, and the last 1 months have been visited documents;
# Find/\ (-nouser-o-nogroup \)-a-atime-30

5, look for the/etc/directory of more than 1M and the type of ordinary files of all files;
# find/etc/-size +1m-a-type F


6, find/etc/directory All users do not have permission to write files;
# find/etc/-not-perm +222
None of them: on the contrary: any one has
All have: the opposite: At least one has no

7, find the/etc/directory at least one class of users do not have write permission;
# find/etc/-not-perm-222

8, find/etc/init.d/directory, all users have execute permission and other users have write permission files;
# find/etc/init.d/-perm-113



Find/tmp-iname "*.doc"-exec mv {} {}x \;
Find and Xargs
When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.

The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.

In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there will be too many processes and degraded system performance, so the efficiency is not high;

With the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.










This article is from "Rookie Diary" blog, please make sure to keep this source http://zkxfoo.blog.51cto.com/1605971/1751888

Linux Find command

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.