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. Therefore, there are a few approaches such as ls, find, etc which we can use to determine whether there are python files in the directory.

In this tutorial, we will review several approaches to check whether particular file types exist in a directory or not.

ls command

One of the most used commands in Linux is called "ls". It is used to display a list of the files and subdirectories found in the current directory. The “ls” command has several parameters and one optional argument. It can also be used on a standard file (non- directory). Below is an example, where we use the “ls” command which will list out all the content,


$ ls


Desktop Pitcures Documents Tutorials Movies lib
tuple.json lists modules rpm_packages public class methods use_cases.css

If we go inside the directory /Desktop and give the command “ls”, it will list out all the content of the /Desktop directory.


$ cd Desktop $ ls



Let's get started with the smart trick this article is intended to cover, i.e., how to check whether a specific file type exists in a directory.

Although readers.cpp and make.cpp are the files we are looking for in the example above, finding them in larger directories won't be as simple or as reliable. Additionally, it is much easier to let a machine handle the calculations.

Therefore, using a wildcard character is the key.

A wildcard character is a placeholder that is typically represented by a single character, such as an asterisk (*) or occasionally a star.

Let's go back to retrieve all files with the “.cpp” extension. Below is the command which lists all the files with the same extension,


$ ls *.cpp


readers.cpp make.cpp


In the following examples, we will try this command with some other file extensions as well,

$ ls *.txt


expert.txt utilities.txt protected.txt

Find command

One of the most well-known and often used commands in Linux operating systems is “find”. The list of files and folders is searched and located using the find command depending on the criteria.

Following is an example where we are trying to find a file named “1.txt” in the directory /hallo,


$ find \ -name “1.txt”



Let's go back to retrieve all files with the “.cpp” extension using the find command.

Following is an example where we use the find command to check whether a “.cpp” file exists in the directory. In the output, it will list all the locations where the files with a “.cpp” extension are present in the current directory or the sub-directory,


$ find /Desktop -type f -name “*.cpp”



File command

The file command displays the type of the file. It's useful when we need to identify a file type that we have never encountered before or when the file lacks a file extension. Depending on the arguments we give, it might also display other information, such as data contained in the compressed file, size, or file version.

In the following example, we will see the most basic form of file command to check the file “1.txt”. In the output, we will see it is a file type in ASCII format.


$ file 1.txt


1.txt : ASCII text

In another example, we would use the following command to determine the type of multiple “.txt” files in the directory /tmp,


$ file *txt


cd.txt: ASCII text
greg.txt: ASCII text
ravi.txt: ASCII text


These are several approaches through which we can check whether the particular file type exists in the directory or not. Some commands, such as “ls” and “find”, use file names and extensions to determine whether the file type is present or not. Another command useful to check a file type is the “file” command.

First, we used the “ls” command to locate a specific file type using a wildcard character. Then we used the “find” command to check the same. Last we used the “file” command to check whether the file type exists with basic and multiple scenarios.

I hope you can find these examples of commands useful in understanding the concept effectively.

Updated on: 19-Dec-2022


Kickstart Your Career

Get certified by completing the course

Get Started