17 January 2014

This snippet shows you how to count files, folders of both in Linux terminal. This is achieved by find and wc commands. Find finds files and folders recursively and wc counts bytes, characters or lines. The first snippet demonstrates how to count directories exclusively, narrowing down the search with the -type d option. Similarly, the second snippet provides a concise method for counting only files using -type f. The third snippet takes a broader approach, counting both files and directories without specifying the type. These commands offer a versatile toolkit for programmers and system administrators, providing a quick and effective means of assessing directory structures in the Linux environment. Whether you're managing files or gaining insights into your system's composition, mastering these commands enhances your command-line capabilities.

Source code viewer
  1. # Count only directories.
  2. find DIRECTORY -type d | wc -l
  3.  
  4. # Count only files.
  5. find DIRECTORY -type f | wc -l
  6.  
  7. # Count files and directories.
  8. find DIRECTORY | wc -l
Programming Language: Bash