The method of counting the number of files of specified type on Linux terminal

Time:2021-4-7

Now let’s take a look at the skills of counting the number of files of a specified type in a directory with LS, grep and WC commands. The interaction between commands is accomplished through named pipes.

Grep – the user’s command to search based on a given pattern or regular expression.
WC – command for counting lines, words, and characters.
Count the number of common files

In Linux, ordinary files are represented by the symbol -.

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ ls -l | grep ^- | wc -l

7
Count the number of directories

In Linux, the directory is represented by the symbol D.

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ ls -l | grep ^d | wc -l

1
Count the number of symbolic links and hard links

In Linux, symbolic links and hard links are represented by the symbol L.

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ ls -l | grep ^l | wc -l

0
Count the number of block files and character files

In Linux, block and character files are represented by the symbols B and C.

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ ls -l /dev | grep ^b | wc -l

37

Copy code

The code is as follows:

[email protected] ~/Linux-Tricks $ ls -l /dev | grep ^c | wc -l

159