Linux find command

In Linux, the “find” command is used to search for files and directories in a specified location and perform various actions on them. The “find” command is highly versatile and allows you to search for files based on different criteria such as name, size, type, and more. Here’s how to use the “find” command:

  1. Open a Terminal:
    Launch a terminal emulator on your Linux system.
  2. Type the “find” command:
    The basic syntax of the “find” command is as follows:
   find <path> <expression> <action>
  • The “” parameter represents the starting directory for the search.
  • The “” specifies the search criteria, such as file name patterns, size, type, and more.
  • The “” defines the action to be performed on the matched files or directories. For example, to search for all files with the extension “.txt” in the current directory and its subdirectories, you can use the following command:
   find . -name "*.txt"

In this example, “.” represents the current directory, “-name” specifies the search by file name, and “*.txt” is the pattern to match files with the “.txt” extension.

  1. Perform Actions on the Matched Files:
    Once you have specified the search criteria, you can perform various actions on the matched files or directories. Some commonly used actions include:
  • Printing the matched files/directories: Use the “-print” action to display the names of the matched files or directories. For example: find . -name "*.txt" -print
  • Executing commands on the matched files/directories: Use the “-exec” action to execute commands on the matched files or directories. For example, to delete all files with the extension “.tmp” in the current directory and its subdirectories, you can use the following command: find . -name "*.tmp" -exec rm {} \; In this example, “{}” represents the placeholder for each matched file, and “\;” marks the end of the “-exec” command.
  • Performing other actions: The “find” command offers many other actions, such as copying files, moving files, changing permissions, and more. Refer to the “man find” command to explore additional actions and their usage.
  1. Verifying the Output:
    After running the “find” command, you will see the results displayed in the terminal. The output will show the names of the matched files or directories based on the specified search criteria.
  2. Exiting the Command:
    Once you have obtained the desired results using the “find” command, you can continue executing other commands or exit the terminal as needed.

The “find” command provides powerful capabilities for searching files and directories in Linux. By combining different search expressions and actions, you can locate specific files based on various criteria and perform actions on them. Make sure to double-check your command and exercise caution when performing actions on files or directories.