funwithlinux blog

How to Recursively Find Files with a Specific Extension Using Bash on Ubuntu 10.04 LTS (Lucid Lynx)

Ubuntu 10.04 LTS (codenamed "Lucid Lynx") may be an older release (released in 2010, end-of-life in 2015), but it remains in use in legacy systems, embedded environments, or by users maintaining vintage setups. A common task in such environments—whether you’re a system administrator, developer, or power user—is recursively finding files by their extension (e.g., .txt, .pdf, .py) across directories and subdirectories.

Recursive search is critical because manually navigating nested folders to locate specific files is time-consuming and error-prone. Bash, Ubuntu’s default shell, provides powerful tools to automate this. In this guide, we’ll focus on the find command—the most reliable and flexible tool for recursive file searches—and explore its usage, advanced options, and practical examples tailored to Ubuntu 10.04 LTS.

2025-11

Table of Contents#

  1. Prerequisites
  2. Understanding Recursive File Search
  3. The find Command: Your Primary Tool
  4. Alternative: ls -R with grep (Limitations Included)
  5. Practical Examples
  6. Troubleshooting Common Issues
  7. Conclusion
  8. References

Prerequisites#

Before diving in, ensure you have:

  • Ubuntu 10.04 LTS (Lucid Lynx) installed or access to a running instance (physical, virtual, or containerized).
  • Command-Line Access: Open a terminal via Applications > Accessories > Terminal or press Ctrl+Alt+T.
  • Basic Bash Knowledge: Familiarity with navigating directories (cd), listing files (ls), and running commands.
  • Sudo Privileges (Optional): Required if searching restricted directories (e.g., /root, /etc).

Note: Ubuntu 10.04 LTS is no longer supported by Canonical, meaning it receives no security updates. Use it only in isolated, non-production environments.

"Recursive" means searching not just in the specified directory, but also in all its subdirectories, sub-subdirectories, and so on. For example, if you search for .txt files in /home/user, the tool will check /home/user/documents, /home/user/documents/old, and every nested folder under /home/user.

Bash is ideal for this because it includes lightweight, built-in tools like find that are optimized for directory traversal. Unlike GUI file managers, these tools work efficiently even on systems with limited resources—common in legacy environments.

The find Command: Your Primary Tool#

The find command is the gold standard for recursive file search in Unix-like systems, including Ubuntu. It’s preinstalled on Ubuntu 10.04 and offers granular control over search criteria (filename, type, size, permissions, etc.).

To recursively find files by extension with find, use this structure:

find [starting-path] -name "*.extension"  

Breakdown:#

  • [starting-path]: The directory to start the search (e.g., . for current directory, /home/user for a specific path). Omit to default to the current directory.
  • -name "*.extension": The search filter. *.extension is a wildcard pattern matching any file ending with .extension (e.g., *.txt for text files).

Critical Note: Always Quote the Pattern!#

Wrap "*.extension" in quotes (single or double) to prevent shell globbing. Without quotes, the shell expands * before find runs, limiting the search to the current directory (not recursively). For example:

  • Wrong: find . -name *.txt (shell expands *.txt to existing .txt files in the current directory, so find only searches for those specific filenames).
  • Right: find . -name "*.txt" (quotes preserve the * for find to handle recursively).

Key Options for Precision#

find supports options to refine results. Here are the most useful for extension searches:

OptionPurpose
-type fSearch only for files (exclude directories, symlinks, etc.).
-inameCase-insensitive search (e.g., match .TXT, .Txt, and .txt).
-maxdepth NLimit recursion to N levels (e.g., -maxdepth 2 searches current dir + 2 subdir levels).
-mindepth NStart searching from at least N levels deep (skips shallow directories).

Example 1: Find All .pdf Files (Current Directory, Files Only)#

To search for .pdf files recursively in the current directory, excluding directories:

find . -type f -name "*.pdf"  

Output might look like:

./reports/2023/annual.pdf  
./docs/manual.pdf  
./old/archive.pdf  

