Linux file management knowledge: Find files (Part 2)

The previous article introduced in detail the basic operations of the tools or command programs locate and find in the Linux system. Well, today's article is followed by an introduction to searching for file-related operations.

The entries in the action list to which the Find command belongs contribute to the output we want. As mentioned in the previous article, the find command is based on the search results to execute the operation command program.

Predefined action descriptions:

operate

describe

-delete

Delete the currently matching file

-ls

Executes the equivalent ls -dils command on matching files. and send the result to standard output

-print

Pipe the full pathnames of matching files to standard output. This is the default action if no other action is specified

-quit

Once a match is found, exit

For example:

find ~   

find ~ -print

// This command outputs every file and subdirectory contained in the home directory. Output as a list. The output of the two commands is exactly the same.

Delete files with certain conditions

Use the find command to delete files that meet certain conditions.

For example:

find ~ -type f -name '*.BAK' -delete

//Delete the file with the extension ".BAK" (which is usually used to specify the backup file), and search for the file name ending with .BAK in the user's home directory and its subdirectories . Once the files ending in .BAK are found, delete them.

Note: You must be extra careful when using the -delete action command. So what should we do better? The proper way is that we can first use the print program command to test whether it meets the search results we want.

Logical Relationships - Logical Operators

For example:

find ~ -type f -and -name '*.BAK' -and -print

Once this program command is executed, it will search for related files with a .BAK suffix, and output the relative path name of the qualified file to us in standard. The reason for command execution is determined by the logical relationship between each test and operation.

So how do logical operators affect how commands are executed?

test/behavior

Executed only if

-print

Only when -type f and -name '*.BAK' are true

-name

'*.BAK' only when -type f is true

-type f

is always executed because it is the first test/behavior in the relationship with -and

From this table, it can be seen that the logical relationship between tests and behaviors determines which one will be executed first, and the order of tests and behaviors is also important.

Let's rearrange the order between tests and behaviors: let the -print behavior be the first, then the result will be quite different:

For example:

find ~ -print -and -type f -and -name '*.BAK'

The above command prints each file (the -print behavior is always true) before testing the file type and the specified file extension.

user defined operation

In addition to the predefined operations, we can invoke arbitrary commands. Usually through the -exec command.

For example:

-exec command {} ;

command Represents the name of a command, {} represents the symbolic representation of the current path name, and the semicolon represents the necessary separator to indicate the end of the command.

Of course, how to interactively execute a user-defined action? By using the -ok action instead of -exec, it prompts the user before executing each specified command:

find ~ -type f -name 'foo*' -ok ls -l '{}' ';'

< ls ... /home/me/bin/foo > ? y

-rwxr-xr-x 1 me me 224 2007-10-29 18:44 /home/me/bin/foo

< ls ... /home/me/foo.txt > ? y

-rw-r--r-- 1 me me 0 2008-09-19 12:53 /home/me/foo.txt

Xargs command

Usage of the xargs command—converts the standard input or the parameter list constructed by the pipeline into the parameter list of a specific command and runs the specific command. In layman's terms, xargs takes the output of one command and executes it as the parameter list of another command. For our example, we can use it like this:

find ~ -type f -name 'foo*' -print | xargs ls -l

-rwxr-xr-x 1 me me 224 2007-10-29 18:44 /home/me/bin/foo

-rw-r--r-- 1 me me 0 2008-09-19 12:53 /home/me/foo.txt

Here we can draw a conclusion:

The execution result of the find command is passed to the xargs command through the pipe symbol |, and then the xargs command uses the output result of the find command as the parameter of the ls command, and finally executes the ls -l command.

Remarks: When the number of parameter lists in a command line is limited, once the maximum length supported by the linux system is exceeded, the xargs command will execute the command, and then repeat this process until the standard input is exhausted.

Next, let's look at how to create a directory that contains many subdirectories and files:

[root@linuxprobe~]$ mkdir -p playground/dir-{00{1..9},0{10..99},100}

[root@linuxprobe~]$ touch playground/dir-{00{1..9},0{10..99},100}/file-{A..Z}

The power of Linux commands is really amazing! With just these two lines of commands, you can create a subdirectory containing one hundred subdirectories, and each subdirectory contains 26 empty files.

Now let's see how the Linux command achieves this miracle!

A simple mkdir command, a special shell expansion (braces) including a touch command. By combining the mkdir command with the -p parameter option (which causes the mkdir command to create the parent directory of the specified path), and curly braces, it is possible to create a hundred directories.

Remarks: The touch command is usually used to set or update file access, change, and modification time attributes.

Today's introduction to the use of Linux system file search commands is temporarily over. The locate, updatedb, find and xargs commands are all part of the findutils GNU project. The GUN project provides a ton of pretty good online documentation that we all need to read. Therefore, it is strongly recommended that you can refer to the book "This is how linux should be learned" . The introduction to the file management level of the linux system is very detailed!

Guess you like

Origin blog.csdn.net/weixin_56035688/article/details/132597196