Reading Files from the Command Line

When working on your server, there will be many occasions in which you will need to read files saved on the file system. These could be config files, README files, log files or scripts. Basic server administration means being able to open files and read what is saved to your system. If you are working with a GUI, you are most likely familiar with clicking into various folders just to see what is in them, and opening up various files with a text editor to read through their contents. It’s kind of computer curiosity fundamentals. The same is possible, indeed necessary, when working from the command line.

Linux comes with a number of tools for viewing file contents. It needs to be stated, though, that only files containing text can be read. This may sound obvious, but bear in mind that there are many different types of file within a Linux files system: text files, binary files, link files, device files. Contrary to Windows computers also, file suffixes, that is the .txt, .doc, .exe part of the file name, are not always present.

Moving Between Directories

In order to read a file, such as a log file, it is obviously important to get to it. This involves navigating to the directory containing the file(s) and seeing a list of what files are present there. The three commands you will use extensively in your linux command line lifetime, for navigating the folder structure, are pwd, cd and ls. pwd tells you which directory you are currently in. cd is used to change directory, that is move into or out of directories, and ls is used to list the files within that directory.

In the example below, I am starting in /home/jupiter/test. ls –l shows, in long format what is in that folder. As there are two more folders, use cd to move into one of them, and ls –l again to see the contents. When finished in here, use cd .. to move back out of the folder.

While pwd and cd are very simple commands with a straight forward purpose, ls has a number of flags and options which will affect how the listing of folder contents is displayed. I will cover this command in a future article.

Reading Files

So how can you tell when a file can be read? The easiest way, if there is no obvious file suffix such as .conf for config files, is to use the file command. This command will examine the file you are running it against, to determine the file type. This will tell you whether it is an ASCII text file, which you can open and read, or another file type. In the example below, file is being used against three different file types: the ls command itself, which is an executable file, a text file and a directory.

There are four utilities you will most likely use most often in reading text files: head, tail, cat and less. Each has a specific purpose which we will cover below.

head
head is used to show the first few lines of a file. By default this is 10 lines but this can be changed using the –n flag: head –n 5 <filename> would show just the first 5.

tail
tail is used to show the last few lines of a file; by default this is 10 lines, but this can also be changed using the –n flag: tail –n 5 <filename>, would show the last 5 lines.

tail also has another useful flag, -f. Using the –f flag, tail stays running and will ‘follow’ the file you are reading. If any new entries are made to the file you are reading, such as in the case of a log file, tail will display them as soon as they are added

cat

The cat command has many uses, and when used against a text file, it will print the content of the file direct to the command line, and then return you to a command prompt. This is most useful for when you want to view the content of a file quickly. The command structure is simple:

cat <filename>

There are also a number of flags you can use which will modify the output of the command:

[table th=false]
-n[attr style=”width:50px”],cat –n <filename> will number each line of the output
-s,If a file has numerous empty lines\, running cat –s <filename> will squeeze multiple empty lines down to just one
[/table]

less

The other common command for viewing text files is less. When you run less against a file, you go into the less interface. You can scroll up and down through the text, jump to the beginning or end of the file and perform text searches through the text. less is very useful for browsing through longer text files. The man pages, which provide info on all commands, use less as their file reader.

To use less against a file, simply run:

less <filename>

To navigate through the file, use the following keys:

[table th=”false”]
Exit
q[attr style=”width:100px”],quit
Navigation:
Up/Down arrow keys,move up/down one line at a time
Return key,move down one line at a time
Spacebar,jump down a page
b,jump back up a page
g,jump to start of file
G,jump to end of file
Searching:
/,start search
n,move to next search result
N,move back up to previous search result
[/table]