Fundamentals 5 min read

Using the Linux find Command: Syntax, Real‑time Search, and Common Methods

The article explains why the Linux find command is preferred over locate for real‑time, precise file searches, outlines its basic syntax, and demonstrates three common usage methods—piping to xargs, command substitution with $(), and the -exec option—complete with example commands and outputs.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Using the Linux find Command: Syntax, Real‑time Search, and Common Methods

find command extensions

Why use the find command?

Linux systems contain thousands of files, making a search utility essential; while locate uses a pre‑built database and can be faster, it does not provide real‑time results and may miss newly created files, plus it performs fuzzy matching. In contrast, find searches in real time, offers exact matching, and, given sufficient permissions, can locate any file you specify.

Syntax format of the find command:

find [search_path] [search_conditions] [action]

1.1 Method 1: |xargs

Using the pipe | to pass the output of one command to xargs , which then supplies the results as arguments to the following command.

[root@master ]# find /opt/ -type f -name "*.sh" | xargs ls -l

-rw-r--r--. 1 root root 7 Aug 17 17:00 /opt/test/del.sh

-rw-r--r--. 1 root root 8 Aug 17 19:35 /opt/test.sh

-rw-r--r--. 1 root root 7 Aug 17 17:00 /opt/t.sh

1.2 Method 2: $()

The $( ) construct runs the enclosed command first and substitutes its output into the surrounding command line.

[root@master]# ls -l $(find /opt/ -type f -name "*.sh")

-rw-r--r--. 1 root root 7 Aug 17 17:00 /opt/test/del.sh

-rw-r--r--. 1 root root 8 Aug 17 19:35 /opt/test.sh

-rw-r--r--. 1 root root 7 Aug 17 17:00 /opt/t.sh

1.3 Method 3: -exec {} \;

The -exec option is built into find ; the placeholder {} represents each file found, and the command following -exec is executed on each such file.

[root@master]# find /opt/ -type f -name "*.sh" -exec ls -l {} \;

-rw-r--r--. 1 root root 7 Aug 17 17:00 /opt/t.sh

-rw-r--r--. 1 root root 8 Aug 17 19:35 /opt/test.sh

-rw-r--r--. 1 root root 7 Aug 17 17:00 /opt/test/del.sh

[root@master]# find /aaemzl/ -type f -name "*.sh" -exec ls -l {} \;

-rw-r--r-- 1 root root 5 Oct 12 01:10 /aaemzl/test/del.sh

-rw-r--r-- 1 root root 5 Oct 12 01:10 /aaemzl/test.sh

-rw-r--r-- 1 root root 5 Oct 12 01:10 /aaemzl/t.sh

linuxShellCommand-linefile-searchfind
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.