Skip to main content

How to count the number of files in a directory.  At some point you'll have a directory or directories that you need to know the number of files in them.  On Linux, the list command (ls) is piped with the wc -l command.

ls | wc -l

This command works great if all the files are located in one directory.  What about if you have multiple directories.  Then you will need to use the find command piped with the wc command

find <directory> -type f | wc -l

This command only files the files and not the directories.  Perfect for me.  But if you also need to count with directories

find <directory> | wc -l

 

Related articles

Andrew Fletcher20 May 2024
Create a copy of files that go to the tmp directory
To review the content of files being generated in the /tmp directory on an Ubuntu server before Microsoft Defender removes them, you can use several approaches. &nbsp;Following is the approach we took.&nbsp;Real-Time MonitoringYou can set up a script to monitor the /tmp directory and log the...