Linux Shell Commands: Find File Extensions and .pc_* Files
Managing files in a Linux environment often requires identifying specific file types or extensions recursively across directories. This guide explains two powerful shell commands to: (1) list distinct file extensions and (2) find files with a specific pattern (.pc_*), both excluding CVS directories commonly used in version control. Whether you're auditing a project or troubleshooting an iMX instance, these commands are essential tools for system administrators and developers.
Command 1: List Distinct File Extensions
This command recursively finds all files with extensions, excludes CVS directories, and outputs a unique list of extensions.
find ./ -type f -name "*.*" -not -path "*/CVS/*" | \
sed 's/.*\.\([^.]*\)$/\1/' | \
sort -u | \
while read -r ext; do
echo "$ext"
done
How It Works
find ./ -type f -name "*.*" -not -path "*/CVS/*": Searches for files (-type f) with extensions (*.*), skipping CVS folders.sed 's/.*\.\([^.]*\)$/\1/': Extracts the extension after the last dot.sort -u: Sorts and removes duplicate extensions.while read -r ext; do echo "$ext": Prints each unique extension.
Example Output: txt, sh, py
Command 2: Find Files with .pc_* Pattern
This command locates files matching the .pc_* pattern (e.g., .pc_1, .pc_backup), excluding CVS directories.
find ./ -type f -name "*.pc_*" -not -path "*/CVS/*"
How It Works
find ./ -type f: Searches for files in the current directory and subdirectories.-name "*.pc_*": Matches files with names starting with.pc_.-not -path "*/CVS/*": Excludes CVS directories.
Example Output: ./scripts/.pc_config, ./data/.pc_backup
Why Exclude CVS?
CVS (Concurrent Versions System) directories contain metadata files (e.g., Entries, Root) that are not part of the project's primary content. Excluding them with -not -path "*/CVS/*" ensures the commands focus on relevant files, improving accuracy and relevance, especially in legacy projects or iMX environments.
Best Practices
- Use these commands in a version-controlled directory to avoid clutter from metadata.
- Combine with
greporawkfor advanced filtering (e.g., text files only). - Test in a small directory first to verify output.