Generally, if you’re working with Linux then you’re working in terminal. It is incredibly important to understand how to read files while working in terminal, and Linux has a few nifty options. What you should do really depends on the situation and size of the file. A few of the basic functions you will use a lot are Cat, Head, and Tail. The cat utility can do two things: output a single file to terminal, and combine files. There are formatting options too, but I did not go through them at all for the video. You can pull up the man page for any utility with the option “–help,” and I highly recommend building that habit. Knowing where to find good information is as important for programming as memorization or any other common skill you develop. The video below provides a demonstration and audio explanation of cat.
If I sound British or Canadian it is entirely due to speech therapy I took as a kid, not the fact I’m secretly not an American. Anyway, all I did in the video was use the “cat” or “concatenation” utility to output two files separately. Then I decided to concatenate, or combine, the files into a single output by using each file as an argument of a single cat call. The result is that they were combined in the output in the order in which they were called. This can be useful for little things like combining logs of different dates or different files on the same subject. I also made a video to briefly demonstrate the Head and Tail utilities.
In this video I use the Head and Tail function to show how you can read the first or last n lines in a file respectively. The head utility is great for when the log should have your desired value within the first n lines. By default, it will output the first ten lines. However, if the file has fewer than ten lines it will output the entire file. Furthermore, you can choose to output more or less than 10 lines using the “-n” flag with the number of lines you would like to use as I do above. The tail utility is similar, except it starts from the bottom and counts upward. You can use negative values to change the behavior of head or tail, but I would recommend keeping it simple.
These are by far the three most common commands for processing text files in the terminal. They are incredibly useful when you need to examine a file without risking altering the file. I recommend getting used to using them in the terminal, and going through their respective man pages to see all of their capabilities. I am very sorry I have fallen off schedule far as content goes, and I am trying to get back on track to some degree. Thank you!
If you’re interested in more common Linux BASH commands, there is a full article available here.