Register to get access to free programming courses with interactive exercises

Reading files CLI fundamentals

Now that we understand the general structure of the file system in POSIX-compliant operating systems, it's time to start interacting with files. In this lesson we will look at different ways to read text files.

The easiest way to read a file is with the cat command.

man cat

NAME
     cat -- concatenate and print files

SYNOPSIS
     cat [-benstuv] [file ...]

...

cat expects arguments, which are the paths to the files to be read. In the simplest case it is enough to pass a single path, which is quite handy when you want to see the contents of a small file:

cat .bash_logout

# ~/.bash_logout: executed by bash(1) when login shell exits.

# when leaving the console clear the screen to increase privacy

if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

Sometimes you only need to see the beginning or end of a file. The commands head and tail will help in that situation. They also take the path to the file as input, only head shows the first 10 lines and tail shows the last 10 lines. This behavior can be changed by specifying the desired number of lines using the -n option.

Head

head -n 2 .bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)

Tail

tail -n 2 .bashrc

  fi
fi

Some files are filled very intensively. For example, logs. Logs are text files that contain information about the processes taking place in a particular system. Under Linux, the logs are stored in the /var/log directory. Both Linux itself and most programs installed on it put their logs there. The main Linux log file is called syslog. Key information about the processes in the operating system and errors that occur goes there. If events are frequent and you need to analyze them as they come in, the cat command will prove inconvenient because syslog is so large. Just running the tail command is no good either, because you'll have to run it every second and you might miss something anyway (if new records are appearing quickly).

In this situation tail, started in a special mode using the -f flag, will be of use. The command tail -f path/to/file doesn't just print the last lines of the file but also waits for new lines to appear. As soon as a file is modified, tail will immediately display the added lines. Among our examples, this is the first command that seizes control, i.e., after running it doesn't then immediately stop running, but rather carries on running, waiting for new data in the file to output. To stop it, press Ctrl + C.

# You probably don't have enough rights to see the syslog file to access it,
# type sudo before the tail command as shown below, the following lessons will discuss this issue in more detail
# There is a chance that sudo will ask you for your password. Do this and then press Enter. When typing the password,
# the cursor will not move, this is done for safety.
sudo tail -f syslog

Aug 28 18:00:01 ip-10-0-1-223 systemd-udevd[15400]: Could not generate persistent MAC address for veth5c6ed9c: No such file or directory
Aug 28 18:00:01 ip-10-0-1-223 kernel: [126412.013499] device veth6969122 entered promiscuous mode
Aug 28 18:00:01 ip-10-0-1-223 systemd[1]: Starting Update resolvconf for networkd DNS...
Aug 28 18:00:01 ip-10-0-1-223 systemd-timesyncd[522]: Network configuration changed, trying to establish connection.
Aug 28 18:00:01 ip-10-0-1-223 systemd-timesyncd[522]: Synchronized to time server 91.173.73.198:123 (ntp.ubuntu.com).
Aug 28 18:00:01 ip-10-0-1-223 sh[15415]: sed: cant read /run/systemd/netif/leases/*: No such file or directory
Aug 28 18:00:01 ip-10-0-1-223 kernel: [126412.086162] IPv6: ADDRCONF(NETDEV_UP): veth6969122: link is not ready
Aug 28 18:00:01 ip-10-0-1-223 systemd-udevd[15403]: Could not generate persistent MAC address for veth6969122: No such file or directory
Aug 28 18:00:02 ip-10-0-1-223 systemd-timesyncd[522]: Network configuration changed, trying to establish connection.
Aug 28 18:00:02 ip-10-0-1-223 systemd-timesyncd[522]: Synchronized to time server 91.173.73.198:123 (ntp.ubuntu.com).
Aug 28 18:00:02 ip-10-0-1-223 systemd-timesyncd[522]: Network configuration changed, trying to establish connection.

There is a special category of programs for viewing file contents called pagers. A pager is like a text editor, but it only opens in read-only mode. The most common pager is called less. Let's try to open the syslog file with it:

# we have to use sudo again
sudo less syslog
# there's a lot to output here

less opens the file and stays in this mode. It allows you to move forward and backward through the file and search. One of the distinguishing features of pagers is that they work equally well and quickly with files of all sizes. This is because the pager doesn't try to load the entire file into memory before displaying it. It only loads the part that fits on the screen and loads the rest when you move through the file.

less provides several dozens of commands to move and search through the text, most of them can be found in the corresponding manual. Here we'll touch on the main ones:

  • q — quit
  • f — page forward
  • b — page backward
  • if you type /, then start typing letters and press Enter, it will search for the text you typed. Move through the matches found by the n command (move to the next match) and N command (move to the previous match).

You may have seen similarities in terms of the behavior of pagers with what we observed in the manuals. Let me tell you a secret: when we launched man, it opened less with the content loaded into it. As you will see later, pagers are incredibly popular and quietly run by other programs.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY