Viewing text files


Get Started. It's Free
or sign up with your email address
Rocket clouds
Viewing text files by Mind Map: Viewing text files

1. Cat

1.1. This is the simplest way to read a text file; it simply output the file content inside the terminal. Be careful: if the file is huge, it could take some time to complete the printing process!

1.2. The cat command is a versatile utility. It can be used to view text, to create text files, and to join files. Its name is short for concatenate, which means to combine files.

1.3. Using cat alone echoes on the screen any text you enter. It will continue to do so until you exit with the [Ctrl]-[D] keystroke.

1.4. cat file_name.txt

2. More

2.1. An improved version of cat. If your file is longer than the display of the terminal, you can simply type

2.2. The more command is a "pager" utility used to view text in the terminal window one page or screen at a time. The [Space] bar moves forward one page and [Q] quits.

2.3. more file_name.txt

3. Less

3.1. The main difference between more and less is that less allows backward and single-line movement using the same navigation as main pages

3.2. Once the document is open, you can type some commands to enable some useful features, such as:

3.2.1. q: close immediately the opened file

3.2.2. /word: search 'word' inside the document. Pressing n you can go to the following occurrence of 'word'

3.2.3. ENTER: scrolls down of a single line.

3.2.4. r: repaints the file content, if it's changing while reading.

3.3. The format of the less command is: less file_name.txt

4. Head

4.1. The head command displays the beginning of a file. The format of the head command is:

4.2. head <filename>

4.3. By default, you can only read the first ten lines of a file. You can change the number of lines displayed by specifying a number option.

4.4. head -20 <filename>

4.5. The above command would display the first 20 lines of a file named <filename>.

5. Tail

5.1. The reverse of head is tail. Using tail, you can view the last ten lines of a file. This can be useful for viewing the last ten lines of a log file for important system messages.

5.2. can be also use tail to watch log files as they are updated.

5.3. Using the -f option, tail automatically prints new messages from an open file to the screen in real-time.

5.4. tail file_name.txt

5.5. For example, to actively watch /var/log/messages, enter the folowing at a shell prompt (as the root user): tail -f /var/log/messages

6. Grep

6.1. The grep command is useful for finding specific character strings in a file. For example, to find every reference made to "pattern" in the file <filename>, enter:

6.2. grep pattern <filename>

6.3. Each line in the file that includes the pattern "pattern" is located and displayed on the screen/

7. Using Ubuntu you have different ways to read a text file, all similar but useful in different context.