Getting Started with Linux (ii): Find instruction Usage __linux

Source: Internet
Author: User
Tags file permissions

We know that the Linux find command can search for the specified file in the directory and do the appropriate action. The Find command provides a phase

When the powerful search conditions, the function is very powerful. So today we're going to analyze the usage of find. Unlike which and Whereis,

Find in the search for files is true in the traversal of all the files, if find did not found the target you need, the system

There must be no files that you need, so find is more detailed when used than any other lookup instruction when we are running a

A resource-consuming find instruction, people tend to run it in the background because traversing a large file system may require

A long time.

Command format

Find Pathname-options[-print-exe-ok ...]

Command features

Locate the file in the file tree and do the appropriate action (possibly accessing the disk).

Command Arguments

The search path for the pathname:find command lookup, for example, to represent the current directory,/to represent the root directory

-print: Will find the matching file and output it to standard output

-exe: Find a matching file and execute the shell command given by this parameter, the corresponding command in the form of ' command ' {} \;

-ok: The same as the-exe, but in a more secure mode to execute, before each execution of the command will ask, let the user choose whether to perform

Command Options

-name: Find by file name

-perm: Find by file permissions

-prune: Use this option to make find not found in the currently specified directory

-user: Find by user who owns the file

-group: Follow the user groups described in the file to find

-mtime-n/+n: According to the file's modified time to find,-n means that the file modification time is now n days, +n said the file modification time is now n days away, found command inside there are-atime,-ctime, the role and-mtime

-nogroup: Find the file for the group that is not valid, that is, the file does not exist in Etc/group

-nouser: Find the file for the invalid owning user, that is, the file does not exist in ETC/PASSWD

-newer File1. File2: Find modified files that are newer than file1 but older than file2

-type: To find by file type:

B-block device files

C-Character device files

D-Directory files

L-Symbol link file

F-Normal file

P-Pipe file

-size [N]:[c] finds files with a file size of n blocks, with C indicating file size in bytes

-depth: Indicates that when locating a file, it first looks in the current directory and then looks in the subdirectory of the current directory

(1) Follow-name to find

①, regardless of the current search path is what, when we want to search in their own root directory down a file, you can use ~ to represent the entire root directory, below to see a chestnut:

where find ~-name "Fuxin"-print This command is to search file name fuxin file by file name and

②, when we search for the target file in the current directory and its subdirectories, we can use the. To replace the current directory, as follows:

Its output, note that the filename here should be enclosed in double quotes.


③, when we want to look in the current directory for a file whose filename starts with a lowercase letter, you can use the

Ind.-name "[a-z]*"-print, for example below :

In uppercase letters, you just need to change A-Z to a-Z.

④, when we want to find files in the home directory, we can use:

Find ~-name "*"-print or find.-print , examples are as follows:

Because there are too many files I only cut a part of it, the rest of you can try it on your own.

(2) Follow-perm to find

Written in front: we know that a file has three types of rights management: User,gorup,other, the properties of the execution of the event have read, write, execute, three categories, if a class of people with a octal to express, we can use a three-digit octal number to represent the permissions of a file. For example, first ~

As shown in the figure above, for file Fuxin, for user permissions for read and write operations, the corresponding octal system is 7, and for the group read and write operations, the corresponding octal system for 7, for other rights for read operations, the corresponding octal system is 5, so the file Fuxin permissions for 775, It is convenient to represent the permissions of a file in such a way.

①, if you are looking for a file in the current directory that has a permission of XXX, you can use:

Find .-perm xxx-print, examples are as follows:

When we are looking for a file with three categories of people with permission to be the same file, such as 777, you can speak octal preceded by a bar, such as-007, representing 777, examples are as follows:

(3) How to avoid a directory when looking for a file.

For example: Under directory Dir1 to find files that are not dir2 under directory

Find Test-path "Dirt/dir2"-prune-o-print

Description

Find [-path ...] [Expression]

At the back of the path list is the expression

-path "Test"-prune-o-print is a shorthand expression for-path "test"-a-prune-o-print, in order,-A and-O are short-circuit evaluated, with Shell && and | | Similar if

-path "Test" is true, the-prune is evaluated,-prune returns TRUE, and the logical expression is true, otherwise it is not evaluated-prune, and the logical expression is false. If the-path "test"-a-prune is false, the-print is evaluated,-print returns TRUE, or the logical expression is true, otherwise no value is-print, or the logical expression is true.

The exception to this expression combination can be written in pseudo code:

If-path "Test" then

-prune

Else

-print

(4) using the-user and-nouser options

① by file owner,-user

Example: In the home directory to find the owner of the User1 file, you can use:

Find ~-user fuxin-print

This file is too much, I only cut a part, the actual operation of the subject.

Similarly, if you are looking for a file in another directory, just change the pathname to a different path.

②, find files that have been deleted by the master account, you can use the-nouser option.

For example: Find all of these files in the/home directory, command: Find/home-nouser-print

(5) using the-group and-nogroup options

①, like-user and-nouser,-group and-nogroup are for the groups that belong to the file, to find files in the home directory that are fuxin by the user Group, command:

Find ~-group fuxin-print

②, to find files in a directory that are not valid for a group, you can use the-nogroup directive:

Find Pathname-nogroup-print

(6) Using the-mtime-n +n option

Find files in the system root directory that are away from today 1st

Find /-mtime-1-print

Similarly, to find directories in the directory before N-day, change-N to +n.

(7) using-newer file1. File2, find a file that is newer and older than the File2 file file1

For example: Look for a file in home that is older than the test1 new test2

Find ~-newer test1. Test2-print

(8) using the-type option

Look for files with a file type D in a directory:

Find ~-type d-print

When looking for different types of files, you just need to change the file type.

(9)-size option to find a file with a length of n block (1 blocks equals 512 bytes)

If you want to find a file with a length of 10000 bytes under home:

Find ~-size 10000c-print

If you want to find a 10000-block file under Home

find~-size 10000-print

(Ten)-depth option

When you use the Find command, you may want to match all the files first and then look in the subdirectory. Use the depth option to make the Find command do so. One reason to do this is that when you back up the file system to tape using the Find command, you want to back up all the files first, and then back up the files in the subdirectory.

Find a file in the root directory named file

Find /-name "file"-depth-print


The above is the use of find common instructions, wrote a long time, I hope we can point out the deficiencies of common progress, thank you.

This article from the "Fu da Xin" blog, reproduced please contact the author.

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.