In Linux, the find command is used to find files in a specified directory. The basic syntax is "find path -option..". Any string before the parameter will be regarded as the directory name to be found; if you use this command without setting any parameters, the find command will search for subdirectories and files in the current directory.
#The operating environment of this tutorial: Red Hat Enterprise Linux 6.1 system, Dell G3 computer.
Linux find command is used to find files in the specified directory.
find command format:
find path -option 【 -print 】 【 -exec -ok |xargs |grep 】 【 command {} \; 】
find command parameters:
1)path: The directory path to be searched.
- ???????? ~ represents the $HOME directory
- ???? . represents the current directory
- ???????? / represents the root directory
2 ) print: Indicates outputting the results to standard output.
3) exec: Execute the shell command given by this parameter on the matching file.
The form is command {} \;, Note that there is a space between {} and \;
4) ok: has the same effect as exec ,
The difference is that before executing the command, a prompt will be given to let the user confirm whether to execute it
5)|xargs It has the same function as exec and serves as a successor
The difference is that |xargs is mainly used to undertake deletion operations, while -exec can be used for copying, moving, renaming, etc.
6)options : Indicates the search method
options Commonly used options include the following options:
-name filename #查找名為filename的文件 -perm #按執(zhí)行權限來查找 -user username #按文件屬主來查找 -group groupname #按組來查找 -mtime -n +n #按文件 更改時間 來查找文件,-n指n天以內,+n指n天以前 -atime -n +n #按文件 訪問時間 來查找文件,-n指n天以內,+n指n天以前 -ctime -n +n #按文件 創(chuàng)建時間 來查找文件,-n指n天以內,+n指n天以前 -nogroup #查無有效屬組的文件,即文件的屬組在/etc/groups中不存在 -nouser #查無有效屬主的文件,即文件的屬主在/etc/passwd中不存 -type b/d/c/p/l/f #查是塊設備、目錄、字符設備、管道、符號鏈接、普通文件 -size n[c] #查長度為n塊[或n字節(jié)]的文件 -mount #查文件時不跨越文件系統(tǒng)mount點 -follow #如果遇到符號鏈接文件,就跟蹤鏈接所指的文件 -prune #忽略某個目錄
Any string before the parameter will be regarded as the directory name to be searched. If you use this command without setting any parameters, the find command will search for subdirectories and files in the current directory. And all found subdirectories and files will be displayed.
Here are some simple examples to introduce the common usage of find:
1. Search by name
In the current directory and subdirectories, search for uppercase letters txt files starting with letters
##
$ find . -name '[A-Z]*.txt' -printIn /etc and its subdirectories, search for files starting with host
$ find /etc -name 'host*' -printIn the $HOME directory and its subdirectories, search for all files
$ find ~ -name '*' -printIn the current directory and subdirectories, search for txt files that do not start with out
$ find . -name "out*" -prune -o -name "*.txt" -print2. Search by directory Search for txt files in subdirectories of the current directory except aa
$ find . -path "./aa" -prune -o -name "*.txt" -printIn the current directory and Search for txt files in subdirectories other than aa and bb
$ find . \( -path './dir0' -o -path './dir1' \) -a -prune -o -name '*.txt' -print
Note: Required in both 1 and 2 Add spaces, otherwise an error will appear as shown in the picture
You can add -a in the 3 places without adding -a In the current directory, no longer in the subdirectory, Find txt files
$ find . ! -name "." -type d -prune -o -type f -name "*.txt" -print
or
find . -name *.txt -type f -printFriendly link: Detailed explanation of find command -path -prune usage in Linux3. Search by permissions In the current directory and subdirectories, search for files whose owner has read-write execution permissions and other files with read-write execution permissions
$find . -perm 755 -printFind files where the user has write permissions Permissions or files or directories that group users have write permissions
find ./ -perm /220 find ./ -perm /u+w,g+w find ./ -perm /u=w,g=w4. Search by type (b/d/c/p/l/f) In the current directory and sub-directories Directory, search for symbolic link files
$ find . -type l -print5. By owner and group
Search for files whose owner is www
$ find / -user www -type f -printSearch for attributes Main deleted files
$ find / -nouser -type f -printFind files belonging to group mysql
$ find / -group mysql -type f -printFind deleted files of user group
$ find / -nogroup -type f -print
6. Search by time
Find files that have been changed within 2 days
$ find . -mtime -2 -type f -printFind files that have been changed 2 days ago
$ find . -mtime +2 -type f -printFind files that have been accessed within one day
$ find . -atime -1 -type f -printFind files that have been accessed one day ago
$ find . -atime +1 -type f -printFind files whose status has been changed within one day
$ find . -ctime -1 -type f -printFind Files whose status was changed one day ago
$ find . -ctime +1 -type f -print
查找10分鐘以前狀態(tài)被改變的文件
$ find . -cmin +10 -type f -print
7、按文件新舊
查找比 aa.txt 新的文件
$ find . -newer "aa.txt" -type f -print
查找比 aa.txt 舊的文件
$ find . ! -newer "aa.txt" -type f -print
查找比aa.txt新,比bb.txt舊的文件
$ find . -newer 'aa.txt' ! -newer 'bb.txt' -type f -print
8、按大小查找
查找超過1M的文件
$ find / -size +1M -type f -print
查找等于6字節(jié)的文件
$ find . -size 6c -print
查找小于32k的文件
$ find . -size -32k -print
9、執(zhí)行命令
1)查找 del.txt 并刪除,刪除前提示確認
$ find . -name 'del.txt' -ok rm {} \;
2) 查找 aa.txt 并備份為aa.txt.bak
$ find . -name 'aa.txt' -exec cp {} {}.bak \;
3)查當前目錄下的所有普通文件
# find . -type f -exec ls -l {} \; -rw-r–r– 1 root root 34928 2003-02-25 ./conf/httpd.conf -rw-r–r– 1 root root 12959 2003-02-25 ./conf/magic -rw-r–r– 1 root root 180 2003-02-25 ./conf.d/README
查當前目錄下的所有普通文件,并在 - exec 選項中使用 ls -l 命令將它們列出
4)在 /logs 目錄中查找更改時間在5日以前的文件并刪除它們
$ find logs -type f -mtime +5 -exec -ok rm {} \;
5)查詢當天修改過的文件
# find ./ -mtime -1 -type f -exec ls -l {} \;
6)查詢文件并詢問是否要顯示
# find ./ -mtime -1 -type f -ok ls -l {} \; < ls … ./classDB.inc.php > ? y -rw-r–r– 1 cnscn cnscn 13709 1月 12 12:22 ./classDB.inc.php # find ./ -mtime -1 -type f -ok ls -l {} \; < ls … ./classDB.inc.php > ? n
關于 有沒有 -print 的區(qū)別
加 -print
查找目錄并列出目錄下的文件(為找到的每一個目錄單獨執(zhí)行l(wèi)s命令,沒有選項-print時文件列表前一行不會顯示目錄名稱)
find /home -type d -print -exec ls {} \;
不加 -print
相關推薦:《Linux視頻教程》
The above is the detailed content of How to use linux find command. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Integrating Postman applications on CentOS can be achieved through a variety of methods. The following are the detailed steps and suggestions: Install Postman by downloading the installation package to download Postman's Linux version installation package: Visit Postman's official website and select the version suitable for Linux to download. Unzip the installation package: Use the following command to unzip the installation package to the specified directory, for example /opt: sudotar-xzfpostman-linux-x64-xx.xx.xx.tar.gz-C/opt Please note that "postman-linux-x64-xx.xx.xx.tar.gz" is replaced by the file name you actually downloaded. Create symbols

