How to Search for Files on Linux using find and locate Commands

Search for Files on Linux using the find and locate Commands

When working with Linux, one common challenge is finding specific files. In this guide, we will explore how to use the find command to search for files based on various filters and parameters. We will also briefly cover the locate command, which provides an alternative way to search for files.

Prerequisites

In order to proceed with this tutorial, you must have a computer that runs on a Linux-based operating system. This can be either a local machine or a virtual private server accessible through SSH. While the demonstrations in this guide were verified on Ubuntu 20.04, they should function on any Linux distribution.

Searching by File Name

The most commonly used method to search for files is by their name. To locate a file by name using the find command, you can utilize the following syntax:

$ find / -name "query"

By default, this search is case-sensitive. If you want to perform a case-insensitive search, you can employ the -iname option:

$ find / -iname "query"

If you wish to find all files that do not match a particular pattern, you can utilize the -not option:

$ find / -not -name "query_to_avoid"

Alternatively, you can use the exclamation point (!) to invert the search:

$ find / \! -name "query_to_avoid"

When employing the exclamation point, remember to escape it with a backslash (\) to prevent the shell from interpreting it.

Searching by File Type

Another way to search for files is based on their type using the -type parameter. Here are some type descriptors you can utilize:

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

For instance, if you want to find all character devices on your system, execute the following command:

$ find /dev -type c

To search for files with a specific extension (e.g., .conf), employ the -name option along with the *.extension pattern:

$ find /usr -type f -name "*.conf"

You can combine multiple search expressions using the -and operator (implied when no option is specified) or the -or operator:

$ find -name query_1 -or -name query_2

Filtering by Size and Time

The find command offers various options to filter files based on their size and time.

Size

To filter files by size, utilize the -size parameter followed by a size value and a suffix indicating the unit. Common size suffixes include:

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

For instance, to locate files that are exactly 50 bytes in size:

$ find /usr -size 50c

To find files that are smaller than 50 bytes:

$ find /usr -size -50c

Time

To filter files based on their modification time, you can employ the -mtime-atime, and -ctime parameters.

  • -mtime searches based on the modification time of the file.
  • -atime searches based on the access time of the file.
  • -ctime searches based on the change time of the file.

You can specify the time using different formats:

  • n: exact number of 24-hour periods ago.
  • +n: more than n 24-hour periods ago.
  • -n: less than n 24-hour periods ago.

For example, to find files modified within the last 7 days:

$ find /usr -mtime -7

To locate files accessed more than 30 days ago:

$ find /usr -atime +30

To discover files whose metadata (such as permissions) has changed within the last 24 hours:

$ find /usr -ctime 0

Finding by Owner and Permissions

The find command also allows you to search for files based on their owner and permissions.

Owner

To find files owned by a specific user, you can use the -user parameter followed by the username. For example, to search for files owned by the user “john”:

$ find / -user john

Permissions

You can search for files based on their permissions using the -perm parameter. This parameter accepts a numeric mode or symbolic notation representing the desired permissions.

To find files with specific permissions expressed in numeric mode, use a three-digit octal number. For example, to search for files with read, write, and execute permissions for the owner, and read permissions for others:

$ find / -perm 744

To search for files with specific permissions expressed in symbolic notation, use a combination of u (user), g (group), o (others), r (read), w (write), and x (execute). For example, to search for files with read and write permissions for the owner:

$ find / -perm u=rw

You can also combine multiple permissions and specify different ownership levels in symbolic notation. For example, to search for files with read and execute permissions for the owner, write permission for the group, and read permission for others:

$ find / -perm u=rx,g=w,o=r

By utilizing the -user and -perm options, you can effectively search for files based on their owner and permissions.

Executing Commands on find Results

The find command not only helps you locate files but also enables you to perform actions on the search results. You can execute various commands on the files found by find using the -exec option.

The basic syntax for executing commands on find results is as follows:

$ find /path/to/search -name "query" -exec command {} \;

In this syntax:

  • /path/to/search is the directory where you want to search for files.
  • "query" is the search query or pattern to match filenames.
  • command is the command you want to execute on each found file.
  • {} represents the placeholder for the found file.
  • \; is used to terminate the -exec option.

For example, to delete all files with the .log extension in the /var/log directory, you can use the following command:

$ find /var/log -name "*.log" -exec rm {} \;

This command will execute the rm command on each file found by find, deleting all files with the .log extension.

You can also combine multiple commands or pass additional arguments to the command. Just ensure to properly escape any special characters.

Using the -exec option with the find command provides flexibility in performing actions on the search results, making it a powerful tool for file management and automation.

Using locate Command

The locate command provides a faster way to search for files on your system by utilizing a prebuilt database called the locate database. The database is updated regularly, so it may not include recently created or modified files.

Database Initialization:

Before using the locate command, it’s crucial to ensure that the database is up to date. Execute the following command as root or with sudo:

$ sudo updatedb

This command updates the database used by locate to index the file system.

Searching for Files:

To search for files using locate, simply specify the keyword or pattern you want to search for. For example:

$ locate file.txt

This command will find all files with “file.txt” in their name.

Case Sensitivity:

By default, the locate command is case-insensitive. If you want to perform a case-sensitive search, use the -i option. For example:

$ locate -i File.txt

This command will find files with “File.txt” or “file.txt” in their name.

Updating the Database:

The file database used by locate may not always be up to date. To ensure you have the latest file information, periodically update the database using the updatedb command.

Conclusion

Searching for files on Linux can be accomplished using the find and locate commands. The find command provides more flexibility and allows for fine-grained searches based on various criteria, such as file name, type, size, and time. On the other hand, the locate command provides a faster search using a prebuilt database.

By mastering these commands, you can efficiently locate files on your Linux system and streamline your workflow.

LEAVE A COMMENT