Unix Tutorial #3: Reading Text Files¶
The command line is useful for both viewing and manipulating text files. Manipulation means editing text — for example, replacing words in text files, or appending text from the command line to the end of a file (also known as redirection). This is useful for creating scripts, text files containing one or more commands that are run consecutively. In later tutorials, you will use these techniques to automate your analyses, which can save enormous amounts of time.
You can display the contents of a file using the cat command, which stands for concatenate. Let’s say we have a file on our Desktop called myFile.txt, which contains the words one through fifteen (i.e., one, two, three…fifteen), with each number on a separate line. Use the command line to navigate to the Desktop, and then type cat myFile.txt . This will print the contents of the file to your command line. This is the same idea as using the GUI to double-click on the text file to see its contents.
Using the command line and the GUI to read the contents of a text file. On the left is the command line using the cat command, which prints the contents to the Terminal. On the right is the contents of the file displayed after using the mouse to double-click the file.
We refer to the output from this command as stdout, or standard output. The commands that are typed into the Terminal are called stdin, or standard input. This touches on the concept of streams, or the flow of information into and out of the command line, and we will use these ideas to give us more flexibility in manipulating text files. For now, think of stdin as anything you type into the Terminal, and stdout as what is returned if the command is run without any errors. If the command that you type does result in an error — for example, because the command was misspelled or because not enough arguments were provided — the text that is output to the Terminal is called stderr, or standard error.
Illustration of streams in Unix. Whatever is typed into the Terminal is stdin, and, if it runs without error, whatever is output is called stdout. If there is an error, the output is instead called stderr.
The cat command is useful for viewing the contents of smaller files, but if the file contains hundreds of lines of text, it is overwhelming to have everything printed to the Terminal at once. To see only a part of the file, we can use the commands head and tail to see the first few or the last few lines of the file, respectively. Using myFile.txt as an example, typing
Would return the first five lines; whereas typing
Would return the last five lines. Although the default is to return five lines, these commands have an option to display any amount of lines that you choose. For example,
head -10 myFile.txt tail -10 myFile.txt
Would return the first ten lines and the last ten lines. Try these out yourself, changing the number of lines that are displayed.
Redirection¶
In addition to displaying the results of a command, stdout can be used to move or append the output to a file, a concept known as redirection. For example, if you type
The word “sixteen” goes into the file tmp.txt instead of being written to standard output. Notice that it creates the file tmp.txt even if it doesn’t exist. However, if we try that again with another string — for example,
It will overwrite the file with whatever we printed to standard output. If you want to append standard output to the end of a file without overwriting the other data in the file, use two greater-than signs. For example, type
If you type cat tmp.txt , you will see both seventeen and eighteen.
Although these examples are trivial, redirection is invaluable for quickly editing text files and for writing scripts, which allow you to run analyses for hundreds or thousands of subjects with only a few lines of code.
Video¶
Click here for a video walkthrough of commands for reading text files. This video will also show you how to read help files using the less command and a paging window.
Exercises¶
- Create a new file called “tmp.txt” and type whatever you want into the file. Use cat to string together both the myFile.txt and tmp.txt files, and redirect the output to create a new file. Print the contents of the new file to stdout.
- If you have AFNI installed on your machine, use less on the command 3dcalc to find strings matching “Example.” Now try it using the less command with an option to ignore whether the letters in the string are upper case or lower case. Hint: To find this option, search for the string “case” in the man file for less . (If you have FSL installed instead of AFNI, try the same exercise with the command fslmerge .)
- Unix has a built-in command called sort which will sort text numerically or alphabetically. What happens when you use myFile.txt as an argument for sort ? What about typing this command:
In your own words, explain the difference between the two methods.
© Copyright 2019, Andy Jahn Revision cb9a0b41 .
Versions latest stable Downloads On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.
5 Commands to View the Content of a File in Linux Command Line
Here are five commands that let you view the content of a file in Linux terminal.
If you are new to Linux and you are confined to a terminal, you might wonder how to view a file in the command line.
Reading a file in Linux terminal is not the same as opening file in Notepad. Since you are in the command line mode, you should use commands to read file in Linux.
Don’t worry. It’s not at all complicated to display a file in Linux. It’s easy as well essential that you learn how to read files in the line.
Here are five commands that let you view the content of a file in Linux terminal.
5 commands to view files in Linux
Before you how to view a file in Unix like systems, let me clarify that when I am referring to text files here. There are different tools and commands if you want to read binary files.
Cat becomes a powerful command when used with its options. I recommend reading this detailed tutorial on using cat command.
The problem with cat command is that it displays the text on the screen. Imagine if you use cat command with a file that has 2000 lines. Your entire screen will be flooded with the 200 lines and that’s not the ideal situation.
So, what do you do in such a case? Use less command in Linux (explained later).
2. nl
The nl command is almost like the cat command. The only difference is that it prepends line numbers while displaying the text in the terminal.
There are a few options with nl command that allows you to control the numbering. You can check its man page for more details.
3. Less
Less command views the file one page at a time. The best thing is that you exit less (by pressing q), there are no lines displayed on the screen. Your terminal remains clean and pristine.
I strongly recommend learning a few options of the Less command so that you can use it more effectively.
There is also more command which was used in olden days but less command has more friendly features. This is why you might come across the humorous term ‘less is more’.
4. Head
Head command is another way of viewing text file but with a slight difference. The head command displays the first 10 lines of a text file by default.
You can change this behavior by using options with head command but the fundamental principle remains the same: head command starts operating from the head (beginning) of the file.
5. Tail
Tail command in Linux is similar and yet opposite to the head command. While head command displays file from the beginning, the tail command displays file from the end.
By default, tail command displays the last 10 lines of a file.
Head and Tail commands can be combined to display selected lines from a file. You can also use tail command to see the changes made to a file in real time.
Bonus: Strings command
Okay! I promised to show only the commands for viewing text files. And this one deals with both text and binary files.
The Strings command displays the readable text from a binary file.
No, it doesn’t convert binary files into text files. If the binary file consists of actual readable text, the strings command displays those text on your screen. You can use the file command to find the type of a file in Linux.
Conclusion
Some Linux users use Vim to view the text file. Of course, you can easily move from the beginning to the end of the lines and edit the file but it’s overkill for just reading a file. My favorite command to open a file in Linux is the less command. It leaves the screen clear and has several options that makes viewing text file a lot easier.
Since you now know ways to view files, maybe you would be interested in knowing how to edit text files in Linux. Cut and Paste are two such commands that you can use for editing text in Linux terminal. You may also read about creating files in Linux command line.
Which command do you prefer?