- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a File Type Exists in a Directory?
Abstract
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,
Example
$ ls
Output
Desktop Pitcures Documents Tutorials Movies README.dm lib tuple.json lists style_sheet.py 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.
Example
$ cd Desktop $ ls
Output
readers.cpp samaritan.sh expert.txt fast.jpg source_code.py utilities.txt make.cpp header.jpg boot_up.docx protected.txt
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,
Example
$ ls *.cpp
Output
readers.cpp make.cpp
Example
In the following examples, we will try this command with some other file extensions as well,
$ ls *.txt
Output
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,
Example
$ find \ -name “1.txt”
Output
/tmp/hallo/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,
Example
$ find /Desktop -type f -name “*.cpp”
Output
/Desktop/readers.cpp /Desktop/make.cpp /Desktop/ext_src/booting_process.cpp /Desktop/notch/filter/install_code.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.
Example
$ file 1.txt
Output
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,
Example
$ file *txt
Output
cd.txt: ASCII text greg.txt: ASCII text ravi.txt: ASCII text
Conclusion
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.