Searching in Bash

How to search files and directories based on various attributes, using different methods. Moreover, this lesson encompasses the ways to search for files based on their contents and patterns.

locate

Definition:

The command locate is a quick way to search for the locations of files and directories.

Syntax:

locate [options] name(s)

Options:

Option Description
-q To suppress error messages, such as those that might be returned in the event that the user does not have permission to access designated files or directories.
-n This option followed by an integer limits the results to a specific number.
-i To perform a case-insensitive search.
-V To show which version of locate is used, including whether it is locate or slocate.

Example:

  • To search for all files named ‘file1’ and all directories named ‘dir1’, for which the user had access permission:
locate file1 dir1
  • To display all the files in the system with “.jpeg” extension:
locate "*.jpeg"
  • In the above example, in order to display only 10 results:
locate -n 10 "*.jpeg"

Get hands-on with 1200+ tech skills courses.