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 from the first line, many commands can be used to view the file by outputting all the contents of tac in reverse order from the last line. 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