Linux Commands | Find of Common Commands

  table of Contents

1. Basic concepts

2. Grammatical form and common parameters

2.1 Grammatical form

2.2 Common parameters

Three, example explanation

3.1 -name/-iname parameter

3.2 -type parameter

3.3 -ctime parameter

3.4 -cnewer parameter

3.5 -exec/-ok parameter

 Four, summary

Five, references


When we use Linux, one of the commands we often use to find files is find. Understanding the function and use of find can enhance work efficiency. The following describes the usage of find with specific examples.

1. Basic concepts

The find command is used to find files (referring to ordinary files and directories) in the specified directory and subdirectories .

2. Grammatical form and common parameters

2.1 Grammatical form

find [path...] [expression]

Among them, path is the search path, and multiple paths are separated by spaces; expression: consists of options and actions.

2.2 Common parameters

-name pattern: Find files according to the specified pattern;

-iname pattern: same as -name parameter, the difference is that it is not case sensitive;

-type c: Find files according to file type c;

Among them: c represents the file type, the possible values ​​are:

          d: directory;

          f: ordinary file;

          l: symbolic link;

          s: socket;

          c: special character file;

          b: special block file;

          p: command pipeline (FIFO)

-ctime n: find files modified in the past n days;

-cnewer file:  find files that are newer than file file ("update": refers to the modification time);

-exec commond {} \;: execute the shell command listed in this parameter on the found file, pay attention to a space between {} and \;

-ok commond {} \;: The function and parameter mode are the same as -exec, but every time commond is executed, the user will be asked whether to execute it. Enter Y/N to select.

Three, example explanation

3.1 -name/-iname parameter

First, create the file readme.md in the current directory, and then find the file whose name matches the pattern "*README.md". 

[root@localhost go]# touch readme.md
[root@localhost go]# find . -name "*README.md"
./README.md
./misc/trace/README.md
./src/cmd/compile/README.md
./src/cmd/compile/internal/ssa/README.md
./src/cmd/vendor/github.com/google/pprof/third_party/d3/README.md
./src/cmd/vendor/github.com/ianlancetaylor/demangle/README.md
./src/cmd/vendor/golang.org/x/sys/unix/README.md
./test/README.md
[root@localhost go]# 

 As you can see from the above example, the files satisfying the pattern "*README.md" are all found, but the lowercase file readme.md is not found. Let's look at the result of replacing the parameter -iname:

[root@localhost go]# find . -iname "*ME.md"
./README.md
./misc/trace/README.md
./src/cmd/compile/README.md
./src/cmd/compile/internal/ssa/README.md
./src/cmd/vendor/github.com/google/pprof/third_party/d3/README.md
./src/cmd/vendor/github.com/ianlancetaylor/demangle/README.md
./src/cmd/vendor/golang.org/x/sys/unix/README.md
./test/README.md
./readme.md
[root@localhost go]#

As you can see, the lowercase file readme.md has also been found. This is the difference between -name and -iname.

PS: In Linux, the i parameter often means that the command is not case sensitive.

3.2 -type parameter

Find all directories named "*src". 

[root@localhost go]# find . -type d -name "*src"
./src
./src/cmd/api/testdata/src
./src/cmd/go/testdata/modlegacy/src
./src/cmd/internal/src
./src/cmd/link/testdata/testPErsrc
./src/go/build/testdata/withvendor/src
[root@localhost go]#

As you can see from the above example, all directories that meet the pattern "*src" are displayed, and no ordinary files that meet the conditions are listed. 

 Find all files named "*src".

[root@localhost go]# find . -type f -name "*src"
./src/cmd/compile/internal/syntax/testdata/issue20789.src
./src/cmd/compile/internal/syntax/testdata/issue23385.src
./src/cmd/compile/internal/syntax/testdata/issue23434.src
./src/cmd/compile/internal/syntax/testdata/issue31092.src
./src/cmd/compile/internal/syntax/testdata/sample.src
./src/go/parser/testdata/commas.src
./src/go/parser/testdata/issue11377.src
./src/go/parser/testdata/issue23434.src
……

