Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The main functions of Linux/Unix 's find command

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "the main functions of Linux/Unix 's find command". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the main functions of Linux/Unix 's find command.

The main features are as follows (all tests start to match in the current directory (test), search):

Basic usage

List all files and subdirectories under a given directory (base_path):

Find base_path-print

Add: search by file name and regular expression, using the option-name or-iname (ignore case):

Find base_path-name 'xxx'-printfind base_path-iname' xxx'-print

Negative parameters can be used! Exclude assigned modes

All files except txt text files will be printed here.

Search based on directory depth

The find command specifies that all subdirectories are traversed. Use-maxdepth and-mindefth to limit the depth of directories that the find command traverses, and the find command does not search for symbolic links by default, and you can change this behavior with the-L option.

For example, the parameter of-maxdepth is 1, which only matches under the current directory.

The parameter of-mindepth represents the shortest distance from the directory to which the match begins to base_path.

Search based on file type

Use-type to specify the file types to search for, linux/unix will all be regarded as files (file types are: normal file f, directory d, symbolic link l, character device c, block device b, socket smei FIFOMIP), using the-type option we can filter the file type.

Only all ordinary files and directories under a particular item will be matched here.

Search based on the timestamp of the file

Each file in the Linux/Unix file system has three kinds of timestamps, access time (- atime), modification time (- mtime), change time (- ctime), the unit is the number of days, specified by an integer, the number is preceded by a +, which means greater than this time; plus -, indicates less than this number of days; and does not add means exactly this number of days.

The file here was created before I took a screenshot. It takes less than one day to visit, modify and change.

Of course, if you use minutes as the unit, you can use the option (- amin) (- mmin) (- cmin). Let's test the modification time as follows

Search based on file size

Find provides a unit option to specify the file size to search for files that match the size, and this search often makes users feel very comfortable (b: block, c: byte, w: word, k: kilobyte, M: megabyte, G: gigabyte).

Before searching, let's use the ls (list) command to look at the file information in the current directory:

The fifth column of the information is the size of each file directory (in bytes). We search by specifying matching criteria:

After testing, under the start directory, the file type is a normal directory, and the file size greater than 30 bytes is zl.txt.

Matching based on file permissions and ownership

The-perm option specifies that find refers to the file that matches the specified permissions, and the parameter is the permission code corresponding to the file.

We can still refer to the first column of all the file information in ⑥, where we need to know something about file permissions. As follows, we look for ordinary files with permissions of 644, that is, users can read and write, group users can read, and others can read.

You can also use the option-user to match the files owned by the specified user. The parameter is username or UID.

Use find to perform the corresponding operation

For example, delete files, use the-delete option; delete all .txt ordinary files in the test directory

You can also use the-exec option in combination with other commands to operate on the file more efficiently, change the ownership of the file, copy the file, etc. The find command uses a pair of curly braces {} to represent the file name, and for each matching file, the find command replaces {} with the corresponding file name; if the-exec command has multiple parameters, you need to pay attention to the end with "\" "or" + ", the former indicates escape, otherwise the system will think it is the end of the find command.

We converted all the .txt files under the test directory from user lihongbo to user litao999. We must do this as user root, and chown is used to change permissions:

Specify that find skip specific directories

Use the-prune option to skip some of the directories we're searching for that we obviously don't need.

Skipped the. / test1 directory

It is important to point out that the priority of options should also be taken into account, because it will affect the efficiency of the execution of the entire command.

At this point, I believe you have a deeper understanding of "the main functions of Linux/Unix 's find command". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report