KB-630208 | How to use Find and Locate to search for files in Linux

KB-630208 | How to use Find and Locate to search for files in Linux

Purpose

To provide Linux administrators and Field Engineers with guidance on using the find and locate utilities to efficiently search for files and directories within a Linux filesystem based on criteria such as name, type, size, timestamps, ownership, permissions, and directory depth.

Scope

This article covers the basic usage of the find and locate commands in Linux, including installation and database updates for locate, searching by file name and type, filtering by size and time, searching by owner and permissions, controlling search depth, combining search criteria, and executing commands on files returned by find. It is intended for routine file-search and filesystem administration tasks on Linux systems.


How to use Find and Locate to search for files in Linux

This article introduces two useful tools for searching files within the Linux filesystem. The first is the find command, which is available by default in virtually all Linux distributions. The second is the locate command, which must be installed separately before it can be used.

Find command

The find command is part of Linux by default. It offers multiple parameters to refine your search.

Find by name

The simplest searching command is to search by name where you are located:

Shell
$ find -name "FILE_NAME"

You can specify the directory where you want to search for the file:

Shell
$ find /home -name my_file.txt
$ find / -name my_other_file.txt

Take into consideration that this command is case sensitive. If you want to ignore the case use:

Shell
$ find -iname "FILE_NAME"

In case you want to search for files that do not adhere to the specified pattern you can negate the search parameters by using the -not parameter or \!. For example:

Shell
$ find -not -name "FILE_NAME_TO_AVOID"

or

$ find \! -name "FILE_NAME_TO_AVOID"

Find by file type

The file command also allows you to search for file types. Some common file type descriptors are:

  • f: regular file
  • d: directory
  • l: symbol link
  • c: character devices
  • b: block devices

The desired file type goes after the -type attribute.

For example, to find al JSON files under the etc directory:

Shell
$ find /etc -type f -name "*.json"

Find by file size

There is also a parameter used to filter the search result depending on the size of the files: -size.

For the size units you can use the following suffixes:

  • c: bytes
  • k: kilobytes
  • M: megabytes
  • G: gigabytes
  • b: 512-byte blocks

And to specify if you are talking about an exact size, a less than, or greater than use the following prefixes:

  • Exact search: no prefix
  • Less than: - prefix
  • Greater than: + prefix

For example, to find all 3 gigabytes files under the home directory:

Shell
$ find /home -size 3G

For all files under 3 gigabytes:

Shell
$ find /home -size -3G

Files over 3 gigabytes:

Shell
$ find /home -size +3G

Search by time

You can also search files according to their last access, modification, or change times.

  • Access time: -atime for the last time the file was read.
  • Modification time: -mtime for the last time the file contents were modified.
  • Change time: -ctime for the last time where the file's inode meta-data was changed.

It is also possible to compare against a reference file to return the ones that are newer:

Shell
$ find / -newer file_name

For the times, the parameters specified are in days. The same prefixes as in search by size can be used:

Shell
$ find /home -mtime 3
$ find /home -atime -2
$ find /home -ctime +5

To find files using time in minutes you can use:

Shell
$ find /home -mmin 3

Find by owner and permissions

There are also parameters to find files according to their owners (-user and -group) and their permissions (-perm):

Shell
$ find /home -user my_user
$ find /home -group my_group
$ find /home -perm 777

Defining search depth

When searching under a directory, the find command will return all files under that directory, even if they are inside other subdirectories. To control this depth you can define a maximum depth, a minimum depth, or both.

For example, to search only on the top directory and one level of subdirectories you could set the -maxdepth parameter to 2:

Shell
$ find -maxdepth 2 -name my_file

To define the minimum depth where find should search you use the -mindepth parameter:

Shell
$ find -mindepth 5 -name my_file

These parameters can be combined:

Shell
$ find -mindepth 4 -maxdepth 7 -name my_file

Executing commands on results

As saw in the depth commands, all find parameters can be combined to obtain very specific results. When you get the results you wanted it is possible to execute commands over them by using the -exec parameter.

When combining search parameters you can also use the -and and -or options to personalize your search criteria. When none of them are used, the -and option is the default.

For example, to change the permissions to all files under the /my_dir directory that currently have 777 permissions to 664 use the following:

Shell
$ cd /my_dir
$ find . -type f -perm 777 -exec chmod 664 {} \;

Locate tool

The locate command is an alternative for find. The difference is that it uses a database of the files in the filesystem so it can perform quicker. To install the locate tool you can use:

  • For Ubuntu/Debian Distributions:
Shell
$ sudo apt install mlocate`
  • For CentOS/RHEL Distributions:
Shell
$ sudo yum install mlocate`

