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.
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" -print6. Find .txt files outside dir0 and dir1 and their sub‑directories:
find ./ \( -path "./dir0*" -o -path "./dir1*" \) -a -prune -o -name "*.txt" -printThe 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'Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
