This is the 29th day of my participation in the August Wenwen Challenge.More challenges in August

preface

As a developer, we often encounter in the server to search a file or a folder, Linux server is not like WIndows that can be searched in the visual interface operation, then we often use the find command to query. Do you understand the find command? This article will share with you the operation of the find command.

Find command for the first time

Using the Linux find command, you can find files in a specified directory. Any string before the parameter is treated as the name of the directory you are looking for. If you use this command without setting any parameters, the find command will find subdirectories and files in the current directory. All subdirectories and files found are displayed.

Syntax format

The syntax format of the find command is as follows:

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;
Copy the code
  • Find: keyword of the find command.
  • Path: indicates the path of the directory to be searched. The following three paths can be used: [~] [.] [/] The meanings of these paths are as follows: “~” indicates the $HOME directory;” “Indicates the current directory. “/” indicates the root directory.
  • Options: indicates the search method, for example, -name, -user, and -ctime.
  • Print: standard output of the result.
  • Exec: Executes the shell command given by this parameter on the matching file.
  • Ok: Has the same function as exec, except that a prompt is given before the command is executed for the user to confirm the execution.
  • command {} ; Command format output.

Common search methods

To find the command describe
-mount, -xdev Check only files in the same file system as the specified directory, avoiding listing files in other file systems
-amin n It has been read in the last n minutes
-anewer file A file that has been read later than the file file
-cnewer file A file that is newer than file file
-empty Empty file -gid n or -group name: gid is N or group name is name
-ipath p, -path p Ipath ignores case for files whose path names match P
-name filename, -iname filename The file name matches the name of the file. Iname ignores case
-size n The file size is n units, where B represents a block of 512 bits, C represents a number of characters, k represents a kilo bytes, and W represents two bits.
-perm Search by execution permission
-user username Search by file owner
-group groupname Look by group
-mtime -n +n To search for files by file change time, -n indicates within N days, +n indicates n days ago
-atime -n +n Search for files by file access time. -n indicates within N days, and +n indicates n days ago
-ctime -n +n To search for a file by creation time, -n indicates that the file is created within N days, and +n indicates that the file was created n days ago
-nogroup Check whether the file has no valid parent group. That is, the parent group of the file does not exist in /etc/groups
-nouser The owner of the file does not exist in /etc/passwd
-type b/d/c/p/l/f Lookup is block device, directory, character device, pipe, symbolic link, plain file
-follow If a symlink file is encountered, trace the file to which the link refers
-prune Ignore a directory

Query by name

List all files with a. Java suffix in the current directory and its subdirectories:

find . -name "*.java"
Copy the code

In the current directory and subdirectories, look for Java files that begin with uppercase letters

find . -name '[A-Z]*.java' 
Copy the code

Finds files starting with B in a specified directory and its subdirectories

find /javafile -name 'B*'
Copy the code

Search by permission

In the current directory and subdirectories, search for files whose owners have read/write execution permission and other files with read execution permission. As the root account is used this time, you have read and write permissions on all files and folders.

 find . -perm 755

Copy the code

Search by time

Search for files that have been changed within 50 days. There has been no update for a long time, so the number of days is set quite a lot. In the actual development, the query is only 1-2 days of data.

find . -mtime -50 -type f
Copy the code

Find files that have been accessed during the day

find . -atime -1 -type f
Copy the code

Recommended column

Build a personal tech blog from scratch

Java full stack architect

About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.

Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.