Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 2.2 - Locating Files

find Command

The find command is an essential utility in Linux that facilitates the search for files and directories within a designated location. It offers extensive flexibility by enabling users to specify search criteria based on attributes like file name, size, modification time, and more.

The basic syntax of the command is:

find [path] [expression]

The path refers to the starting directory for the search. If omitted, the command defaults to the current directory. The expression is where you define the search criteria, which can encompass multiple options to refine the search. Some of these options are listed below:

  • -name: This option is used to search for files by their names.
  • -type: Allows you to search based on file types (e.g., regular files or directories).
  • -size: Enables searching by file size, for example, files larger than 1MB.
  • -mtime: Helpful for searching by modification time; for instance, to find files modified within the last 7 days.
  • -exec: Use this option to execute a command on each located file, enabling actions such as deletion of specific file types.

In more complex searches, you can combine expressions using operators like -o (OR) and -a (AND). For instance, you can find all .txt files modified within the last 7 days by using:

find . -name "*.txt" -a -mtime -7

If both -o and -a options are missing, the -a option is assumed.

locate Command

In contrast to find, the locate command operates differently and provides an efficient means of searching for files. It relies on a pre-built database that contains filenames and their corresponding paths.

The basic syntax of the command is:

locate [filename]

You can execute this command to quickly locate a file by its name. For example:

locate example.txt

One key advantage of locate is its speed. It is considerably faster than find for basic file searches because it doesn't search the filesystem directly; it leverages the prebuilt database. The database is typically updated automatically as a system cron job which typically runs daily. However, a notable limitation is that locate might not find recently added files until the database is refreshed.

The choice between find and locate depends on your specific requirements. If you need real-time file location and the ability to specify complex search criteria, find is the better choice. If you desire quick searches for frequently accessed files, locate is a more suitable option. Both find and locate are valuable tools in the Linux command-line toolkit, and mastering them can significantly enhance your file management and search capabilities in a Linux environment.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.