[Common Directory Description] Directory/bin stores binary executable files (ls, cat, mkdir, etc.), and common commands are generally here. /etc stores system management and configuration files/home stores all user files. The root directory of the user's home directory is the basis of the user's home directory. For example, the home directory of the user user is /home/user. You can use ~user to represent /usr to store system applications. The more important directory /usr/local Local system administrator software installation directory (install system-level applications). This is the largest directory, and almost all the applications and files to be used are in this directory. /usr/x11r6?Directory for storing x?window/usr/bin?Many

Setting the location of the interpreter in PyCharm can be achieved through the following steps: 1. Open PyCharm, click the "File" menu, and select "Settings" or "Preferences". 2. Find and click "Project:[Your Project Name]" and select "PythonInterpreter". 3. Click "AddInterpreter", select "SystemInterpreter", browse to the Python installation directory, select the Python executable file, and click "OK". When setting up the interpreter, you need to pay attention to path correctness, version compatibility and the use of the virtual environment to ensure the smooth operation of the project.

The main difference between Java and other programming languages ??is its cross-platform feature of "writing at once, running everywhere". 1. The syntax of Java is close to C, but it removes pointer operations that are prone to errors, making it suitable for large enterprise applications. 2. Compared with Python, Java has more advantages in performance and large-scale data processing. The cross-platform advantage of Java stems from the Java virtual machine (JVM), which can run the same bytecode on different platforms, simplifying development and deployment, but be careful to avoid using platform-specific APIs to maintain cross-platformity.

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

The installation and configuration of MySQL can be completed through the following steps: 1. Download the installation package suitable for the operating system from the official website. 2. Run the installer, select the "Developer Default" option and set the root user password. 3. After installation, configure environment variables to ensure that the bin directory of MySQL is in PATH. 4. When creating a user, follow the principle of minimum permissions and set a strong password. 5. Adjust the innodb_buffer_pool_size and max_connections parameters when optimizing performance. 6. Back up the database regularly and optimize query statements to improve performance.

I have a lot of experience in participating in VSCode offline technology exchange activities, and my main gains include sharing of plug-in development, practical demonstrations and communication with other developers. 1. Sharing of plug-in development: I learned how to use VSCode's plug-in API to improve development efficiency, such as automatic formatting and static analysis plug-ins. 2. Practical demonstration: I learned how to use VSCode for remote development and realized its flexibility and scalability. 3. Communicate with developers: I have obtained skills to optimize VSCode startup speed, such as reducing the number of plug-ins loaded at startup and managing the plug-in loading order. In short, this event has benefited me a lot and I highly recommend those who are interested in VSCode to participate.

Informix and MySQL are both popular relational database management systems. They perform well in Linux environments and are widely used. The following is a comparison and analysis of the two on the Linux platform: Installing and configuring Informix: Deploying Informix on Linux requires downloading the corresponding installation files, and then completing the installation and configuration process according to the official documentation. MySQL: The installation process of MySQL is relatively simple, and can be easily installed through system package management tools (such as apt or yum), and there are a large number of tutorials and community support on the network for reference. Performance Informix: Informix has excellent performance and
