Michael O.
4min Read

Linux Basics – head, tail, cat and more (or less)

Linux Basics - head, tail, cat and more (or less)

The following are all BASH commands that are in daily use for most admin tasks. Need to extract a list of email addresses from a Linux server log file or a list of domains from a zone file list? These tools can do all of this and more. Let us dig in and have a look at what we can accomplish. Linux basics are the foundation of good Linux Administration and Engineering. Don’t forget to download our Linux commands cheat sheet and read our grep command in Linux guide.

Some Basics first

Let us look at some redirectors and pipes. First of all, the PIPE symbol: This vertical bar is used to take the output from a command and apply another action to it. Let us look at an example:

# DO A DIRECTORY LISTING AND PIPE INTO THE tee COMMAND WHICH WILL ADD IT TO A FILE AND SIMULTANEOUSLY DISPLAY THE OUTPUT
ls /home |tee -a /tmp/dirlist.txt

Now we can look at the > and < symbols. We will use the word TARGET to represent our file. The > TARGET symbol is used to redirect output INTO our file or device (not a command) and the < TARGET is used to extract content from the file into the preceding command. Each has its own use case. Let us see some examples:

# PRINT CONTENT OF A FILE TO SCREEN BUT REDIRECT TO ANOTHER FILE
cat /tmp/dirlist.txt >/tmp/homefolders.txt
# FOR EACH ITEM IN A FILE DO SOMETHING (REDIRECT FROM FILE TO LOOP)
while read HOME
do
  echo $HOME
done </tmp/homefolders.txt
Head, Tail, Cat

Our next Linux Basics commands all display the contents of a file in different ways. The head command is used to show a file from the beginning (Top or Head of file). Here are a few examples:

# DISPLAY THE FIRST 5 LINES (default number) OF /var/log/messages
head /var/log/messages
# DISPLAY THE FIRST 100 LINES OF /var/log/messages
head -n 100 /var/log/messages  # ALTERNATIVE -> # head -100 /var/log/messages

The tail command is used to show a file from the end (Tail). The added bonus switch here is the follow (-f) switch which allows you to watch any file, but usually log files, as lines get added to it in real time! A few examples of tail usage:

# DISPLAY THE LAST 5 LINES (default number) OF /var/log/messages
tail /var/log/messages
# DISPLAY THE LAST 100 LINES OF /var/log/messages
tail -n 100 /var/log/messages  # ALTERNATIVE -> # tail -100 /var/log/messages
# FOLLOW THE END OF /var/log/messages AS IT GETS WRITTEN IN REAL TIME
tail -f /var/log/messages

If you add the head and tail together, you get a cat (Linux humour). Thus, to display a whole file’s content, we use the cat command. This is actually also an abbreviation of the word ‘concatenate’. You can also use the cat command to print out many files and possibly stream that into a single file.

# DISPLAY THE CONTENT OF THE HOMEFOLDER LIST GENERATED IN THE FIRST EXAMPLE
cat /tmp/homefolders.txt

# DISPLAY THE CONTENT OF THE HOMEFOLDER LIST GENERATED IN THE FIRST EXAMPLE, SORT IT NAD REMOVE ANY DUPLICATES, GIVING A COUNT OF OCCURENCES 
cat /tmp/homefolders.txt |sort |uniq -c
Less and More

The less command can be confusing, but only because it has many options and it captures the shell while it is running. It is used to display the content of a file, much like the cat command. The main difference is that you can move through the content and even search for a specific word within this content. With this command, we cannot give a good example as you just type ‘less /tmp/homefolders.txt‘, but once you have done that, you see one page of data, not all the data as you do with the cat command. Also, you are now almost ‘locked’ into the data content. While less is active, you can move through the data with arrow keys, PgUp and PgDn, Home and End keys. To search for something, press the forward slash /. Then type in your search term and press ENTER. To exit from less, press for quit.

The less command has a twin command called more. What more does is to limit the flood of output from a cat command or a long output from a directory listing, to ONE PAGE at a time. To see more, you press the spacebar. The more command is used in conjunction with the pipe | symbol as follows:

cat /var/log/messages |more

In our next episode of Linux Basics, we will examine the grep, sed and awk commands.

hostafrica logoHappy Hosting!


The Author

Michael O.

Michael is the founder, managing director, and CEO of HOSTAFRICA. He studied at Friedrich Schiller University Jena and was inspired by Cape Town's beauty to bring his German expertise to Africa. Before HOSTAFRICA, Michael was the Managing Director of Deutsche Börse Cloud Exchange AG, one of Germany's largest virtual server providers.

More posts from Michael