

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Count Number of Files and Subdirectories inside a Given Linux Directory?
It often becomes essential to know not just the count of files in my current directory but also the count of files from all the subdirectories inside the current directory. This can be found out using the
Using ls
we can use ls to list the files, then choose only the ones that start with ‘-‘ symbol. The R option along with the l option does a recursive search. The ‘-c’ option counts the number of lines which is the number of files.
ls -lR . | egrep -c '^-'
Running the above code gives us the following result −
13
Using find With Hidden Files
The find command helps us find the files with certain criteria by recursively traversing all the directories and there subdirectories. We use it with the type option to get only files by supplying the argument f. Here it also counts all the hidden files.
find . -type f | wc -l
Running the above code gives us the following result −
1505
Using find Without hidden Files
We use the same approach as in the previous command, but use a regular expression pattern to avoid the . character by using the escape character \.
find . -not -path '*/\.*' -type f | wc -l
Running the above code gives us the following result −
13
- Related Questions & Answers
- How to grep a string in a directory and all its subdirectories in Linux?
- Get all subdirectories of a given directory in PHP
- How to use the sed command to replace a text in files present in a directory and subdirectories?
- How to unzip all zipped files in a Linux directory?
- How to copy a file, group of files, or directory in Linux?
- How to get all the files, sub files and their size inside a directory in C#?
- Count the number of rhombi possible inside a rectangle of given size in C++
- Linux – How to find the files existing in one directory but not in the other directory?
- Increase number of maximum open files in linux
- How to move a file, group of files, and directories in Linux?
- How to disable delete permission of File and Directory in Linux?
- How can I iterate over files in a given directory in Python?
- How to Count the Number of Threads in a Process on Linux
- How to count digits of given number? JavaScript
- PHP: Unlink All Files Within A Directory, and then Deleting That Directory