Practicing the cat command
I wanted to challenge myself to learning how to use the Unix CLI better, so I am attempting to write this whole article using only a few simple commands, and learning how to use them better as I go. I wanted to begin with the cat command, short for concatenate, because it is one of the most commonly used commands, and opening vim to look at a file gets a little old after awhile.
The cat command was part of version one of Unix, and was written by Ken Thompson and Dennis Ritchie at Bell Labs in the early 1970's. It was a part of their original intended version of plugging input and output into one another and creating a powerful modular system made up of simple well defined tools.
I started by how to create a file using the cat command, cat > [your file name here] then hitting the return key followed by CTRL + D, doing this will generate a new file. I then learned you can use cat >> [your file name here] and use >> to append to the file, again by pressing the return key then using CTRL + D to let the command know you are done.
This was great news because I originally thought I would be using echo the whole time to append text, and that felt like cheating, if I was going to learn how to use cat I was going to use cat.
From there I started to play. I attempted to use the tac command, which displays the file's contents in reverse order, but my Mac's OS does not offer that option, instead I had to use tail -r [your file name here]. This seems to do the same thing as tac, but is not as funny, since the command is not cat spelled backwards.
Recommended by LinkedIn
At this point I was running into a wall of text when I used cat to reread what I was writing and started piping my cat output to the less command. This can be done by using cat [your file name here] | less, it makes the file's contents scrollable and searchable, and is the opposite of using cat [your file name here] | more, which allows you to scroll and search the entire file. This also is an example of something I learned about called UUOC short for useless use of cat. In my less and more examples I was using cat to display the file, which less and more would have done for me.
I wanted to try merging files, so I created the file that this text would be input into, and am going to merge them using cat [first file name] [second file name] > [final file name]. This worked really well, but I started to understand that the cat command was not just creating an appending text when I used cat >> [file name] the blank space between cat and the >> was allowing it to accept standard input and then appending it, not opening for appending then accepting standard input.
Then I started to make some mistakes, since I was only using the cat commands I was making typos in both my text and commands so I ended up with extra files, since cat was creating them for me in my attempts to write to my pre-exisiting files, but there's a cat command for that too. If you do cat *.txt it will list all the text in your .txt files in your directory. This helps to find out what you're missing from one file to another.
This whole practice is a good example of the reason I wanted to attempt this, the CLI is powerful and I want to know it better and to do that I have to practice what it has to offer. The CLI has the tools I need and I want to reach for them more often it's easy to use a certain process to find what you need like me opening vim and searching for or reading what I want, but if I learn to use tools like cat I can do it faster, simpler, and all around better.