Use find for precise, real-time searches by name, size, type, or modification time; 2. Use locate for fast filename searches with an updated database; 3. Use grep -r to search file contents recursively; 4. Use which to find executable paths in $PATH; 5. Use whereis to locate binaries, configuration files, and man pages. The right tool depends on whether speed, precision, or content-based search is needed, and all commands together cover most file-finding scenarios in CentOS.
Finding a file in CentOS is a common task, especially when managing servers or troubleshooting. Here are the most effective methods to locate files, depending on what you know about the file (name, content, type, etc.).

1. Using find
– Most Powerful and Flexible
The find
command searches the filesystem in real time and supports many options.
Basic syntax:

find [path] [expression]
Examples:
Find by filename (exact or partial):
find / -type f -name "filename.txt"
Use
*
for wildcards:find /home -type f -name "*.conf"
Case-insensitive search:
find /etc -type f -iname "nginx*"
Find by file type:
find /var -type d -name "log" # directories named "log" find / -type l # symbolic links
Find by size:
find /tmp -type f -size 100M # larger than 100MB
Find by modification time:
find /home -type f -mtime -7 # modified in the last 7 days
?? Run as root or use
sudo
to avoid permission errors when searching system directories.
2. Using locate
– Fast but Requires Updated Database
locate
is much faster than find
because it uses a pre-built database (/var/lib/mlocate/mlocate.db
), but the database must be up to date.
Install mlocate
(if not already installed):
sudo yum install mlocate
Update the database:
sudo updatedb
Search for a file:
locate filename.txt
- Case-insensitive partial match:
locate nginx
? Best for quick searches when you know part of the filename.
? Remember to runupdatedb
after new files are created.
3. Search by File Content
If you don’t know the filename but know some text inside it:
grep -r "search term" /path/to/search/
Example:
grep -r "Listen 80" /etc/httpd/
Use
--include
to limit file types:grep -r --include="*.conf" "error_log" /etc/httpd/
Add
-n
to show line numbers:grep -rn "timeout" /etc/
4. Using which
and whereis
– For Executables and Binaries
which
– Shows the path of executables in your$PATH
:which python3 # Output: /usr/bin/python3
whereis
– Locates binaries, source, and manual pages:whereis nginx # Output: nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
These are useful for finding installed programs quickly.
Summary: Which Tool to Use?
Need Command Search by name, size, time, etc. find
Quick filename search (updated db) locate
Search inside files grep -r
Find executable in PATH which
Find binary, config, and man pages whereis
For most day-to-day tasks,
find
andlocate
cover the majority of use cases. Usefind
when you need precision, andlocate
when speed matters and the file likely existed before the lastupdatedb
.Basically, it's about knowing what you're looking for — and picking the right tool.
The above is the detailed content of How to find a file in CentOS. 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 migrate from CentOS8 to AlmaLinux or RockyLinux, follow the clear steps. First, choose AlmaLinux (suitable for long-term enterprise support) or RockyLinux (emphasizing exactly the same as RHEL) according to your needs. Secondly, prepare the system environment: update the software package, back up key data, check third-party repositories and disk space. Then, the conversion is automatically completed using the official migration script. RockyLinux needs to clone the repository and run the switch-to-rocky.sh script. AlmaLinux replaces the repository and upgrades with one click through the remote deployment script. Finally, verify system information, clean up residual packets, and update GRUB and ini if ??necessary

To correctly install the local RPM file and handle dependencies, you should first use dnf to install it directly, because it can automatically obtain the required dependencies from the configured repository; if the system does not support dnf, you can use yum's localinstall command instead; if the dependency cannot be resolved, you can manually download and install all related packages; finally, you can also forcefully ignore the dependency installation, but this method is not recommended. 1. Use sudodnfinstall./package-name.rpm to automatically resolve dependencies; 2. If there is no dnf, you can use sudoyumlocalinstall./package-name.rpm; 3. Force installation and execute sudorpm-ivh--nod

AminimalinstallofCentOSisalightweightsetupthatincludesonlyessentialcomponents,makingitidealforserversorsystemsrequiringfullcontrol.Itcontainscoreutilitieslikebash,yum/dnf,networkingtools,andsecuritypackages,whileexcludingdesktopenvironments,webserver

How to set a static IP address using nmcli on CentOS8 or 9? 1. First run the nmcliconnectionshow and ipa commands to view the current network interface and its configuration; 2. Use the nmcliconnectionmodify command to modify the connection configuration, specify parameters such as ipv4.methodmanual, ipv4.addresses (such as 192.168.1.100/24), ipv4.gateway (such as 192.168.1.1), and ipv4.dns (such as 8.8.8.8). 3. Run the nmcliconnectiondown and up commands to restart the connection to make the changes take effect, or

Installing and configuring fail2ban on CentOS is not complicated, it mainly includes the following steps: 1. Install fail2ban using yum; 2. Manually enable and start the service; 3. Create a jail.local file for custom configuration; 4. Set SSH defense rules, including enabling sshd, specifying the blocking time and retry times; 5. Configure firewalld as an action actuator; 6. Regularly check the blocking IP and logs. Fail2ban detects abnormal login behavior through monitoring logs and automatically blocks suspicious IPs. Its core mechanism relies on key parameters such as bantime (banned time), findtime (statistic window time) and maxretry (maximum failure number).

KernelCare and kpatch are both tools for implementing hot patches in the Linux kernel, but the applicable scenarios are different. 1. KernelCare is a commercial service that supports CentOS, RHEL, Ubuntu and Debian, automatically applies patches without restarting, and is suitable for hosting service providers and enterprise production environments; 2. kpatch is an open source tool developed by Red Hat. It is based on the ftrace framework and requires manual construction of patch modules. It is suitable for RHEL and compatible systems, and is suitable for organizations that need to finely control the patch process or use customized kernels. When choosing, automation requirements, system distribution, whether official support is required, and the degree of control over open source tools should be considered. Neither of them can fix all vulnerabilities, some still need to be restarted, and

How to add or remove a service in FirewallD? 1. Add a service: First use firewall-cmd-get-services to view available services, temporarily add --add-service=service name, and permanently add --permanent parameter; 2. Remove service: Use --remove-service=service name to temporarily remove, add --permanent permanently remove, and after modification, all need to perform --reload reload configuration; 3. Custom service: Use --new-service to create a service and edit the XML file to define the port, and then add it according to the standard service. Pay attention to distinguish between temporary and permanent settings during operation, and reload the firewall in time.

The method of installing MariaDB or MySQL to CentOS is as follows: 1. Install MariaDB: After updating the system, use yum to install mariadb-server, start the service and run the security initialization script; 2. Install MySQL: After adding the official source, then use yum to install mysql-community-server, start the service and view the log to get the temporary password, and then run the security initialization script. MariaDB is the default recommended option, suitable for development and testing environments; MySQL is suitable for scenarios with specific enterprise needs, the community version has limited functions, and the enterprise version requires a fee. Frequently asked questions include port conflicts, permission issues, and database status checks, which can be accessed through open firewall ports.
