Linux Find command

Source: Internet
Author: User

Becausef i n dhas powerful features, so it has a lot of options, most of which are worth taking the time to understand. Even if the system contains a network file system(N F S),f i n dcommands are equally valid in the file system, and only you have the appropriate permissions.
When running a very resource-intensivef i n dcommand, many people tend to put it in the background, because traversing a large file system can take a long time(Here's the point3 0 Gfile system with more than bytes).

F i n dthe general form of the command is:
CODE:
Find Pathname-options [-print-exec-ok]Let's take a look at the parameters of the command:
QUOTE:
Pathname:findthe directory path that the command looks for. For example with.to represent the current directory, using the/to represent the system root directory.
-print:Findcommand to output the matched file to standard output.
-exec:Findcommand to execute the parameter for the matching file .s H e l lcommand. The corresponding command is in the form' command ' {} \;, note{ }and the\; the space between.
-ok: And-e x e cis the same, except that it is performed in a more secure mode .s H e l lcommand, a prompt is given before each command is executed, allowing the user to determine whether to execute.

Check it out first.FindThe parameters that are taken, what functions can be achieved
FindCommand Options
-name: FollowFile name to find files.
-perm: FollowFile permissions to find files.
-prune: Use this option to makef i n dCommandNot found in the currently specified directory, if you are using both-depthoption, then-prunewill bef i n dcommand ignored.
-user: FollowThe file belongs to the owner to find the file.
-group: FollowThe group that the file belongs to to locate the file.
-mtime-n +n: FollowFile change time to find the file,-Nindicates that the file change time is nowNdays or less,+ Nindicates that the file change time is nowNdays ago. F i n dCommand and-a t i m eand the-C t i m eoptions, but they all-M t i m eoptions.
-nogroup: FindA file that does not have a valid owning group, that is, the group to which the file belongs/e t c/g r o u P sdoes not exist in the.
-nouser: FindA document without a valid owner, i.e. the owner of the document/E t c/p a s S W ddoes not exist in the.
-newer file1! File2:Find change time than filef i l E 1new but more than filef i l e 2the old file.
-typefind a file of a certain type, such as:

CODE:
B-block device files.
D-directory.
Ccharacter device files.
P-pipeline files.
LSymbolic link file.
F-normal file. -size N:[C]FindFile length isNBlockThe file, withCindicates the length of the file as a byte count.
-depth:When looking for a file, first find the file in the current directory, and then look in its subdirectories.
-fstype: Find located inFiles in a file system of a certain type, these file system types can typically be used in a configuration file/E t c/f s t a b, this configuration file contains information about the file system in the system.

QUOTE:
-mount: Do not cross the file system when locating filesm o u n TPoint.
-follow: Iff i n dcommand encounters a symbolic link file, it tracks to the file that the link points to.
-cpio: Use for matching filesC P i ocommand to back up these files to a tape device.
UseexecorOKto performShellCommand

QUOTE:
UseFind, just write the desired action in a file and you can use theexecto matchFindfind, very convenient
(only allowed in some operating systems-e x e coption to perform such asL Sorls-lsuch a command). Most users use this option to find old files and delete them. It is recommended that the actual implementationR Mbefore you can delete a file, it is best to useL Scommand to check that they are the files that you want to delete.
E x e Coption followed by the command or script you want to execute, followed by a pair of{ }, a space and a\, and the last is a semicolon.
In order to usee x e coption, you must use bothP r i n toptions. If you verifyf i n dcommand, you will see that the command outputs only the relative path and file name from the current path. For example, in order to usels-lcommand to list the files that are matched to, you can putls-lcommand placed inf i n dof the command-e x e coption in

CODE:
# Find. -type f-exec ls-l {} \;
-rw-r--r--1 root root 34928 2003-02-25./conf/httpd.conf
-rw-r--r--1 root root 12959 2003-02-25./conf/magic
-rw-r--r--1 root root 2003-02-25./conf.d/readmein the example above,f i n dcommand matches all the normal files in the current directory and-e x e coption, usels-lcommand to list them.
In/L o G Sfind change time in directory5files and delete them before the date:

CODE:
$ find Logs-type f-mtime +5-exec rm {} \;remember that ins H e l lbefore you delete a file in any way, you should look at the appropriate file first, be careful! When using such asM vorR Mcommand, you can use the-e x e cthe Safe mode of the option. It will prompt you before you work on each file that is matched to it.
In the following example,f i n dcommand to find all filenames in the current directory to. L O Gend, change time in5and delete them, just give them a hint before deleting them.

CODE:
$ find. -name "*.conf"-mtime +5-ok rm {} \;
? NPressykey to delete the file, pressNkey is not deleted.
Any form of command can be-e x e coption is used.
In the following example, we useg R E Pcommand. f i n dthe command first matches all files named"passwd*"of files, such aspasswd,Passwd.old,Passwd.bak, and then
Yesgrepcommand to see if any of these files have aSamuser.

CODE:
# find/etc-name "passwd*"-exec grep "Sam" {} \;
Sam:x:501:501::/usr/sam:/bin/bash.-execexample of executing a script
UseFindcommand to find a time period OnePoint to APoint ofShell
Create a scriptJudgetime, the contents are as follows:
Ls-l $*|awk ' {split ($8,hour, ":"); if (hour[1]>23 | | HOUR[1]

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.