Home>Article>Operation and Maintenance> How to count the number of files in a directory in Linux
What should you do if you want to know how many files there are in a directory in Linux? The following article will take you through the method of calculating the number of directory files in Linux. I hope it will be helpful to you. [Video tutorial recommendation:Linux tutorial]
In Linux, we can use the ls command and the wc command to count the number of files in a directory.
The ls command is used to list the directory contents; the wc command is used to count the number lines and characters in the file. It can be used with -l to count the number of lines.
To count the number of files in a directory, you need to use the following syntax:
#ls -1 | wc -l
Let’s break down the syntax and see what it does
● ls: List files
● -1: This is a. It prints one entry per line. To print hidden files, change it to -1a
● | : pipe output to...
● wc : count word count
● -l : display only Number of lines
Example:Let’s take a look at how these commands work through an example.
Create a new directory test_folder and navigate to it.
#mkdir test_folder && cd test_folder
Next, we will create some text files
#touch file1.txt file2.txt file3.txt file4.txt file5.txt
Let us confirm the existence of the files by listing them using the ls command
#ls -l
Output:
We use the wc command ls command to calculate the number of files in the directory:
#ls -1 | wc -l
Output:
5
The above is the entire content of this article , I hope it can be helpful to everyone’s study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of How to count the number of files in a directory in Linux. For more information, please follow other related articles on the PHP Chinese website!