How to check if a File Type Exists in a Directory?

There are times when we need to determine whether a particular file type is present in a directory or not. For instance, we might want to check if a directory contains Python files or C++ source files. Several Linux commands like ls, find, and file can help us accomplish this task efficiently.

In this tutorial, we will review multiple approaches to check whether particular file types exist in a directory using wildcard patterns and command-line utilities.

Using the ls Command

The ls command is one of the most frequently used commands in Linux for listing files and directories. It becomes particularly powerful when combined with wildcard characters to filter specific file types.

Basic Directory Listing

First, let's see what files are in our current directory

$ ls
Desktop Pictures Documents Tutorials Movies README.md lib
tuple.json lists style_sheet.py modules rpm_packages public class methods use_cases.css

Now, let's navigate to the Desktop directory to see its contents

$ cd Desktop
$ ls
readers.cpp
samaritan.sh
expert.txt
fast.jpg
source_code.py
utilities.txt
make.cpp
header.jpg
boot_up.docx
protected.txt

Using Wildcards to Filter File Types

A wildcard character (*) serves as a placeholder that matches any sequence of characters. To find all files with a specific extension, we use the pattern *.extension.

To list all C++ files (.cpp extension)

$ ls *.cpp
readers.cpp make.cpp

Similarly, to find all text files

$ ls *.txt
expert.txt utilities.txt protected.txt

Using the find Command

The find command is more powerful than ls as it can search recursively through subdirectories and provides more filtering options. It's particularly useful for searching large directory trees.

Basic find Syntax

To search for a specific file by name

$ find . -name "1.txt"
./tmp/hallo/1.txt

Finding Files by Extension

To find all C++ files in the current directory and subdirectories

$ find /Desktop -type f -name "*.cpp"
/Desktop/readers.cpp
/Desktop/make.cpp
/Desktop/ext_src/booting_process.cpp
/Desktop/notch/filter/install_code.cpp

The -type f option ensures we only find regular files, not directories that might match the pattern.

Using the file Command

The file command determines the actual file type by examining the file's contents, not just its extension. This is useful when files lack extensions or when you need to verify the actual file format.

Checking a Single File

$ file 1.txt
1.txt: ASCII text

Checking Multiple Files

To check the type of all text files in the current directory

$ file *.txt
cd.txt: ASCII text
greg.txt: ASCII text  
ravi.txt: ASCII text

Comparison of Methods

Command Search Scope Based On Best For
ls *.ext Current directory only File extension Quick listing in single directory
find -name "*.ext" Recursive (subdirectories) File extension Comprehensive search across directories
file * Specified files File content analysis Verifying actual file types

Practical Examples

Checking for Python Files

$ ls *.py 2>/dev/null && echo "Python files found" || echo "No Python files"

Counting Files by Type

$ find . -name "*.cpp" | wc -l

Conclusion

These commands provide efficient ways to check for specific file types in directories. The ls command with wildcards is perfect for quick searches in single directories, while find excels at comprehensive searches across directory trees. The file command verifies actual file types regardless of extensions, making it invaluable for content validation.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements