Linux view the number of files and folders

Project github address: bitcarmanlee easy-algorithm-interview-and-practice
welcome everyone to star, leave a message, and learn and progress together

1. View the number of files in the current directory

ls -l |grep -c "^-"

In the above command, ls -lthe format displayed is

-rw-r--r--  1 wanglei  staff   1.4K Aug 12 20:13 ? extends T 与 ? super T.md
-rw-r--r--  1 wanglei  staff   6.7K Aug 12 19:34 Comparable 与 Comparator 比较.md
-rw-r--r--  1 wanglei  staff   3.3K Aug 12 15:15 Double 中的 NAN与INFINITY.md
-rw-r--r--  1 wanglei  staff   3.9K Aug 12 21:03 HashMap简单小结.md

Similar to this form, each file occupies one line

grep -c "^-"The following regular expression means to match the line starting with the "-" character, and the -c option means to count the number. This combination achieves the purpose of counting the number of files in the current directory.

2. View the number of folders in the current year's directory

ls -l |grep -c "^d"

The idea is similar to the above, the only difference is that the folder starts with the character "d".

3. View the number of all files in the folder, including subdirectories

ls -lR | grep -c "^-"

The -R option of ls, if you use man to check the information of ls, you can see the following explanation

-R      Recursively list subdirectories encountered.

4. View the number of all folders in the folder, including subdirectories

ls -lR | grep -c "^d"

5. View the number of files ending in md in the folder

ls -lR | grep "^-" | grep -c "md$"

6. View the number of files containing java in the folder

ls -l | grep -c "java"

Guess you like

Origin blog.csdn.net/bitcarmanlee/article/details/108019179