Introduction
Before we begin talking about how to count files in directory on Linux, let's briefly understand - What is a Directory?
A directory, refers to a file system container that holds files and other directories. Also, commonly known as a folder, a directory provides a hierarchical structure for organizing and storing files logically.
There are instances when you need to know how many files are in a directory. Counting files in directories is a common task on Linux when you need to quickly determine the number of files present. With a simple command, you can retrieve this information, assisting in various administrative and organizational tasks.
In this tutorial, we'll show you how to get the number of files in a directory on Linux using various different methods. We will also address few FAQs on how to Count Files in Directory on Linux.
Count Files in Directory
The most straightforward technique to count files in a directory is to use ls
to list one file per line and route the output to wc
to count the lines:
ls -1U DIR_NAME | wc -l
The command above will return the total number of files in your directory, including directories and symlinks. The -1
argument indicates list one file per line, while the -U
option tells ls
not to sort the output, which speeds up the command execution.
The command ls -1U
does not count hidden files (dotfiles).
If you simply want to count files and not directories, use the following formula:
ls -1Up DIR_NAME | grep -v / | wc -l
The -p
option tells ls
to append a slash (/
) to directory names. The output is passed to grep -v
, which removes the directories.
Use the find
command instead of ls
to have more control over which files are listed.
find DIR_NAME -maxdepth 1 -type f | wc -l
The -type f
argument directs find
to only list files (including dotfiles), and the -maxdepth 1
option restricts the search to the top-level directory.
Recursively Count Files in Directory
Use the find
command to recursively count files in a directory:
find DIR_NAME -type f | wc -l
To refresh your memory, the find
command is used to look for files on your computer.
When the -f
option is used, just one file is targeted.
The find
command, by default, does not stop at the first level of the directory, it will look into every subdirectory, making the file search recursive.
For instance, if you wanted to count files in the /etc
directory recursively, you'd write the following query:
find /etc -type f | wc -l
You may not be authorised to investigate every single sub entry when recursively counting files in a directory, resulting in permission refused errors in your terminal.
You can use "output redirection" to have error messages diverted to /dev/null
by using "output redirection."
find /etc -type f 2> /dev/null | wc -l
Count Files using tree
Using the tree
command and specifying the name of the directory to be inspected is a simple approach to count files and directories.
tree <directory>
Output
4 directories, 18 files
The "tree" command is not installed by default on all hosts.
If you're seeing errors like tree: command not found
or tree: no such file or directory
, you'll need to install it with sudo
privileges.
sudo apt-get install tree (for Ubunbu/Debian hosts)
sudo yum install tree (for CentOS/RHEL hosts)
Counting Hidden Files with Tree
You might wish to count hidden files on your system in various instances.
Hidden files are not printed in the terminal output by default, regardless of whether you use the tree
, find
or ls
commands.
To count hidden files using tree
, run it with the -a
option for "all," followed by the directory to be inspected.
tree -a <directory>
If we count the files and folders in your /home
directory, we can see that there is a discrepancy because there are several hidden files.
tree /home/user
Output
1321 directories, 17047 files
tree -a /home/user
Output
7388 directories, 22633 files
Frequently Asked Questions - Count Files in Directories on Linux
Can I include subdirectories while counting files?
Yes, you can use the find
command with the -type f
option to count files recursively in subdirectories.
How can I count the number of hidden files in a directory?
By using the ls
command with the -A
option and piping the output to wc
, you can count the number of hidden files.
How do I count files of a specific file type, such as .txt files?
You can use the find
command with the -name
option, followed by the file extension, to count files of a specific type.
What if I want to count files in multiple directories at once?
You can specify multiple directories separated by a space in the command to count files in multiple directories simultaneously.
Can I count files based on their size?
Yes, you can use the find
command with options such as -size +<size>
or -size -<size>
to count files based on their size.
What if I want to count files modified within a specific time range?
By using the find
command with the -mtime
option followed by the number of days, you can count files modified within a specific time range.
How can I count files while excluding specific directories?
You can specify directories to be excluded using the -path
option with the find
command to count files while excluding those directories.
Can I count files in a directory across all users on the system?
No, the file count is limited to the permissions of the user running the command and only applies to directories accessible by that user.
Conclusion
Using the ls
, find
, and tree
commands, we demonstrated how to count files in a directory.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.