As can be seen from the above example, all ordinary files that match the pattern "*src" have been found, not including directories. 

3.3 -ctime parameter

 First, create the testCtime file under /var/account, and then search for the files modified in the last day in the current directory.

[root@localhost account]# ls
pacct
[root@localhost account]# touch testCtime
[root@localhost account]# find . -ctime -1
.
./testCtime
[root@localhost account]#

As you can see from the above, both "." and testCtime have been found, where "." represents the current directory.

3.4 -cnewer parameter

First, create two files testOne and testTwo, and then create a directory testdir to find newer files than testOne.

[root@localhost go]# touch testOne
[root@localhost go]# touch testTwo
[root@localhost go]# mkdir testdir
[root@localhost go]# find . -cnewer testOne
.
./testTwo
./testdir
[root@localhost go]#

As can be seen from the above query results, files that are newer than testOne can be queried .

3.5 -exec/-ok parameter

Search for files matching the pattern "README.md" in the current directory and subdirectories, and execute the command ls -l for the results. 

[root@localhost go]# find . -name "README.md" -exec ls -l {} \;
-rw-r--r--. 1 root root 1607 9月  10 00:57 ./README.md
-rw-r--r--. 1 root root 5066 9月  10 00:57 ./misc/trace/README.md
-rw-r--r--. 1 root root 5655 9月  10 00:57 ./src/cmd/compile/README.md
-rw-r--r--. 1 root root 7945 9月  10 00:57 ./src/cmd/compile/internal/ssa/README.md
-rw-r--r--. 1 root root 2834 9月  10 00:57 ./src/cmd/vendor/github.com/google/pprof/third_party/d3/README.md
-rw-r--r--. 1 root root 98 9月  10 00:57 ./src/cmd/vendor/github.com/ianlancetaylor/demangle/README.md
-rw-r--r--. 1 root root 8678 9月  10 00:57 ./src/cmd/vendor/golang.org/x/sys/unix/README.md
-rw-r--r--. 1 root root 577 9月  10 00:57 ./test/README.md
[root@localhost go]# 

Same as above, change the parameter from -exec to -ok, and execute as follows: 

[root@localhost go]# find . -name "README.md" -ok ls -l {} \;
< ls ... ./README.md > ? y
-rw-r--r--. 1 root root 1607 9月  10 00:57 ./README.md
< ls ... ./misc/trace/README.md > ? y
-rw-r--r--. 1 root root 5066 9月  10 00:57 ./misc/trace/README.md
< ls ... ./src/cmd/compile/README.md > ? y
-rw-r--r--. 1 root root 5655 9月  10 00:57 ./src/cmd/compile/README.md
< ls ... ./src/cmd/compile/internal/ssa/README.md > ? y
-rw-r--r--. 1 root root 7945 9月  10 00:57 ./src/cmd/compile/internal/ssa/README.md
< ls ... ./src/cmd/vendor/github.com/google/pprof/third_party/d3/README.md > ? y
-rw-r--r--. 1 root root 2834 9月  10 00:57 ./src/cmd/vendor/github.com/google/pprof/third_party/d3/README.md
< ls ... ./src/cmd/vendor/github.com/ianlancetaylor/demangle/README.md > ? y
-rw-r--r--. 1 root root 98 9月  10 00:57 ./src/cmd/vendor/github.com/ianlancetaylor/demangle/README.md
< ls ... ./src/cmd/vendor/golang.org/x/sys/unix/README.md > ? N
< ls ... ./test/README.md > ? N
[root@localhost go]#

As you can see from the above example, every time you execute the ls -l command, you will be asked whether to execute it. 

 Four, summary

The common parameters of find are listed above. It should be noted that the search of find is recursive search. For example, if you search in the current directory, it will find files in the current directory and subdirectories by default.

Five, references

[1] https://www.runoob.com/linux/linux-comm-find.html

[2] https://man7.org/linux/man-pages/man1/find.1.html

 

Guess you like

Origin blog.csdn.net/u011074149/article/details/112093819