Locate uses a cron job to update the database daily, but it can be manually updated by using:

Shell
$ sudo updatedb

For searching the "basename", namely that the query is contained only in the file's name and not in the file's path, use the -b flag:

Shell
$ locate -b name

To find files that still exist (after the last updatedb call) use the -e flag:

Shell
$ locate -e name
For statistics from the generated database use:

Shell
$ locate -S

References

    • Related Articles

    • KB 258332 - Backup, Snapshot, and Restore Linux installations using Timeshift

      If you mess up (or are going to mess up) some config files or drivers, it is always good to have a fallback point. The de-facto standard for doing this on Linux is Timeshift. Timeshift for Linux is an application that provides functionality similar ...
    • KB 361001 - What is Boot Process in Linux OS.

      Boot Process in Linux OS. Have you ever wondered what happens behind the scenes from the time you press the power button until the Linux login prompt appears? Press the power button on your system, and after few moments you see the Linux login ...
    • KB-630164 | How to use rsync to Synchronize Files

      Purpose To provide a clear and practical procedure for using the rsync utility to synchronize files and directories between local systems and remote hosts, including push and pull operations, while preserving file attributes and optimizing data ...
    • KB 019765 - How to Add Device Images in PNETLab (Installed on Proxmox)?

      1. Overview This article provides a comprehensive guide for adding device images (such as Cisco, Huawei, FortiGate, and other network operating systems) to PNETLab. Adding images is essential for creating virtual network labs. There are two primary ...
    • KB 591186 - Creating a multi-OS live USB with Ventoy

      Instead of having separate USBs for Windows, Linux and their variants, Ventoy allows you to assign a single drive capable of booting to any OS you choose. After setup, you can drag-and-drop iso files into the external drive and it will show up as an ...
    • Recent Articles

    • KB 148090 - Install an OS remotely via ASUS IPMI's KVM

      Here's how to install an OS remotely via ASUS IPMI's KVM (Java or HTML5-based remote console): 1. Access the BMC web interface. Connect to the ASUS IPMI/ASMB management LAN port (separate from the server's normal NICs). Browse to ...
    • KB 114035 - Zotac RMA Process

      ZOTAC RMA PROCESS Overview This plan outlines the overall operational support process for ZOTAC hardware supported by Mbuzz, covering warranty validation, technical diagnosis, RMA coordination, replacement and return handling, SLA management, ...
    • Download Log from ASUSTOR NAS

      How to download Log from NAS Purpose: This article will helps support team to guide customer to download Log to share the evidence for component failure. Problem: Customer reports for Storage device not detected. Storage failure. / Storage not ...
    • TeamGroup Memory Warranty & RMA Process

      Purpose To provide a clear and simple process for handling Team Group Memory warranty and RMA claims. Warranty Verification Product model and serial number. Purchase invoice or proof of purchase. Warranty eligibility. Product condition and any signs ...
    • Common Reasons for RMA Rejection

      Purpose This article outlines the most common reasons why RMA requests are rejected. Understanding these reasons helps Technical Support teams reduce claim delays and improve first-time approval rates. Physical Damage RMA requests are rejected when ...
    • Popular Articles

    • CP Plus Camera and NVR Configuration

      NVR Configuration The CP Plus Pro Series of NVRs have been meticulously designed for providing you with upgraded performance and higher recording quality in your IP video surveillance solution. The robust processor that has been inculcated in this ...
    • KB 775235 - Kerberos Authentication – Overview

      What is Kerberos? Kerberos is a secure authentication method used in our Active Directory (AD) environment (mbuzztech.com). It allows users to: Access multiple systems without re-entering passwords (Single Sign-On – SSO) Log in once Where We Use It ...
    • KB 935001 - AMD Ryzen AM4 Motherboard CPU Compatibility

      For the AM4 Platform, AMD has been very generous when it comes to motherboard compatibility. A CPU launched in 2022, like the Ryzen 7 5800X3D, will work with an almost 6-year-old motherboard, like the ASRock X370 Pro, which was launched in early ...
    • KB 532001 - Ubuntu Server 22.04 Installation Hangs on ASUS WRX80e-SAGE Series

      During the installation of Ubuntu Server 22.04 on ASUS WRX80E-SAGE series motherboards, the installation process hangs during the boot phase. The issue is associated with USB disk recognition errors, particularly with devices managed by the XHCI ...
    • KB 686001 - ASUS Server Stress Testing Guide for INTEL and AMD Platforms

      Creating a Bootable USB with Asus Test Tool Step 1: Download Required Files Download Asus Linux Image: Save the Linux image file provided by Asus. Download Memtest Tool: Open a browser, search for "Memtest," and download the free version from the ...