commands for viewing the contents of a file under Linux

Source: Internet
Author: User

commands for viewing the contents of a file under Linux

Commands to view the contents of a file:

Cat starts displaying content from the first line and outputs all content

The TAC displays the contents in reverse from the last line and outputs all the content

More according to the window size, one page of the actual file content

Less is similar to more, but its advantages can be a page forward, and the ability to search for characters

Head only shows the first few lines

Tail show only the last few lines

NL similar to cat-n, output line number when displayed

Tailf similar to Tail-f

1.cat and TAC

The function of cat is to output the contents of the file continuously on the screen from the first line. However, cat is not commonly used, because when a file is large, the number of rows is too high, and the screen cannot be fully allowed, only a subset of the content is visible.

Cat syntax: Cat [-n] File name (-N: Output with line number when displayed)

The function of the TAC is to output the content data to the screen backwards from the last line. We can find that the TAC is actually cat in turn written. This command is also not used.

TAC Syntax: TAC file name.

2.more and less (common)

The more function is to start the file from the first line, according to the Output window size, the appropriate output file content. When a page cannot be fully output, you can use "enter" to turn down the line, with the "SPACEBAR" to page down. To exit the view page, press the "Q" key. In addition, more can be combined with the pipe symbol "|" (pipe), for example: Ls-al | More

More syntax: more file names

Enter down n rows, need to be defined, default to 1 rows;

Ctrl F scrolls down one screen;

The space bar scrolls down one screen;

Ctrl-B returns to the previous screen;

= Output the line number of the current line;

: F output File name and line number of the current line;

V Call VI Editor;

! command to invoke the shell and execute the command;

Q Exit More

Less has the same function as more, but using more can not page forward, only backward.

Less can use the "PageUp" and "PageDown" keys to turn the front and back pages, which makes it easier to look.

Less syntax: less file name

Less has a feature that you can search for in the file you want, assuming you want to find in the passwd file there is no WebLogic string, then you can do this:

[email protected] etc]# less passwd

Then enter:

/weblogic

Enter

At this point, if there is a WebLogic string, Linux displays the character in a highlighted manner.

To exit the view page, press the "Q" key.

3.head and tail

Head and tail are typically used in cases where only the first few lines 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 (numbers show rows)

Tail functions just as opposed to head, showing only the last few lines of content

Tail Syntax: tail [-N number] file name

4.nl

The function of NL is the same as cat-n, which outputs the entire contents from the first line and displays the line number.

NL syntax: NL file name

5.tailf

The TAILF command is almost identical to the tail-f and should be more closely similar to tail--follow=name. When the file is renamed it can continue tracking, especially for log file tracking (follow the growth of a log files).  Unlike Tail-f, if the file does not grow, it will not go to access the disk file (it is similar to tail-f but does not access the file when it's not growing. This have the side effect of not updating the access time for the file, so a filesystem flush does not occur periodically When no log activity is happening.). TAILF is especially suitable for tracking log files on portable machines because it saves power because it reduces disk access (TAILF is extremely useful for monitoring log files on a laptop when logging was in Frequent and the user desires the hard disk spin down to conserve battery life.). The TAILF command is not a script, but a binary execution file compiled with C code, and some Linux installations do not have this command, this article provides a way to compile and install the TAILF command.

Talk about the difference between them:

1. Tailf always reads from the beginning of the file at 1.1 points, while Tail-f starts at the end of the file.

2. When the Tailf check file grows, it uses the file name, which is called with the stat system, while TAIL-F uses the open file descriptor; Note: Tail can also be similar to the effect of tracking file names, but tail always uses fstat system calls. Instead of a stat system call, the result is: By default, when tail files are secretly deleted, tail is not known, and TAILF is aware.

Common parameters

Format: Tailf logfile

The dynamic trace log file logfile, initially prints the last 10 lines of the file.

commands for viewing the contents of a file under Linux

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.