UNIX is a base OS for many popular OS you are using right now, from Linux to Mac OSX. Those OS share common commands so it is pretty easy to switch from one OS to another. Because it is so important to understand and use UNIX commands, I want to create a series to introduce to you some of the most frequent used UNIX commands which I think are extremely helpful for your daily tasks. I cannot imagine how hard it would be if these tools do not exist.
I do not want to feed you many fishes at a time. Let's get started with just 3 commands that I have to type almost every day. All of 3 commands are used for showing the content of a file but each has different usage.
cat is used when you want to display the entire content of a file, no matter how big it is. Whenever I want to look into a text file (like the content of my SSH key), this is the command I use. It is, however, not suitable to be used when the content of the file is too big to be displayed nicely on the screen. Because it tries to show the entire content, it floods your screen with characters and it is really hard to see (you have to scroll backward to see what is in the beginning of the file). In addition, you can not use it for searching a particular token in a file. Rule of thumb: use
cat if your text file is small and you can read it easily without scrolling too much. For example, try running:
cat /var/log/system.log and see how it floods your terminal.
Another nice ability of
cat is to concatenate a list of files into a single file. This can be seen when setting up SSL certificate, it often asks us to concatenate a private key and an intermediate key into a single
.pem file. This can be done easily using the following command:
cat private_key intermediate_key > final_key.pem
You can merge as many files as you want, just specify them in the command.
less is an enhancement of an old tool called
more. Even though its name is
less, it actually has more features than
more. It allows you to read the content of a file page-by-page instead of showing them all on the screen like
cat does. The term
page is defined as the amount of text which fits nicely in your terminal. This means that it has no problem working with a big file since it only fetches enough text to show for the user. Navigation is easy, you can use arrow key or other keys combination (
Ctrl + V for page down,
Ctrl + B for page up). So, try running:
less /var/log/system.log and see the difference.
In addition, you can also search a particular text inside a text file while viewing a file using
less. Just type
/ (slash) then type your searching text and finally press
Enter, less will jump to a page which contains the text you are looking for.
tail is the last command I would like to introduce in this list. It is the command which often used to view real-time log. It allows you to view the content at the end of a file, which is the most valuable information of a log file. You can specify how many lines of text you want to read using
-n option, and you can also use
monitoring mode which shows all updates made to a file. When I debug a Rails application,
tail is my choice:
tail -f log/production.log
Even though these 3 commands are so simple, they are quite powerful if you understand how to use it correctly. To learn more about these commands, I recommend using
man command to read their documentation of each system, ex:
man tail. Remember that if you want to be good, start with simple thing first. Do not try to learn fancy & complex tools right at the beginning to show off your skills. Make sure you get the fundamental things first and enhance it by the time.
I'll see you next time with more commands and explanation. Happy coding!