Unix's Find command

Source: Internet
Author: User
Tags current time uppercase letter valid file permissions

When running a very resource-intensive f i n d command, many people tend to put it in the background because it can take a long time to traverse a large file system (this refers to a file system with more than 3 0 g bytes).
The general form of the F i n d command is:
Find Pathname-options [-print-exec-ok]
Let's take a look at the parameters of the command:
Pathname the directory path found by the Find command. For example, use. To represent the current directory, and/to represent the system root directory.
The-print find command outputs the matched file to standard output.
The-exec find command executes the S H L command given by this parameter to the matching file. The corresponding command is in the form of ' C o m M and ' {} \;, note the space between {} and \;
-ok and-e x e C are the same, except that the S H L l command given in a more secure mode is executed, and a prompt is given before each command to let the user determine whether to execute.

Find command options:

The fi n d command has a number of options or expressions, followed by a horizontal bar in front of each option. Let's take a look at the main options for the command, and then give some examples.
-name finds files by file name.
-perm to find files according to file permissions.
-prune using this option allows the f i n d command not to be found in the currently specified directory, and if the-D e p t option is used at the same time, then the-P r u n e option will be ignored by the f i n d command.
-user Search for files according to the owner of the file.
-group finds files according to the group to which the files belong.
-mtime-n +n The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago. The F i n d command also has-a t i m e and-c t i m e option, but they are similar to the-m-M-e option, so we're only here to introduce-M t i m e option.

