Detailed explanation of the Find command in Linux

  
 

The find command is very useful in linux, even more abundant than the search function in the Microsoft system. Down, Lao Cai came to explain the syntax and actual operation of the Find command.

find syntax:

find [starting directory] looking for conditional operations

There is also a way of expressing: find PATH OPTION [-exec COMMAND { } \\;]< Br>

Because the find command will recursively search for files in the file and its subdirectories based on the options we gave, that is, looking for conditions, so I think this place is "ld"; The starting directory & rdquo; is very good.

The search condition in this command can be a compound condition consisting of logical operators not, and, or. The meanings of the logical operators and, or, not are:

(1) and: logical AND, expressed in the command by “-a”, is the default option of the system, indicating that only the conditions given When both are satisfied, the search for conditions is satisfied. For example:

find –name ‘tmp’ –x type c -user ‘inin’

% This command looks for all files that satisfy all three given conditions

(2) or: Logic OR, expressed in the command by “-o”. This operator indicates that the search condition is satisfied as long as one of the given conditions is satisfied. For example:

find –name ‘tmp’ –o –name ‘mina*’

% The command query file name is ’tmp’ or match & rsquo; Mina*’ all files.

(3) not: Logic is not indicated in the command by “!”. This operator indicates that the file that does not satisfy the given condition is found. For example:

find ! –name ‘tmp’

% This command queries all files whose file name is not ‘tmp’.

It should be noted that when using a lot of logic options, you can bracket these options. In order to avoid the shell itself misunderstanding the parentheses, you need to add the escape character “\\” before the phone number to remove the meaning of the brackets.

Example:

find \\(–name ‘tmp’ –xtype c -user ‘inin’ \\)

I feel that now I should say Out of the query conditions, the contents of the option in find:

In the option, there are specific parameters:

-name ‘string & rsquo; find the file name matches the given string For all files, the wildcard characters *, ?, [ ] are available in the string.

-lname ‘String & rsquo; Find all symlink files whose filename matches the given string. The wildcards *, ?, [ ] are available in the string.

-gid n Find all files belonging to the user group with ID number n.

-uid n Find all files belonging to a user with ID number n.

-group ‘String & rsquo; Find all files belonging to the user group named string.

-user ‘String & rsquo; Find all files belonging to the user name given by the string.

-empty Look for a directory or file of size 0.

-path ’String & rsquo; Find all files with the path name matching the given string. The wildcard characters *, ?, [ ] are available in the string.

-perm Permissions Find files and directories with the specified permissions, which can be as 711,644.

-size n[bckw] Finds the file of the specified file size. The characters after n represent the unit. The default is b, which represents a block of 512 bytes.

-type x Find files of type x, where x is one of the following characters:

b block device file

c character device file

d Directory File

p Named Pipes (FIFO)

f Normal Files

l Symbolic Links

s socket Files

-xtype x is basically the same as -type, but only looks for symbolic link files.

Finding on time

-amin n Find all files that have been accessed n minutes ago.

-atime n Find all files that have been visited n days ago.

-cmin n Find all files whose file status has been modified before n minutes.

-ctime n Find all files whose file status has been modified n days ago.

-mmin n Find all files whose file contents have been modified n minutes ago.

-mtime n Find all files whose contents have been modified n days ago.

-print: Output search results to standard output.

Example: Find the root and subdirectory files that do not include the directory /root/bin, the greek user, the file type is a normal file, the file named test-find.c 3 days ago, and Structure output, find command is as follows:

find /-name “test-find.c” -type f -mtime +3 -user greek -prune /root/bin -print

Of course In this, -print is a default option, we don't have to deliberately configure it.

Let's look at the exec option again:

-exec: The shell command specified for the search structure directive. Note that the format is correct: ”-exec command {} \\;”

There must be a space between } and \\;

{} means that the command parameter is The file found; the end of the command must end with “ \\;”.

Example: To delete the file searched by the above example, the command is as follows:

find /-name “test-find.c” -type f -mtime +3 -user greek -prune /root/bin -exec rm {} \\;

find command directive example:

find . – name ‘main*’ – exec more {} \\;< Br>

% Finds all files in the current directory that start with main and displays the contents of those files.

find . \\(- name a.out – o – name ‘*.o’\\)> – atime +7 – exec rm {} \\;

% Deletes a .out or *.o files that have not been accessed in the current directory for all weeks.

% The “.” in the command indicates the current directory. At this time, find will start from the current directory and find files in its subdirectories one by one that meet the specified conditions.

% “\\(” and “\\)” means parentheses (), where “\\” is called an escape character. The reason for this is that for the Shell, (and) has a different meaning, not the purpose for combining conditions here.

% “-name a.out” means looking for a file named a.out;

% “-name ‘*.o’” means looking for All files whose names end in .o.

The -o between these two -names represents a logical OR (or), which is to find a file whose name is a.out or whose name ends with .o.

% The find command finds the file in the current directory and its subdirectories, and then judges whether the last access time is 7 days ago (condition -atime +7), and if so, then This file executes the command rm(-exec rm {} \\;).

where {} represents the currently found qualified file name, \\; is required by the grammar.

% The last \\ in the first line of the above command is a continuation character. When the command is too long to write in one line, you can enter a \\ and the system will display a > to instruct the user to continue entering the command.

Copyright © Windows knowledge All Rights Reserved