Operations 4 min read

Using the Linux find Command to Locate *.txt Files with Path, Prune, and Logical Operators

This guide demonstrates how to use the Linux find command with various path patterns, logical operators, and pruning techniques to locate *.txt files in specific directories, exclude certain directories, and combine multiple search criteria in a single command.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Using the Linux find Command to Locate *.txt Files with Path, Prune, and Logical Operators

1. Find all files with a .txt suffix in the current directory:

find ./ -name "*.txt"

2. Find .txt files under the dir0 directory and its sub‑directories:

find ./ -path "./dir0*" -name "*.txt"

3. Find .txt files under dir0/dir00 and its sub‑directories:

find ./ -path "*dir00*" -name "*.txt"

4. Find .txt files outside dir0 and its sub‑directories:

find ./ -path "./dir0*" -a -prune -o -name "*.txt" -print

Note: -a is the logical AND operator, -o is OR, and -prune returns true for the matched path, causing the following -o branch to be evaluated only for directories that are not dir0 .

5. Find .txt files under both dir0 and dir1 (including their sub‑directories):

find ./ \( -path "./dir0*" -o -path "./dir1*" \) -a -name "*.txt" -print

6. Find .txt files outside dir0 and dir1 and their sub‑directories:

find ./ \( -path "./dir0*" -o -path "./dir1*" \) -a -prune -o -name "*.txt" -print

The parentheses group expressions; they must be escaped (\( and \)) so the shell passes them unchanged to find .

7. Find .txt files in any directory named dir_general :

find ./ -path "*/dir_general/*" -name "*.txt" -print

8. List all files while excluding the .git directory (the .git directory itself is not printed):

find . -path ./.git -prune -o -print -a \( -type f -o -type l -o -type d \) | grep '.git'

linuxshellfile-searchpathfindprune
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.