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.
Similar Programs Chapter Last Updated
How To Enable Permission On File Linux Ubuntu Linux Commands 26-08-2023
Linux Command To Give All Permissions To A File Linux Commands 24-08-2023
Important Network Commands In Linux Linux Commands 24-08-2023
Important Commands In Linux Linux Commands 24-08-2023
Grep Command In Linux Example Linux Commands 10-05-2023
Traceroute Command In Ubuntu Linux Commands 10-05-2023
Linux Version Command Linux Commands 01-12-2020
Unzip Command In Linux Linux Commands 20-11-2019
Process Command In Linux Linux Commands 16-11-2019
Linux Command To Create Link Linux Commands 15-11-2019
Linux Command To View File Content Linux Commands 25-10-2019
Linux Command To Check OS Version Linux Commands 25-10-2019
Netstat Command In Linux Linux Commands 13-08-2019
Telnet Command In Linux Linux Commands 13-08-2019
Linux Command To Create A File Linux Commands 23-05-2019
Linux echo Command Linux Commands 05-05-2018
Linux Ping Command Linux Commands 05-05-2018
Linux ls (List Directory) Command Linux Commands 17-03-2018

1