How to Recursively Find All .pem Files in Linux Using Find Command


2 views

The basic command structure for finding files by extension is:

find [starting-directory] -type f -name "*.extension"

For your specific case of finding .pem files across the entire filesystem, you would use:

sudo find / -type f -name "*.pem"

There are a few potential issues with the original command you proposed:

sudo find / -type f -name *.pem

1. The unquoted wildcard (*.pem) might be expanded by the shell before find executes
2. Case sensitivity might cause you to miss some files
3. Permission errors might stop the search prematurely

Here's a more robust version that addresses these issues:

sudo find / -type f -iname "*.pem" 2>/dev/null

Key improvements:

  • -iname makes the search case-insensitive (matches .PEM, .pEm, etc.)
  • Quotes around the pattern prevent shell expansion
  • 2>/dev/null suppresses permission denied errors

If you only need to search in specific directories:

find /etc /usr/local /home -type f -name "*.pem"

For better performance on large filesystems:

sudo locate *.pem

(Note: locate uses a pre-built database, so run sudo updatedb first if files were recently created)

You can pipe results to other commands, for example counting matches:

sudo find / -type f -name "*.pem" | wc -l

Or process each file found:

sudo find / -type f -name "*.pem" -exec chmod 600 {} \;

This changes permissions of all found .pem files to 600.

If the command seems slow, try limiting the search depth:

sudo find / -maxdepth 3 -type f -name "*.pem"

For extremely large filesystems, consider using GNU parallel:

sudo find / -type f -name "*.pem" | parallel -j8 grep "BEGIN CERTIFICATE" {}

To find all .pem files on a Linux system, the find command is indeed the right tool. The basic syntax you proposed:

sudo find / -type f -name "*.pem"

This command searches the entire filesystem (/) for regular files (-type f) with names ending in .pem. The sudo ensures you have permission to search all directories.

There are a few key points to note about this command:

1. Always quote the pattern: -name "*.pem" prevents shell expansion
2. Consider adding -print0 for safer handling of filenames with spaces:
   find / -type f -name "*.pem" -print0 | xargs -0 ls -l
3. For better performance on large filesystems, you might want to exclude certain directories:
   find / -path /proc -prune -o -path /sys -prune -o -type f -name "*.pem" -print

While find is the most comprehensive solution, there are other ways to locate files:

# Using locate (faster but depends on updatedb)
locate "*.pem"

# Using grep with ls (not recursive)
ls -R / | grep "\.pem$"

# Using fd (a modern alternative to find)
fd -e pem /

You can pipe the results to other commands for further processing:

# Count all .pem files
find / -type f -name "*.pem" | wc -l

# Get detailed file information
find / -type f -name "*.pem" -exec ls -lh {} \;

# Copy all found files to a directory
find / -type f -name "*.pem" -exec cp {} /path/to/destination/ \;

For very large filesystems, consider these optimizations:

# Limit search depth
find / -maxdepth 3 -type f -name "*.pem"

# Search only specific filesystems
find / -xdev -type f -name "*.pem"

# Parallel processing with GNU parallel
find / -type f -name "*.pem" | parallel -j8 ls -l