Description
The Counting number of files part I and II exercises in the "Counting and Mining in the Shell" episode can be easily done by the command ls | wc -l since the total is not included in an ls command. When we use the ls -l | wc -l command a line total in the ls -l output throws the count off by one. We then come back to this exercise later in the episode to fix this "one more" problem with grep, though all this is really unnecessary. This was pointed out to me by a student the last time I taught this lesson. Maybe we don't need this grep exercise, or another one can be used that might be better. Below are the exercises I am talking about.
Counting and mining in the shell episode challenge:
Counting number of files, part I
Let’s make a different pipeline. You want to find out how many files and directories there are in the current directory. Try to see if you can pipe the output from ls into wc to find the answer, or something close to the answer.
Solution
You get close with
$ ls -l | wc -l
but the count will be one too high, since the “total” line from ls is included in the count. We’ll get back to a way to fix that later when we’ve learned about the grep command.
Counting number of files, part II
In the earlier counting exercise in this episode, you tried counting the number of files and directories in the current directory.
Recall that the command ls -l | wc -l took us quite far, but the result was one too high because it included the “total” line in the line count.
With the knowledge of grep, can you figure out how to exclude the “total” line from the ls -l output?
Hint: You want to exclude any line starting with the text “total”. The hat character (^) is used in regular expressions to indicate the start of a line.
Solution
To find any lines starting with “total”, we would use:
$ ls -l | grep -E '^total'