Andrew Fletcher published: 1 April 2023 1 minute read
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 Fletcher
•
04 Jun 2024
Checking if a file exists using command line queries in Ubuntu with Nginx
On a server directory with over 100,000 PDF files, I needed to verify the existence of specific documents. Whether there are a few hundred or whatever the number of files, this article explores the practical use of command-line tools to quickly ascertain whether a file is present. In the area of...
Andrew Fletcher
•
20 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. Following is the approach we took. Real-Time MonitoringYou can set up a script to monitor the /tmp directory and log the...
Andrew Fletcher
•
09 May 2024
Efficiently managing large file archives in Linux: excluding directories with Zip
Managing large directories, especially those filled with a diverse mix of media such as documents and images, can quickly become cumbersome. In my latest project, dealing with a directory that totals a whopping 87GB, the need for efficient file compression and selective archiving became apparent....