Preface: This article is the content of the basic Linux operation chapter!
The environment I use is based on Tencent Cloud Server: CentOS 7.6 64bit.
Study set:
Directory index:
1. Basic syntax format and functions
2. Basic usage in file search
- - 2.1 Create test cases
- - 2.2 "-name": Search for files according to file names [Key points]
- - 2.3 Find the specified file in the current directory/ Directory
- - 2.4 Find the specified file/directory in the specified directory
3. Instructions on find
4. Recommended related articles or series
1. Basic syntax format and functions
Basic syntax: find pathname [option]
Function: Used to search for files in the file tree and process them accordingly (possibly accessing the disk)
Updated addition: The find command and the grep command are often mentioned in interviews, and you need to answer the similarities and differences!
2. Basic usage in file search
2.1 Create test cases
/* 指令测试准备 */
[Mortal@VM-12-16-centos test_findsome]$ mkdir -p a/aa
[Mortal@VM-12-16-centos test_findsome]$ mkdir -p a/bb
[Mortal@VM-12-16-centos test_findsome]$ mkdir -p a/cc
[Mortal@VM-12-16-centos test_findsome]$ mkdir -p b/aa
[Mortal@VM-12-16-centos test_findsome]$ touch a/aa/t1.txt
[Mortal@VM-12-16-centos test_findsome]$ touch a/aa/t2.txt
[Mortal@VM-12-16-centos test_findsome]$ touch a/cc/t2.txt
[Mortal@VM-12-16-centos test_findsome]$ touch b/aa/t2.txt
[Mortal@VM-12-16-centos test_findsome]$ tree .
.
|-- a
| |-- aa
| | |-- t1.txt
| | `-- t2.txt
| |-- bb
| `-- cc
| `-- t2.txt
`-- b
`-- aa
`-- t2.txt
6 directories, 4 files
2.2 "-name": Search files according to file names [Key points]
用法:find pathname -name filename
- [ That is: find specifies the path-name specifies the file name ]
Note: The speed may be slow during the first query, but the speed of subsequent queries will improve! [It can be understood as the first traversal search, and the disk may be accessed during the process]
2.3 Find the specified file/directory in the current directory
Find the specified file/directory in the current directory
/* 查找当前目录下指定文件 */
[Mortal@VM-12-16-centos test_findsome]$ find . -name t2.txt
./b/aa/t2.txt
./a/cc/t2.txt
./a/aa/t2.txt
/* 查找当前目录下指定目录 */
[Mortal@VM-12-16-centos test_findsome]$ find . -name aa
./b/aa
./a/aa
2.4 Find the specified file/directory in the specified directory
Find the specified file/directory in the specified directory
[Mortal@VM-12-16-centos test_findsome]$ find ./a -name t2.txt
./a/cc/t2.txt
./a/aa/t2.txt
[Mortal@VM-12-16-centos test_findsome]$ find ./a -name t1.txt
./a/aa/t1.txt
/* 指定查找当前用户目录下的指定文件/目录 */
[Mortal@VM-12-16-centos test_findsome]$ ls ~
StudyingOrder_Linux test1 test2 test3 test_cp test_findsome test_mkdir test_mv test_txtfile
[Mortal@VM-12-16-centos test_findsome]$ find ~ -name test_findsome
/home/Mortal/test_findsome
3. Instructions about find
- The find command under Linux searches for files in the directory structure and performs the specified operation.
- The find command under Linux provides quite a few search conditions and is very powerful. Because find has powerful functions, it also has many options, most of which are worth our time to understand. ( Since the current knowledge system is simple and does not involve knowledge such as file systems, the content of this article will be continuously updated on the use of find during continuous learning. Currently, it is only a simple search understanding! )
The following instructions are just for a temporary meeting!
- Even if the system contains a Network File System (NFS), the find command is also valid in the file system, as long as you have the corresponding permissions.
- When running a very resource-consuming find command, many people tend to execute it in the background, because traversing a large file system may take a long time (here refers to a file system of more than 30G bytes).
4. Recommendations for related articles or series
1. Linux learning directory collection ;
2. Linux :: [Basic commands:: Search/query command: (1)]:: which command: Specify system file (command) search command | Alias of query command 3. Linux:: Content filtering command [3]
: grep command [Detailed explanation]: Filter search information in the specified file and (fuzzy) find content containing the specified string!