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í)行權(quán)限來查找 -user username #按文件屬主來查找 -group groupname #按組來查找 -mtime -n +n #按文件 更改時(shí)間 來查找文件,-n指n天以內(nèi),+n指n天以前 -atime -n +n #按文件 訪問時(shí)間 來查找文件,-n指n天以內(nèi),+n指n天以前 -ctime -n +n #按文件 創(chuàng)建時(shí)間 來查找文件,-n指n天以內(nèi),+n指n天以前 -nogroup #查無有效屬組的文件,即文件的屬組在/etc/groups中不存在 -nouser #查無有效屬主的文件,即文件的屬主在/etc/passwd中不存 -type b/d/c/p/l/f #查是塊設(shè)備、目錄、字符設(shè)備、管道、符號(hào)鏈接、普通文件 -size n[c] #查長度為n塊[或n字節(jié)]的文件 -mount #查文件時(shí)不跨越文件系統(tǒng)mount點(diǎn) -follow #如果遇到符號(hào)鏈接文件,就跟蹤鏈接所指的文件 -prune #忽略某個(gè)目錄
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 并刪除,刪除前提示確認(rèn)
$ find . -name 'del.txt' -ok rm {} \;
2) 查找 aa.txt 并備份為aa.txt.bak
$ find . -name 'aa.txt' -exec cp {} {}.bak \;
3)查當(dāng)前目錄下的所有普通文件
# 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
查當(dāng)前目錄下的所有普通文件,并在 - exec 選項(xiàng)中使用 ls -l 命令將它們列出
4)在 /logs 目錄中查找更改時(shí)間在5日以前的文件并刪除它們
$ find logs -type f -mtime +5 -exec -ok rm {} \;
5)查詢當(dāng)天修改過的文件
# 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
關(guān)于 有沒有 -print 的區(qū)別
加 -print
查找目錄并列出目錄下的文件(為找到的每一個(gè)目錄單獨(dú)執(zhí)行l(wèi)s命令,沒有選項(xiàng)-print時(shí)文件列表前一行不會(huì)顯示目錄名稱)
find /home -type d -print -exec ls {} \;
不加 -print
相關(guān)推薦:《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)

To tune MySQL into a Chinese interface, it can be implemented through MySQLWorkbench or command line tools. 1) In MySQLWorkbench, open "Preferences", select the "Appearance" tab, and then select "Chinese(Simplified)" in the "Language" drop-down menu, and restart. 2) When using command line tools, set the operating system locale variables, such as using "exportLANG=zh_CN.UTF-8" on Linux or macOS, and then run the mysql client.

Linux and Windows have their own advantages and disadvantages in CPU and memory usage: 1) Linux uses time slice-based scheduling algorithms to ensure fairness and efficiency; Windows uses priority scheduling, which may cause low-priority processes to wait. 2) Linux manages memory through paging and switching mechanisms to reduce fragmentation; Windows tends to pre-allocate and dynamic adjustment, and efficiency may fluctuate.

Linux's cost of ownership is usually lower than Windows. 1) Linux does not require license fees, saving a lot of costs, while Windows requires purchasing a license. 2) Linux has low hardware requirements and can extend the service life of the device. 3) The Linux community provides free support to reduce maintenance costs. 4) Linux is highly secure and reduces productivity losses. 5) The Linux learning curve is steep, but Windows is easier to use. The choice should be based on specific needs and budget.

LinuxoftenoutperformsWindowsinI/Operformanceduetoitscustomizablekernelandfilesystems,whileWindowsoffersmoreuniformperformanceacrosshardware.1)LinuxexcelswithcustomizableI/OschedulerslikeCFQandDeadline,enhancingperformanceinhigh-throughputapplications

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

The key to enabling EPEL repository is to select the correct installation method according to the system version. First, confirm the system type and version, and use the command cat/etc/os-release to obtain information; second, enable EPEL through dnfinstallepel-release on CentOS/RockyLinux, and the 8 and 9 version commands are the same; third, you need to manually download the corresponding version of the .repo file and install it on RHEL; fourth, you can re-import the GPG key when encountering problems. Note that the old version may not be supported, and you can also consider enabling epel-next to obtain the test package. After completing the above steps, use dnfrepolist to verify that the EPEL repository is successfully added.

Linux usually performs better in web server performance, mainly due to its advantages in kernel optimization, resource management and open source ecosystem. 1) After years of optimization of the Linux kernel, mechanisms such as epoll and kqueue make it more efficient in handling high concurrent requests. 2) Linux provides fine-grained resource management tools such as cgroups. 3) The open source community continuously optimizes Linux performance, and many high-performance web servers such as Nginx are developed on Linux. By contrast, Windows performs well when handling ASP.NET applications and provides better development tools and commercial support.

Newbie users should first clarify their usage requirements when choosing a Linux distribution. 1. Choose Ubuntu or LinuxMint for daily use; programming and development are suitable for Manjaro or Fedora; use Lubuntu and other lightweight systems for old devices; recommend CentOSStream or Debian to learn the underlying principles. 2. Stability is preferred for UbuntuLTS or Debian; you can choose Arch or Manjaro to pursue new features. 3. In terms of community support, Ubuntu and LinuxMint are rich in resources, and Arch documents are technically oriented. 4. In terms of installation difficulty, Ubuntu and LinuxMint are relatively simple, and Arch is suitable for those with basic needs. It is recommended to try it first and then decide.
