File Descriptors and Redirections - I'm flying further.


This chapter of the training somehow oddly corresponds to the results of tasks from the previous chapter. It discusses functions that I've already had to use to solve those tasks. Admittedly, it touches on a few interesting concepts worth acquainting oneself with. And they will certainly come in handy when starting to write scripts in BASH.

Okay - what will surprise me today?

1. How many files exist on the system that have the ".log" file extension? 

This task is very simple to execute - it's just a precise repetition of what I learned while completing tasks from the previous chapter.

 find / -type f -name *.log 2>/dev/null | wc -l

Since most of you probably have no doubts about what commands of this syntax deal with, I'm posting a detailed description of how they operate.

This command performs the following tasks:

1. `find / -type f -name *.log`: This part of the command starts at the root directory ("/") and searches for files (`-type f`) with names ending in ".log" (`-name *.log`). It will search the entire file system starting from the root directory. The `*` is a wildcard that matches any characters in the file name.

2. `2>/dev/null`: This part of the command redirects any error messages from the `find` command (such as permission denied errors) to `/dev/null`, a special device file that discards any data written to it. This ensures that error messages are not displayed on the terminal.

3. `|`: This symbol is a pipe, which takes the output of the command on its left and uses it as input for the command on its right.

4. `wc -l`: This command counts the number of lines in its input. Since the `find` command outputs a list of file paths (one per line), `wc -l` will count the number of files found with the ".log" extension.

So, in summary, the entire command finds all files with the ".log" extension in the file system, discards any error messages, and then counts the total number of such files found.

I'm glad I managed to satisfy the curiosity of the most inquisitive readers :D

2. How many total packages are installed on the target system? 

 Here I had to search a bit for the answer to this question. I had the option to use both APT and DPKG, and both are excellent for counting installed packages.

a) DPKG - In the case of dpkg, the solution looks like this: 

dpkg -l | grep ^ii | wc -l

b)  APT - The solution is very similar to that with apt, essentially considering the differences in displaying results and using grep here. The rest looks the same.

apt list --installed | grep "installed" | wc -l

 Until the next episode!







Popular posts from this blog

Task Scheduling and System Services: A Theoretical Overview

Three tasks related to user management.

Working with Web Services.