Advanced Pipeline Example
Learn advanced techniques of applying pipelines.
We'll cover the following...
du
Let’s go over a more complex example with pipelines and the du utility. The utility evaluates disk space usage. We run it without parameters in the current directory. The utility passes through all subdirectories recursively. It prints the space occupied by each of them.
Traversing a directory recursively means visiting all its subdirectories. If any of them has subdirectories, we should visit them, too. The traversal algorithm looks like this:
-
Check the contents of the current directory.
-
If there is an unvisited subdirectory, go to it and start from the 1st step of the algorithm.
-
If all subdirectories are visited, go to the parent directory and start from the 1st step of the algorithm.
-
If it is impossible to go to the parent directory, finish the algorithm.
We should select a starting point for this algorithm. It’s a specific file system path. Then, the algorithm traverses all subdirectories starting from this path. The traversal finishes when the algorithm reaches the parent directory of the starting point.
We have considered the universal traversal algorithm. We can add any action to it to process each subdirectory. The du
utility calculates disk space usage.
The algorithm of the du
utility looks like this:
-
Check the contents of the current directory.
-
If there is an unvisited subdirectory, go to it and start from the 1st step of the algorithm.
-
If there are no subdirectories: ...