How to view files in CentOS

Source: Internet
Author: User

There are many commands that can view files. Different commands have different advantages. You can select commands for different needs to improve efficiency:
Cat displays content starting with the first line and outputs all content
Tac displays the content in reverse order from the last line and outputs all the content
More according to the window size, the actual file content on one page
Less is similar to more, but it has the advantages of turning pages forward and searching for characters.
The head displays only the first few rows.
Tail only displays the last few rows
Nl is similar to cat-n and displays the output row number.
What I use most is more and less!


1. cat and tac
The cat function is to continuously output content from the first line to the screen. However, cat is not commonly used, because when the file size is large and the number of rows is large, the screen cannot be fully stored, only part of the content can be seen.

Cat Syntax: cat [-n] File Name
Parameter description:-n: The line number is output together when it is displayed.

For example:
[Root @ redhat ~] # Cat. bashrc
#. Bashrc
# User specific aliases and functions
Alias rm = 'rm-I'
Alias cp = 'cp-I'
Alias mv = 'mv-I'
# Source global definitions
If [-f/etc/bashrc]; then
./Etc/bashrc
Fi

Add the-n Parameter
[Root @ redhat ~] # Cat-n. bashrc
1 #. bashrc
2
3 # User specific aliases and functions
4
5 alias rm = 'rm-I'
6 alias cp = 'cp-I'
7 alias mv = 'mv-I'
8
9 # Source global definitions
10 if [-f/etc/bashrc]; then
11./etc/bashrc
12 fi

The tac function is to output content data from the last line to the screen. We can find that tac is actually written by cat in turn.

Tac Syntax: tac file name
For example:
[Root @ redhat ~] # Tac. bashrc
Fi
./Etc/bashrc
If [-f/etc/bashrc]; then
# Source global definitions
Alias mv = 'mv-I'
Alias cp = 'cp-I'
Alias rm = 'rm-I'
# User specific aliases and functions
#. Bashrc
No. The comparison with the cat output is totally inverted. This command is not commonly used.

2. more and less (recommended)
Compared with cat and tac, more and less are useful.

The more function is to output the file content from the first line according to the size of the output window.
When a page cannot be output in full, you can use the "Enter key" to Flip three lines down (my environment is three lines, and other linux versions may be different), or use the "Space key" to flip the page down.
Exit the view page. Press "q.
In addition, more can be used with the Pipeline character "|" (pipe), for example, ls-al | more

More Syntax:
More file name

The less function is similar to the more function, but the more function can only be used to flip the page forward.
You can use the [pageup] and [pagedown] keys to flip pages before and after pages, which makes it easier.

Less Syntax:
Less file name

If you want to search for the weblogic string in the passwd file, you can do the following:
[Root @ redhat etc] # less passwd
Then enter:
/Weblogic
Enter
If weblogic string exists, linux will highlight it.

Exit the view page. Press "q.

3. head and tail
Head and tail are usually used when only the first few or the last few lines of the file need to be read.

The head function is to display the first few lines of the file.

Head Syntax:
Head [-n number] File Name

For example:
[Root @ redhat etc] # head-n 5 passwd -- only show 5 rows
Root: x: 0: 0: root:/bin/bash
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin

The tail function is opposite to the head function. Only the last few lines of content are displayed.

Tail Syntax:
Tail [-n number] File Name

For example:
[Root @ redhat etc] # tail-n 10 passwd -- only display the last five rows
Userb: x: 503: 504:/home/userb:/bin/bash
Userc: x: 504: 502:/home/userc:/bin/bash
Zgz: x: 505: 505:/home/zgz:/bin/bash
Myy: x: 506: 505:/home/myy:/bin/bash
Weblogic: x: 507: 508:/home/weblogic:/bin/bash

4. nl
Like cat-n, nl outputs all content from the first line and displays the row number.

Nl Syntax:
Nl file name

For example:
[Root @ redhat etc] # nl ~ /. Bashrc
1 #. bashrc

2 # User specific aliases and functions

3 alias rm = 'rm-I'
4 alias cp = 'cp-I'
5 alias mv = 'mv-I'

6 # Source global definitions
7 if [-f/etc/bashrc]; then
8./etc/bashrc

9 fi


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.