Example 2: Case-Insensitive Search for .jpg/.JPG#

Use -iname to match .jpg, .JPG, or .Jpg:

find /home/user/photos -type f -iname "*.jpg"  

Advanced: Executing Commands on Found Files#

The -exec option lets you run commands (e.g., ls, cp, rm) on files found by find. Syntax:

find [path] -name "*.ext" -exec [command] {} \;  
  • {}: A placeholder for the path of each found file.
  • \;: Signals the end of the -exec command (required).

Example: List Details of Found .sh Files#

To recursively find .sh (shell script) files and display their permissions/sizes with ls -l:

find /home/user/scripts -type f -name "*.sh" -exec ls -l {} \;  

Output:

-rwxr-xr-x 1 user user 1234 Jan 1 10:00 /home/user/scripts/backup.sh  
-rw-r--r-- 1 user user 567  Feb 2 14:30 /home/user/scripts/utils.sh  

Caution: Deleting Files with -exec rm#

You can delete found files with -exec rm {} \;, but always test first with ls to avoid accidental data loss:

# Test: List files to delete  
find . -type f -name "*.tmp" -exec ls {} \;  
 
# Delete (if safe)  
find . -type f -name "*.tmp" -exec rm {} \;  

Alternative: ls -R with grep (Limitations Included)#

A common alternative is combining ls -R (recursive list) with grep (pattern search):

ls -R | grep -i "\.txt$"  

How It Works:#

  • ls -R: Lists all files and directories recursively.
  • grep -i "\.txt$": Filters lines ending with .txt (case-insensitive via -i).

Limitations (Why find is Better):#

  • Includes Directories: If a directory is named notes.txt, ls -R | grep will falsely match it.
  • Performance: ls -R is slower than find for large directory trees.
  • No File-Specific Filters: You can’t easily exclude symlinks or limit depth like with find -type f or -maxdepth.

Use find instead for reliability.

Practical Examples#

Let’s walk through real-world scenarios to solidify your understanding.

Example 1: Search System-Wide for .conf Files#

Find all configuration files (.conf) across the entire system (may require sudo for restricted directories):

sudo find / -type f -name "*.conf" 2>/dev/null  
  • 2>/dev/null: Suppresses "Permission denied" errors (common when searching system directories).

Example 2: Find .log Files Modified in the Last 7 Days#

Use -mtime -7 to filter files modified in the last 7 days (useful for recent logs):

find /var/log -type f -name "*.log" -mtime -7  

Example 3: Copy Found .csv Files to a Backup Folder#

Use -exec cp to copy all .csv files from ~/data to ~/backups/csv:

mkdir -p ~/backups/csv  # Create backup dir if missing  
find ~/data -type f -name "*.csv" -exec cp {} ~/backups/csv/ \;  

Troubleshooting Common Issues#

"No Files Found" When They Exist#

  • Check Quoting: Did you wrap "*.ext" in quotes? Unquoted * causes shell globbing (e.g., find . -name *.txt fails).
  • Path Correctness: Verify the starting path (e.g., find /home/yourname instead of find /home/othername).
  • Case Sensitivity: Use -iname if the extension might have uppercase letters (e.g., .TXT).

"Permission Denied" Errors#

  • Run with sudo if searching restricted directories (e.g., sudo find /etc -name "*.conf").
  • Suppress errors with 2>/dev/null (e.g., find / -name "*.txt" 2>/dev/null).

find Returns Directories Instead of Files#

Add -type f to exclude directories (e.g., find . -type f -name "*.txt").

Conclusion#

Recursively finding files by extension in Ubuntu 10.04 LTS is straightforward with the find command. Its flexibility—via options like -name, -type f, and -exec—makes it indispensable for system administration and development tasks. Remember to:

  • Quote wildcard patterns (e.g., "*.txt") to avoid shell globbing.
  • Use -type f to focus on files, not directories.
  • Test -exec commands with ls before risky actions like rm.

While alternatives like ls -R | grep exist, find remains the most reliable and efficient choice for recursive searches.

References#