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

    • Recent Articles

    • 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, ...
    • 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-630107 | How to create a Linux swap file

      Purpose To provide Field Engineers and Linux administrators with a standardized procedure for creating, configuring, and validating a Linux swap file. This procedure helps ensure sufficient virtual memory is available when physical RAM is exhausted ...
    • MSI All-in-One (AIO) PC – Physical Damage Policy for Warranty Claims

      MSI All-in-One (AIO) PC – Physical Damage Policy for Warranty Claims MSI's standard warranty does not cover physical or accidental damage to an All-in-One (AIO) PC. If the damage is determined to be caused by external factors rather than a ...
    • KB 134833 - Troubleshooting NVSM Alert NV-CPU-XX – Unrecoverable CPU Internal Error

      Purpose This document provides a general troubleshooting procedure for the NVIDIA System Management (NVSM) alert NV-CPU-XX, which indicates that a CPU has reported an internal error. The article outlines how to verify whether the alert represents an ...
    • 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 ...
    • How to Remove and Reinstall NVIDIA Drivers on Ubuntu

      This article provides step by step guide to completely remove existing NVIDIA drivers and reinstall specific version of the NVIDIA driver on the Ubuntu system Prerequisites Administrative (sudo) access to the Ubuntu system. Internet access to ...
    • KB 692001 - Personal Computers and Servers - Classification and Point of Contact

      We can classify the computers that MBUZZ handles based on their form-factor as below: Tower Workstations, Desktops, Gaming PCs and SFF (Small form factor) PCs fall under this category. These are computers people would use on a desk and rarely move. ...
    • KB 298031 - M.2 SSD Tier List

      The sequential read and write speeds, which are usually the most advertised number, are not a proper benchmark of real-world performance or the quality of an SSD. This article categorizes and tiers SSDs based on factors like the type of NAND flash, ...