Basic Linux document viewing

Basic Linux document viewing

Basic Linux document viewing

Quick commands to view config files or just plain text files… or any file really.
First in the list of basic linux document viewing would be cat.
If we want to view the network config file on a Debian/Ubuntu system

$ cat /etc/network/interfaces

You will get the following output:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet static
        address 192.168.10.229
        netmask 255.255.255.0
        network 192.168.10.0
        broadcast 192.168.10..255
        gateway 192.168.10..1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 192.168.10.2
        dns-search windohs.co.za

Now the problem with using cat on it’s own, if you’re looking at a big file then it will just output everything and you have a huge mess.
To get around that you will have to pipe to another command like less or more.
Instead of expanding and adding more commands in one line you can just use less or more on it’s own

$ less /etc/network/interfaces

You will be able to scroll up and down and find specific strings in there, all you need to do to exit is press Q
With the more command it’s a combination between cat and less. It outputs to the terminal and works like less. Also just press Q to exit.

$ more /etc/network/interfaces

Now there’s two more that I want to cover, these are more for just viewing pieces of a file and not the entire thing.
First there’s head which when used on it’s own will give you the first 10 lines of a file and output it to the terminal.

$ head /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet static

If you want to see more or less lines then you just add a -n and replacing the “n” with the number of lines that you wish to view.

$ head -5 /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo

There you have the first 5 lines of the /etc/network/interfaces file
Next we have tail, it’s the opposite of head. It will give you the last 10 lines of a file when used on it’s own but can also be used the same with -n, again replacing “n” with the number of lines that you wish to view.

$ tail -5 /etc/network/interfaces
        broadcast 192.168.10..255
        gateway 192.168.10..1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 192.168.10.2
        dns-search windohs.co.za

Tail has another very useful option to use is -f (follow).
This is particularly useful when tailing logs for errors in real-time.

$ tail /var/log/apache2/error.log

There’s also tac, which does the same as cat but in reverse… not sure what use that would be to anyone.

Leave a Reply

Your email address will not be published. Required fields are marked *