How to see how many files or directories are in a Linux directory
Linux has different methods of determining how many files and directories are in a Linux directory. Below we have listed a few different methods of how you can display the number of files or directories in Linux.
Use the tree command
The tree command is an easy and quick way to not only get a list of the files and directories, but to get a report with how many files and directories there are. Keep in mind if tree is run without any additional options that it will not only list the amount of files and directories in the current directory, but all subdirectories as well.
If you want to list only the contents of the current directory, you can use the below command.
tree -i -L 1
- See the tree command page for additional information about this command as well as other available options.
Use echo in combination with wc
Users can also see how many files or folders are in a directory by using the echo command in combination with the wc command. Below are few different examples of how this can be done.
Get a count of all files and directories in the current directory
echo * | wc
Once the above command has been typed you will get an output similar to the example below. In this example, the "10" indicates the amount of directories and files in the current directory.
1 10 104
Get a count of only the directories in the current directory
echo */ | wc
Get a count of just the files in a directory
Note: This example is assuming all the files in the directory have extensions. If a file does not have an extension it would not be counted.
echo *.* | wc