In this section, we will learn all the important tools to print out text file content on the command line. We'll also learn how to view text files using a text file viewer. In Linux, there exists two different basic file types, text files and binary files. Text files are configuration files, while binary files can be image files or compressed data files. The files' encoding defines whether a file should be treated as a text file or binary file. Text files normally use UTFR. On Linux, text files normally are encoded using UTF-8 or ASCII. You can use the file command to detect the file type, like:
file /etc/passwd file ~file4.tar.gz
To print out a text file's content, you can use the cat command. cat stands for concatenate, that's also the reason where the command has its name from. So, let's concatenate some files and put the results in a new file by redirecting stdout:
cat /etc/passwd /etc/grp /etc/services > /tmp/concatenated-file
This line concatenates the three files passwd,group, and services to a new file called concatenated-files in the /tmp directory. Sometimes using cat to print out the whole file's content is pure overkill. If we are only interested in some lines at the beginning or end of the file, we can use the head or tail commands instead. The beginning of a file is also sometimes called the file header, while the end of a file is also called the file footer. To display the first 10 lines of our new concatenated file, use:
head /tmp/concatenated-file
Alternatively, if you are only interested in the last 10 lines of our new file, use instead:
tail /tmp/concatenated-file
To change head and tail default behavior of printing the first 10 lines use the -n option. Head and tail have other very useful options, use the manual pages to learn more. A more important and often used feature is to use the tail follow option. For example, using the follow option with the root account, the -f flag keeps the tail command open and tail will listen for new file content constantly and outputs it if new text is appended to the var/log/messages file. If you need a live view on a file which gets written to permanently and in real-time, this command needs to be memorized. To close the tail program, use Ctrl + C:
su root -c 'tail -f /var/log/messages'
Now, to read a file's content the cat command can be used for smaller files. For bigger files, it's better to use a real text viewer program such as less, which has some powerful features such as searching, scrolling, and displaying line numbers. It's also very useful to learn how to navigate text files using the less command, as a lot of Linux commands are using less, also called lesser navigation, to browse text content for the page or settings, as we will see later. To open a file using less, you can use less and then the filename as an argument. You can also directly use stdout unless using pipes, which is very useful so we can easily navigate and scroll bigger command output, which does not fit the screen. Navigating in less is pretty simple and should be memorized because you will use it a lot in your Linux career. There is a lot more to learn. Read the manual pages for the less command to view all the available options.
A lot of movement actions can be done in the following ways:
- To scroll down a line, you can either use the arrow key or the J key. Here, we will only show you one of these keyboard options per action.
- To quit out of the less command, use the Q key.
- The uppercase G scrolls to the end of the file, while small g scrolls to the beginning of the file.
- The down arrow key scrolls down line by line.
- The up arrow key scrolls up line by line.
- Press the Page Down key to scroll down a page and press the Page Up key to scroll up a page.
- Press the right arrow key to scroll to the right for longer lines; to scroll back to the left, use the left arrow key.
- Press Ctrl + G to display file information at the bottom of the page.
- Press the Return key to quit the file information field.
- Type the slash key followed by search term, for example, HTTP, and press the Return key for searching for a keyword HTTP in the file using a forward search.
- Pressing the n key will jump to the next search result. Pressing the capital N key will jump back to the last form of search result.
- Note that the search is case insensitive if the search pattern is all lowercase; otherwise, it's case sensitive. For example, if you search for the word HTTP all in capital letters, it will only find patterns, which exactly have the HTTP in case-sensitive form.
- Now, jump to the end of the file by pressing capital G.
- A normal search using the forward slash key searches the file for a keyword from top to bottom.
- If you want to search for a keyword the other way around, from bottom to top, you can use the question mark operator, the question mark key, and then the keyword.
- Press the n key to jump to the next higher search result in the file. Press capital N to jump to the last form of search result.
- Less -N starts less in line number mode, which means that every line is prefixed by the corresponding line number.
- To go to a specific line number, for example, line 100, type the line number followed by a g, or to go to line number 20, type 20g.
- To view text files without editing it, you can also use the VIM editor.
- To start VIM in read-only mode, type view space and then the file.
- We will proceed with VIM editor in the next section.