Detailed command explanation for viewing linux file

  • 2021-07-24 11:57:22
  • OfStack

How do I view an linux file

To view the contents of a file:

cat displays content from line 1 and outputs everything tac displays the contents in reverse order from the last line and outputs everything more based on window size, page-by-page real file content less is similar to more, but it has the advantages of turning pages forward and searching for characters head shows only the first few lines tail shows only the last few lines nl is similar to cat-n and outputs line numbers when displayed tailf is 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. When the file is large and the number of lines is large, when the screen cannot be fully accommodated, only one part of the content can be seen.

cat Syntax: cat [-n] Filename (-n: When displayed, line number 1 is output)

The function of tac is to invert the file from the last line to output the content data to the screen. We can find that tac is actually written in reverse of cat.

tac syntax: tac file name.

2. more and less (commonly used)

The function of more is to start the file from line 1 and output the file contents appropriately according to the size of the output window. When 1 page can not be all output, you can use the "Enter Key" to turn down the line, and use the "Space Bar" to turn down the page. To exit the view page, press the "q" key. In addition, more can also be used with the pipe symbol "" (pipe), for example: ls-ES50more

Syntax for more: more Filename Enter goes down to n line, which needs to be defined, and the default is 1 line; Ctrl f scrolls down 1 screen; The space bar scrolls down 1 screen; Ctrl b returns to the last screen; = Output the line number of the current line; : f output file name and line number of the current line; v calls the vi editor; ! The command calls Shell and executes the command; q Exit more

The function of less is similar to that of more, but with more, you can't turn pages forward, you can only turn pages backward.

less can use the "pageup" and "pagedown" keys to page forward and back, which looks more convenient.

Syntax for less: less Filename

less also has a function that can search the file for what you want. If you want to find whether there is an weblogic string in the passwd file, you can do this:


[root@redhat etc]# less passwd

Then enter:


/weblogic

Carriage return

If there is an weblogic string at this time, linux will display the character as highlighted.

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

3. head and tail

head and tail are commonly used when only the first or last lines of a file need to be read. The function of head is to display the first few lines of the file

Syntax for head: head [n number] Filename (number shows lines)

The function of tail is exactly the opposite of that of head, showing only the last few lines

Syntax for tail: tail [-n number] Filename

4.nl

The function of nl is similar to that of cat-n1, which is to output all the contents from line 1 and display the line number

Syntax for nl: nl Filename

5.tailf

The tailf command is almost equivalent to tail-f, and should be more strictly similar to tail-follow=name. It also continues tracing after a file is renamed, especially for log file tracing (follow the growth of a log file).

Unlike tail-f, it does not access disk files if they do not grow.

tailf is especially suitable for tracing log files on laptops because it saves power and reduces disk access.

The tailf command is not a script, but a binary executable file compiled with C code, which is not available after some Linux installations.

The above is all the relevant knowledge points introduced this time. Thank you for your study and support for this site.


Related articles: