DEV Community

Cover image for  Linux Terminal Commands With Cowsay
dotbehrens
dotbehrens

Posted on

Linux Terminal Commands With Cowsay

This is an extension to my previous blog: Linux Terminal Commands.
Throughout this blog I will be using the extension cowsay. This extension is available on both mac and linux.

To install cowsay type in cowsay to your terminal and follow the directions. If that is unsuccessful here is a link to find the version of cowsay you are looking for.

https://pkgs.org/download/cowsay

All cowsay does is prints the text you tell the cow to say in a lovely little speech bubble. It is super cute.

Alt Text

first lets learn about pipes. Pipes are a single vertical line that lets the computer know that the outcome of the previous command is the input of the next command.

To demonstrate I will use another fun extension called fortune. When I type fortune into the console a fortune is returned.

Alt Text

Now what if I wanted to be told my fortune by a cow. Well then I could probably use a pipe.

fortune | cowsay

A pipe will let your function know that the output of what comes before the pipe is meant to be the input of what comes after the pipe. The output of our fortune ( the fortune) will be what the cowsays.

Alt Text

For the next part we are going to venture a little further into cowsay. I you type in the command:

cowsay -l

you will get back a list of the different cowsay options.

Alt Text

To use the options in cowsay, simply use the command:

cowsay -f <option> <whatcowsays>

Alt Text

Now lets think about using grep. "grep" will search through text to find matching characters say we want to find all of the letter k's in the list of cowsay options to do this we would use :

cowsay -l | grep k

The output of cowsay -l becomes the input for grep.

Alt Text

Lets say we wanted only to find the whole word koala. We would then use the command:

cowsay -l | grep -w koala

Alt Text

The -w searches for the whole word if we were to use the command:

cowsay -l |grep -w koal

nothing would be returned.

Personally my terminal is a little cluttered right now. Use the command




to clear the terminal and start with a clean slate.

If you would like to make your cow more vocal try the command:



```espeak <words> | cowsay <words>```



Be sure to turn up your computer volume.

Now that we have some experience with pipes and grep, lets try and figure out what the command below is telling the computer to do.



```history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10```



As we know history will print out the previously used commands, but what is awk? Good question

The time has come for me to introduce you to another one of my favorite terminal commands "whatis". "whatis" is wonderful as it tells you what a command is. This may keep you from doing all sorts of horrible things to your computer. Try the command:



``` whatis whatis```



![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8yqr100gos1hya4aljfl.png)

So what is awk? "awk" is a pattern scanning programming language. We can then deduce that we are looking for the things within history that match the pattern. The {'print $2'} is dealing with the fields we are putting into the command. For the most part lets ignore that complexity for now. 
We then take the outcome and sort it, then look for the unique values, then sort it again by a different criteria. Then we will ask for only the top ten or ten items from the head. 

We can test it to further figure out what this command is doing now that we are fairly sure it is not going to corrupt all of our files. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/lsvc787xtptup6u9sbiw.png)

As you can see the command prints out our most used commands and how many times we have used them. Handy? Maybe.. I don't know, but can a cowsay it?

Discussion (0)