Linux Command To Count Number Of Files In A Directory
Chapter:
Linux Commands
Last Updated:
18-03-2023 14:49:19 UTC
Program:
/* ............... START ............... */
# Below command will give the count of file in directory.
ls -1 | wc -l
# Note that this command will count all files, including hidden files
find . -maxdepth 1 -type f | wc -l
/* ............... END ............... */
Notes:
-
To count the number of files in a directory using the Linux terminal, you can use the ls command along with the wc command:
- The ls command lists all the files in the current directory, and the -1 option ensures that each file is listed on a separate line. The output of ls is then piped (|) to the wc command, which counts the number of lines in the output. Since each file is listed on a separate line, the number of lines returned by wc is equal to the number of files in the directory.
- Note that this command will count all files, including hidden files (those starting with a dot .) and directories. If you want to count only regular files (i.e., not directories or other types of files), you can use the find command (Mentioned above).
- This command searches for files (-type f) in the current directory (.) and returns only files that are at the maximum depth of 1 (i.e., not in subdirectories). The output of find is then piped to wc, which counts the number of lines as before.