How to find file in linux with find Command and Locate Command

Photo of author
By Jay
— 6 min read
Photo of author
Written by
Photo of author
Verified by
Published On
— 6 min read

find file in linux

Finding files in a Linux system can sometimes be a daunting task, especially if you have a large number of files and directories. Fortunately, Linux provides several powerful tools to help you quickly locate files. If you’re looking to find a file in Linux, two of the most commonly used commands for this purpose are find and locate. In this article, we will explore how to use these commands effectively to find file in linux.

What are the commands to search files and directories in linux?

To search files and directories there are two commands.

  • find command
  • locate command


Find file in linux with find Command

The find command is an essential and powerful utility in Linux and Unix-like operating systems that enables users to find file in Linux by searching for files and directories within a specified directory hierarchy. It offers a wide range of search criteria, allowing for highly customizable searches based on various attributes, such as file name, type, size, permissions, and more.

Basic Syntax

The basic syntax of the find command is as follows:

find [path] [expression]
  • [path]: This specifies the starting directory for the search. If you want to search from the root directory, you can use /.
  • [expression]: This includes the search criteria and options that define what you are looking for.

Here, path is the directory in which you want to start your search to find file in Linux, and expression is the search criteria you want to use.

Syntax:

# find <location><options><file or directory>

The options are,

-type: Search for files of a specific type, such as f for regular files or d for directories.
-name: Search for files or directories with a specific name.
-prem: search for permissions
-size: Search for files of a specific size, such as -size +10M for files larger than 10 megabytes.
-user: Search for files owned by a specific user.
-uid:  search for files/directories of uid)
-gid:  search for files/directories of gid)
-group: search for the group owner
-empty: search for empty files
-amin: searches for files that were last accessed n minutes ago.
-mmin: searches for files that were last modified n minutes ago.
-cmin: searches for files that were last changed n minutes ago.
-atime: search for access day (access day, minutes, hrs, ...etc)
-mtime: Search for files modified within a certain time period, such as -mtime -1 for files modified within the last day.
-ctime: search for change day (permissions, .....etc)

Examples of Using the find Command to find file in linux

To search for file names in the root directory:

find / -name <file name>

To find file names only in the root directory:

find / -name <file name> -type f

To find directories with lowercase names only in the root directory:

find / -name <directory name> -type d

To search for files/directories with a specified name (case-insensitive) in the root directory:

find / -iname <file/directory name> -type d

To search for empty files or directories in the root directory:

find / -empty

To search for empty files only in the root directory:

find / -empty -type f

To search for empty directories only in the root directory:

find / -empty -type d

To search for .mp3 files only in the root directory:

find / -name "*.mp3"

To search for files/directories with exact 10MB size in the root directory:

find / -size 10M

To search for files/directories smaller than 10MB in the root directory:

find / -size -10M

To search for files/directories larger than 10MB in the root directory:

find / -size +10M

To search for files/directories owned by the ‘student’ user in the root directory:

find / -user student

To search for files/directories owned by the ‘student’ group in the root directory:

find / -group student

To search for files/directories owned by the ‘student’ user and not owned by the ‘student’ group in the root directory:

find / -user student -not -group student

To search for files/directories owned by the ‘student’ user or owned by the ‘student’ group in the root directory:

find / -user student -o -group student

To search for files/directories which belong to the user having the specified user id:

find / -uid <uid no.>

To search for files/directories which belong to the group having the specified group id:

find / -gid <gid no.>

To search files/directories which are having the permissions 755:

find / -perm 755

To search files/directories which are having permissions below 755 and also at least one match also:

find / -perm -755

To search for files/directories which are modified within 20 minutes (above 20 minutes is represented by +20 and below 20 minutes is represented by -20):

find / -mmin -20

To search files/directories which are modified within 2 days:

find / -mtime 2

To search all .mp3 files and delete them:

find / -name "*.mp3" -exec rm -rf {} ;

To search all mp3 files and copy them into /ram directory:

find / -name "*.mp3" -exec cp -a {} /ram ;

To search student user’s files and directories and copy them into /ram directory:

find / -user student -exec cp -a {} /ram ;

To search files/directories which do not belong to any user and move them into /home/ram directory:

find / -nouser -exec mv -a {} /home/ram ;

To search for the 10 biggest files in the root directory:

du -h / | sort -rh | head -n 10

Find file in linux with locate Command

The locate command is a powerful utility in Unix-like operating systems that allows users to quickly find file in Linux by searching through a pre-built database. This command is particularly useful for quickly locating files without the need to traverse the filesystem in real time, which can be time-consuming.

How the locate Command Works

  1. Database-Driven Search: Unlike other search commands, such as find, which search the filesystem in real time, locate relies on a database that contains a list of all files and directories on the system. This database is typically stored in the file /var/lib/mlocate/mlocate.db. As a result, locate can provide faster search results when you want to find file in Linux, but it may not reflect the most current state of the filesystem if the database hasn’t been updated recently.
  2. Database Updates: The contents of the locate database are not updated automatically. Instead, they must be refreshed manually using the updatedb command. If the database is not updated, locate will not be able to find file in Linux that has been newly created, as it only searches the data that was present at the time of the last update.
  3. Performance Considerations: While locate is very fast due to its database-driven approach, it is not recommended for use on production servers without caution when you want to find file in Linux. Running updatedb can consume significant system resources and potentially impact server performance, especially if the filesystem is large or complex. For this reason, many system administrators prefer the find command, which does not require a pre-built database and searches in real-time.

Using the locate Command to find file in linux

To update the locate database, you can use the following command to find file in Linux:

sudo updatedb

This command typically requires superuser (root) privileges, as it needs access to all files on the system.

To search for a specific file or directory using the locate command, you would use the following syntax:

locate <file_name_or_directory_name>

For example, to find a file named example.txt, you would run:

locate example.txt

Read Also | RHCSA EX200 Latest Exam Questions with solutions

Summary

To find file in Linux, you can use powerful commands like find and locate. The find command allows you to search for files based on various criteria, such as name, type, size, and modification date, making it highly versatile. The locate command, on the other hand, quickly searches through a pre-built database of files, offering faster results but may not reflect the most recent changes. Understanding how to effectively use these tools can significantly enhance your file management skills and streamline your workflow in a Linux environment.

Reference: https://man7.org/linux/man-pages/man1/find.1.html

https://man7.org/linux/man-pages/man1/locate.1.html

Related Posts


About Author

Photo of author

Jay

I specialize in web development, hosting solutions, and technical support, offering a unique blend of expertise in crafting websites, troubleshooting complex server issues, and optimizing web performance. With a passion for empowering businesses and individuals online, I provide in-depth reviews, tech tutorials, and practical guides to simplify the digital landscape. My goal is to deliver clear, reliable, and insightful content that helps readers make informed decisions and enhance their online presence.

Leave a Comment