-nogroup finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in the/E t c/g r o u P S.
-nouser finds a file without a valid owner, that is, the owner of the file does not exist in the/E T c/p a s S W D.
-newer file1! File2 find change time than file F i l e 1 new but newer than file F i l e 2 old file.
-type find a file of a certain type, such as:
B-block device files.
D-Directory.
C-character device file.
P-Pipeline file.
L-Symbolic link file.
F-Normal file.
-size N[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes.
-depth when looking for a file, first find the file in the current directory, and then look in its subdirectories.
-fstype find files located in a file system of a certain type, these file system types can usually be found in the configuration file/E t c/f s t a B, which contains information about the file system in this system.
-mount does not cross the file system m o u n T point when locating files.
-follow if the f i n d command encounters a symbolic link file, it is tracked to the file that the link points to.

Use the name option:

The file name option is the most commonly used option for the f i n d command, either by using this option alone or with other options. You can use a file name pattern to match files, remembering to enclose the filename pattern in quotation marks.
Regardless of the current path, if you want to find the file name in your own root directory $ H O M e matches *. T x t file, using ~ as ' p a t h n a m e parameter, tilde ~ represents your $ h O m e directory.
$ find ~-name "*.txt"-print
You want to find all the ' * ' in the current directory and subdirectories. T x t ' file, can be used:
$ find. -name "*.txt"-print
You want the current directory and subdirectories to find file names that begin with an uppercase letter, which can be used:
$ find. -name "[a-z]*"-print
To find files with a file name beginning with H o s T in the/e T C directory, you can use:
$ find/etc-name "host*"-print
To find files in the $ H O M directory, you can use:
$ find ~-name "*"-PRI or ntf Ind. -print
To get the system running at a high load, start looking for all the files from the root directory. If you want to keep a good impression on your system administrator, it's best to think about it before doing so.
$ find/-name "*"-print
If you want to find the file name in the current directory with two lowercase letters, followed by two digits, and the last is *. T x t file, the following command will be able to return the name of a X 3 7. T x t files:

$ find. -name "[A-z][a-z][0--9][0--9].txt"-print

Using the PERM option:

You can use the-P e r m option if you want to find files in file permission mode. You may need to find files that all users have execute permissions on, or you want to see the file permission types in a user directory. When using this option, it is best to use the octal permission notation. In order to find in the current directory file permission bit 7 5 5 files, that is, the file owner can read, write, execute, other users can read, execute files, can be used:
$ find. -perm 755-print
If you want to find files that all users can read, write, and execute in the current directory (be careful with this), we can use the-P e r m option of the f i n d command. Add a horizontal bar before the octal digit-. In the following command-the P e r m represents the lookup according to the file permissions, and the ' 0 0 7 ' is exactly the same as the representation you used in the absolute mode of the C H o d command.
$ find. -perm-007-print

Ignore a directory:

If you want to ignore a directory when you are looking for a file, because you know that the directory does not have the file you are looking for, you can use the-P r u n e option to indicate the directories that need to be ignored. Be careful when using the-P r u n e option, because if you are using the-D e p t option at the same time, then the-P r u n e option will be ignored by the f i n d command. If you want to find the file in the/a P s directory, but do not want to find it in the/a p p s/b i n directory, you can use:
$ find/apps-name "/apps/bin"-prune-o-print

Use the user and Nouser options:

If you want to follow the file owner to find the file, you can give the corresponding user name. For example, in the H O M directory, look for files in which the file belongs to D a v e, which can be used:
$ find ~-user Dave-print
Find files in the/E T C directory where the file belongs to U-C p:
$ find/etc-user Uucp-print
In order to find files that are already deleted from the master account, you can use the-N o u S e r option. This will enable you to find files that have no valid accounts in the/E t c/p a s W d file. When using the-N o u S e r option, you do not have to give the user name; the f i n d command can do the work for you. For example, to find all such files in the/h o m e directory, you can use:
$ find/home-nouser-print

Use the group and Nogroup options:

Using the group and Nogroup options, like the U S e r and N o u S e r options, the f i n d command has the same option for the user group to which the file belongs, in order to find files belonging to a C C T s user Group under the/a P s directory, you can use:
$ find/apps-group Accts-print

To find all files that do not have a valid owning user group, you can use the N o g r o u p option. The following f i n d command looks for such a file from the root directory of the file system
$ fine/-nogroup-print

To find files by change time:

Find files by change time
If you want to find the file by changing the time, you can use the M t i m e option. If the system suddenly does not have free space, it is possible that the length of a file grows rapidly during this period, you can use the M t i m e option to find such a file. Use a minus sign-to limit the time to change the file within the current n days, and use the Plus + to limit the change time before the current n days of the file. To find files that change within 5th of the system root directory, you can use:
$ find/-mtime-5-print
In order to find files that change time before 3rd in the/V a r/a d m directory, you can use:
$ find/var/adm-mtime +3-print

To find new or older files than a file:

You can use the-n e w e r option if you want to find all files that have changed to a new but older file than the other. The general form of it is:
Newest_file_name! Oldest_file_name
Among them,. is a logical non-symbol. There are two files, and their change time is about two days apart.

The following gives the f i n d command to be able to look for a change time than file a G E. A W k new but more than file B E L t S. A w k old file:

If you want to use this option for the f i n d command to find files that change within two hours, there is no file that can be used to compare the change time unless there is a ready-made file that changes exactly two hours ago. To solve this problem, you can first create a file and set its date and time stamp to the required time. This can be accomplished with the T o u C h command.
Assuming the current time is 2 3:4 0, and you want to find files that change within two hours, you can first create such a file:

A compliant file has been created; Here we assume today is May 4, and the file change time is 2 1:4 0, which is just two hours earlier than now. Now we can use the-n e w e r option in the f i n d command to find all files that have changed within two hours of the current directory:
$ find. -newer Dstamp-print

Use the type option:

If you want to find all the directories in the/e T-C directory, you can use:
$ find/etc-type D-print
In order to find all types of files except directories in the current directory, you can use:
$ find. ! -type D-print
In order to find all the symbolic link files in the/E T C directory, you can use:
$ find/etc-type L-print

Use the size option:

Files can be found by file length, and the length of the file referred to here can be measured either in blocks (b l o C k) or in bytes. The length of the measured file in bytes is expressed as n C, and the length of the block measurement file is only represented by a number. As far as I am concerned, I always use a byte-count method, when looking up files by file length, most people prefer to use this byte-length file size, rather than the number of blocks, unless you are viewing the file system sizes, because it is easier to convert by using blocks to measure.
In order to find files with a file length greater than 1 m bytes in the current directory, you can use:
$ find. -size +1000000c-print
In order to find files with a file length of exactly 1 0 0 bytes in the/h o m e/a p a c h e directory, you can use:
$ find/home/apache-size 100c-print
In order to find files with a length of more than 1 0 blocks in the current directory (a piece equals 5 1 2 bytes), you can use:
$ find. -size +10-print

Using the depth option:

When using the f i n d command, you might want to match all the files before looking in subdirectories. Use the D e p t h option to enable the F i n d command to do so. One reason for this is that when you back up the file system to tape using the f i n command, you want to back up all the files first, and then back up the files in the subdirectories. In the following example, the f i n d command starts at the root of the file system and looks for a name C O N. F I L E's file.
It will first match all the files and then go to the subdirectory to find them.
$ find/-name "CON. FILE "-depth-print

Using the Mount option:

To find files in the current file system (without entering other file systems), you can use the M o u n T option of the f i n d command. In the following example, we begin by looking at the file in the current directory that ends in x C for the file name in this filesystem:
$ find. -name "*. XC "-mount-print

Use exec or OK to execute the shell command:

When you match some files, you might want to do something about it, and then you can use the-e x E c option. Once the f i n d command matches the corresponding file, it can be manipulated with the commands in the-e x E C option (only the-e x E C option is allowed in some operating systems to execute commands such as L s or ls-l). Most users use this option to find old files and delete them. Here I strongly recommend that before you actually execute the r m command to delete a file, it is best to use the L S command to check that they are the files you want to delete. The E x E C option is followed by the command to be executed, followed by a pair of {}, a space and a \, and finally a
Semicolon. To use the E x E C option, you must use the P r i n t option at the same time. If you verify the f i n d command, you will see that the command outputs only the relative path and file name from the current path.
In order to list the matched files with the Ls-l command, you can place the Ls-l command in the-e x E c option of the f i n d command, for example:

In the above example, the f i n d command matches all normal files in the current directory and lists them in the-e x E C option using the Ls-l command.
In order to find files that change time before 5th in the/L o G S directory and delete them, you can use:
$ find Logs-type f-mtime +5-exec rm {} \;
Remember that before you delete files in any way in the S H e l L, you should be careful when you look at the appropriate files.

Finally, to teach you a useful example, use the Find command to count the number of records in a file:

Find. -type f-exec cat * {} \; | Wc

This command counts the number of records in all the files in the current directory and is a very useful command in my work.

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.