Working with text files

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: