Count the number of files and folders/directories in Linux by manipulating commands

Hey there! Today, we're diving into a set of advanced Linux commands that can be incredibly useful for managing and analyzing your file system. Whether you're looking to count files, directories, or even track specific user-created files, these commands will help you do it efficiently. In this tutorial, we'll explore how to combine multiple commands like ls, egrep, wc, and find to perform more complex tasks.

To demonstrate, I've created a small test environment with 7 files and 2 folders, including 5 regular files and 2 hidden files. The tree command gives a clear view of the structure:

/opt
├── magi
│   └── 2g
│       ├── test5.txt
│       └── .test6.txt
├── test1.txt
├── test2.txt
├── test3.txt
├── .test4.txt
└── test.txt
2 directories, 7 files

Now let's look at some practical examples of how to count files and directories using different commands.

Example 1: Count regular files in the current directory (excluding hidden files)

# ls -l . | egrep -c '^-'

Output: 4

Explanation: This command lists all non-hidden files by checking lines that start with a hyphen (-), which indicates a regular file in long listing format.

Example 2: Count all files including hidden ones

# ls -la . | egrep -c '^-'

Output: 5

This includes hidden files because ls -la shows all entries.

Example 3: Count total files and directories in the current directory

# ls -l | wc -l

Output: 5

The wc -l command counts the number of lines, which corresponds to the number of entries listed by ls -l.

Example 4: Count all files and directories including hidden ones

# ls -la | wc -l

Output: 8

This time, it includes hidden files and directories as well.

Example 5: Recursively count all files, including hidden ones

# find . -type f | wc -l

Output: 7

The find command searches recursively and filters only regular files.

Example 6: Use tree to count files and directories (excluding hidden files)

# tree | tail -1

Output: 2 directories, 5 files

Example 7: Include hidden files in the tree output

# tree -a | tail -1

Output: 2 directories, 7 files

Example 8: Count directories recursively

# find . -type d | wc -l

Output: 3

Example 9: Count files by extension (e.g., .txt)

# find . -name "*.txt" | wc -l

Output: 7

Example 10: Count all files using echo and wc

# echo *.* | wc

Output: 1 4 39

The second number represents the number of files in the current directory.

Example 11: Count directories using echo and wc

# echo */ | wc

Output: 1 1 6

The second number is the number of directories.

Example 12: Count all files and directories

# echo * | wc

Output: 1 5 1544

Here, the second number is the total number of items (files and directories).

Example 13: Count all files in the entire system

# find / -type f | wc -l

Output: 69769

Example 14: Count all directories in the entire system

# find / -type d | wc -l

Output: 8819

Example 15: Count all types of files, directories, links, etc., across the system

# find / -type d -exec echo dirs \; -o -type l -exec echo symlinks \; -o -type f -links +1 -exec echo hardlinks \; -o -type f -exec echo files \; | sort | uniq -c

Output:

 8779 dirs
 69343 files
  20 hardlinks
 11646 symlinks

This command categorizes and counts various types of system elements efficiently.

Spring Terminal Blocks

Spring Clamp Terminal Block,Screwless Spring Terminal Blocks,Spring Conductor Terminal,Spring Clip Terminal Blocks

Wonke Electric CO.,Ltd. , https://www.wkdq-electric.com