Linux find Command: File Searching

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial provides an introduction to the find command in Linux, a versatile utility for searching and locating files and directories based on various criteria.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") subgraph Lab Skills linux/cd -.-> lab-219191{{"`Linux find Command: File Searching`"}} linux/find -.-> lab-219191{{"`Linux find Command: File Searching`"}} end

find Command

The find command is a powerful tool for searching and locating files and directories in a Linux filesystem. It allows users to specify a wide range of criteria to narrow down the search results.

Command Usage

Let's begin by understanding the basic usage of the find command. The find command is used to search for files and directories based on specified criteria. And first, by using cd /home/labex/project, we change to the specified path /home/labex/project.

terminal

Input:

cd /home/labex/project
find "example1.txt"

Output:

example1.txt

In this example, the find command searches for files with the name example1.txt within the /home/labex/project directory.

Parameters and Usage Examples

The find command provides a plethora of options to customize searches and meet specific requirements.

Option Parameter

find [path] [expression]

The default search path is the current directory and the default expression is -print, and the common find expression options are:

  • -name : Find files by file name.
  • -type : Find certain type of file.
  • -exec : find command executes the shell command given for the matching file.
  • -print: find command outputs matching files to standard output using it.

Example Usage

1. Find Files by Name (-name)

The find command is commonly used to locate files based on their names. In this example, we will search for files named example_name:

Input:

find /home/labex -name "example_name.txt"

Output:

/home/labex/project/example_name.txt

The find command can be used to search specifically for directories. In this example, we will find all directories within the /home/labex/project directory:

Input:

find /home/labex/project -type d

Output:

/home/labex/project
/home/labex/project/documents

3. Execute a Command for Each Matching File (-exec)

The find command can also be used to execute a command for each matching file. In this example, we will list the permissions and owner of

Input:

find /home/labex/project -name "*.txt" -exec ls -l {} \;

Output:

-rw-rw-r-- 1 labex labex 0 Dec 27 16:37 /home/labex/project/example1.txt
-rw-rw-r-- 1 labex labex 0 Dec 27 16:37 /home/labex/project/example2.txt
-rw-rw-r-- 1 labex labex 0 Dec 27 16:37 /home/labex/project/example_name.txt

The command find /home/labex/project -name "*.txt" -exec ls -l {} \; searches for files with the extension .txt in the /home/labex/project directory and executes the ls -l command on each found file, displaying their detailed information.

Summary

The find command is a versatile tool for searching and locating files and directories in a Linux system. Its flexibility in specifying search criteria makes it a powerful asset for both basic file searches and complex operations performed by system administrators and advanced users.

Other Linux Tutorials you may like