search

The find command is one of my favorite commands. It can easily find the files I want to find. It can support too many methods to find, such as file name, file size, file type, etc. wait. Next, let’s take a look.

Syntax: find [search path] [options] [action]

Don’t have too many options in the find command. Today I will only talk about some frequently used options.

Search by file name

The option to search by file name is -name, which supports simple regular search.

For example, I know that locale.conf is saved in the /etc directory, but I have forgotten the specific path, then

# find /etc/ -name locale.conf/etc/locale.conf

I can find out the specific path of the file.

Now I want to know how many php files there are in a certain directory, then I can use the following command to complete it.

# find default -name *.php | wc -l122

Or option -o

Here is another option, -o, which means or, generally the default is between find options They all mean "and". Let's look at an example to find the total number of php or js files in a certain directory.

# find default -name *.php -o -name *.js | wc -l225

Search based on file type and directory depth

You need to use the -type option to find file types. Commonly used types include f (file), d(directory). Another option is introduced, -maxdepth indicates the maximum number of recursive directories.

# find ./  -maxdepth 1  -type d././default./default.bak

Negative option!

# find ./  -maxdepth 1 ! -type f././default./default.bak

Time-related lookup

Time-related options: There are -atime, -ctime and -mtime , using -mtime description
-mtime n n as a number, meaning files whose contents were changed "within one day" n days ago;
-mtime n List the file names whose contents were changed n days ago (excluding n days itself)
-mtime -n List the file names of files whose contents have been changed within n days (including n days themselves)
- newer file file is an existing file and lists the file names that are newer than file

  这个选项很有作用,比如进行数据定时备份时,只保留最近7天的数据,超过7天的自动删除就会用到该选项。注意+n表示n天之前,-n表示n天之内。

# find $bakdir -name "*.sql.bz2" -type f -mtime +7 -exec rm -rf {} \;

根据用户名、组来查找

与使用者或群组名称有关
-uid n   n 为数字,这个数字是使用者的帐号ID,亦即UID    
-gid n  n 为数字,这个数字是群组名称的ID,亦即GID    
-user name  name 为使用者帐号名称,例如dmtsai   
-group name  name 为群组名称,例如users ;    
-nouser 寻找文件的拥有者不存在 的人!    
-nogroup  寻找文件的拥有群组不存在于/etc/group 的文件!    

  查找某目录下,所有者不是www的文件有哪些。

find /home/wwwroot/default ! -user www | wc -l

根据文件大小查找

按文件大小查找使用-size选项,比如查找大于1M的文件,那么使用-size +1M,如果查找小于1K的,那么使用-size -1K

# find /home/wwwroot/default -size +1M
# find /home/wwwroot/default -size -1k

根据文件权限查找

  • -perm mode 搜寻文件权限『刚好等于』 mode 的文件,这个mode 为类似chmod的属性值,举例来说, -rwsr-xr-x 的属性为4755 !

  • -perm -mode    搜寻文件权限『必须要全部囊括mode 的权限』的文件,举例来说,我们要搜寻-rwxr--r-- ,亦即0744 的文件,使用-perm -0744,当一个文件的权限为-rwsr-xr-x ,亦即4755 时,也会被列出来,因为-rwsr-xr-x 的属性已经囊括了-rwxr--r-- 的属性了。    

  • -perm /mode    搜寻文件权限『包含任一mode 的权限』的文件,举例来说,我们搜寻-rwxr-xr-x ,亦即-perm /755 时,但一个文件属性为-rw-------也会被列出来,因为他有-rw.... 的属性存在!    

我们知道,文件的权限一般为644,目录的权限一般为755。如果,不是等于这个权限,可能就会有点问题,那么我们来找找看,是否有这类文件。

find /home/wwwroot/default ! -perm 644 -type d -exec ls -ld {} \;
# 查找权限不是644的文件,并将其修改为644
find /home/wwwroot/default ! -perm 644 -type f | xargs -n 10 chmod 644;

动作执行

其实这个命令上面已经使用到了,使用-exec选项,然后接命令,最后要以{} ;结尾,比如

find /home/wwwroot/default ! -perm 644 -type d -exec ls -ld {} \;

其他

find还支持正则(-regex)查找文件名,还可以不区分大小写(-iregex);

使用-empty可以查找文件大小为0的文件。

# find . -empty -exec ls -l {} \;

The above is the detailed content of File search command find in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use regular expressions (regex) in Linux for pattern matching?How do I use regular expressions (regex) in Linux for pattern matching?Mar 17, 2025 pm 05:25 PM

The article explains how to use regular expressions (regex) in Linux for pattern matching, file searching, and text manipulation, detailing syntax, commands, and tools like grep, sed, and awk.

How do I monitor system performance in Linux using tools like top, htop, and vmstat?How do I monitor system performance in Linux using tools like top, htop, and vmstat?Mar 17, 2025 pm 05:28 PM

The article discusses using top, htop, and vmstat for monitoring Linux system performance, detailing their unique features and customization options for effective system management.

How do I implement two-factor authentication (2FA) for SSH in Linux?How do I implement two-factor authentication (2FA) for SSH in Linux?Mar 17, 2025 pm 05:31 PM

The article provides a guide on setting up two-factor authentication (2FA) for SSH on Linux using Google Authenticator, detailing installation, configuration, and troubleshooting steps. It highlights the security benefits of 2FA, such as enhanced sec

How do I configure SELinux or AppArmor to enhance security in Linux?How do I configure SELinux or AppArmor to enhance security in Linux?Mar 12, 2025 pm 06:59 PM

This article compares SELinux and AppArmor, Linux kernel security modules providing mandatory access control. It details their configuration, highlighting the differences in approach (policy-based vs. profile-based) and potential performance impacts

How do I back up and restore a Linux system?How do I back up and restore a Linux system?Mar 12, 2025 pm 07:01 PM

This article details Linux system backup and restoration methods. It compares full system image backups with incremental backups, discusses optimal backup strategies (regularity, multiple locations, versioning, testing, security, rotation), and da

How do I set up a firewall in Linux using firewalld or iptables?How do I set up a firewall in Linux using firewalld or iptables?Mar 12, 2025 pm 06:58 PM

This article compares Linux firewall configuration using firewalld and iptables. Firewalld offers a user-friendly interface for managing zones and services, while iptables provides low-level control via command-line manipulation of the netfilter fra

How do I use sudo to grant elevated privileges to users in Linux?How do I use sudo to grant elevated privileges to users in Linux?Mar 17, 2025 pm 05:32 PM

The article explains how to manage sudo privileges in Linux, including granting, revoking, and best practices for security. Key focus is on editing /etc/sudoers safely and limiting access.Character count: 159

How do I manage software packages in Linux using package managers (apt, yum, dnf)?How do I manage software packages in Linux using package managers (apt, yum, dnf)?Mar 17, 2025 pm 05:26 PM

Article discusses managing software packages in Linux using apt, yum, and dnf, covering installation, updates, and removals. It compares their functionalities and suitability for different distributions.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.