Hey everyone, today we're diving into a set of powerful Linux commands that can help you manage and analyze files and directories efficiently. Whether you're trying to count files in a directory, perform recursive counts, or filter by user-created files, these commands are essential for system administrators and developers alike.
In this tutorial, we'll explore how to use multiple commands together to perform advanced file operations. We'll be using the ls
, egrep
, wc
, and find
utilities. These tools can be combined in various ways to achieve precise results.
For our experiment, I’ve created a total of 7 files and 2 folders (including 5 regular files and 2 hidden files). The following tree
command output clearly shows the structure of the directory:
# tree -a /opt
/opt
├── magi
│ └── 2g
│ ├── test5.txt
│ └── .test6.txt
├── test1.txt
├── test2.txt
├── test3.txt
├── .test4.txt
└── test.txt
2 directories, 7 files
Example 1
Count non-hidden files in the current directory. This command will count only regular files, excluding hidden ones.
# ls -l . | egrep -c '^-'
4
Detail:
- ls
: Lists directory contents.
- -l
: Uses long listing format.
- .
: Refers to the current directory.
- |
: Pipes output to another command.
- egrep
: Filters lines matching a pattern.
- -c
: Counts matching lines.
- '^-'
: Matches lines starting with '-', which represent regular files in ls -l
.
Example 2
Count all files, including hidden ones, in the current directory.
# ls -la . | egrep -c '^-'
5
Example 3
Count all files and directories in the current directory.
# ls -l | wc -l
5
Detail:
- wc
: Counts lines, words, and bytes.
- -l
: Counts lines.
Example 4
Count all files and directories, including hidden ones.
# ls -la | wc -l
8
Example 5
Recursively count all files, including hidden ones.
# find . -type f | wc -l
7
Detail:
- find
: Searches for files in the directory tree.
- -type f
: Matches regular files only.
Example 6
Use the tree
command to count files and directories (excluding hidden files).
# tree | tail -1
2 directories, 5 files
Example 7
Use the tree
command with hidden files to count all files and directories.
# tree -a | tail -1
2 directories, 7 files
Example 8
Recursively count all directories, including hidden ones.
# find . -type d | wc -l
3
Example 9
Count files by extension, such as all .txt
files.
# find . -name "*.txt" | wc -l
7
Example 10
Use echo
and wc
to count all files in the current directory.
# echo *.* | wc
1 4 39
Example 11
Count all directories using echo
and wc
.
# echo */ | wc
1 1 6
Example 12
Count all files and directories in the current directory.
# echo * | wc
1544
Example 13
Count all files in the entire system.
# find / -type f | wc -l
69769
Example 14
Count all directories in the entire system.
# find / -type d | wc -l
8819
Example 15
Count all types of files (files, directories, hard links, symbolic links) 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
8779 dirs
69343 files
20 hardlinks
11646 symlinks
The high-power axial fan support "blow" or "pump" installation method.The rainproof function of the ventilating cascade can prevent the machine mechanical damage, and prevent the entry of moisture. With the dense Enclosure protection category, it can reach IP54 according to EN60529/10.91.Easily to replace a new fiber by moving the cascade
Industrial Filter Fan Mechanical ,Fan Plastic Filter,Plastic Filter Fan,Samll Fan Filter
Wonke Electric CO.,Ltd. , https://www.wkdq-electric.com