Detailed explanation of commands to view linux files

Detailed explanation of commands to view linux files

How to view linux files

Command to view file contents:

  • cat starts displaying the content from the first line and outputs all the contents
  • tac displays the contents in reverse order from the last line and outputs all the contents
  • more Displays the contents of a file page by page according to the window size
  • less is similar to more, but has the advantage of being able to turn pages forward and search for characters
  • head only displays the first few lines
  • tail only displays the last few lines
  • nl is similar to cat -n, but outputs line numbers when displaying
  • tailf is similar to tail -f

1.cat and tac

The function of cat is to output the contents of a file continuously on the screen starting from the first line. When the file is large and has many lines, it cannot fit entirely on the screen and only part of the content can be seen.

cat syntax: cat [-n] file name (-n: when displaying, output the line number together)

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

tac syntax: tac filename.

2. more and less (commonly used)

The function of more is to output the file content appropriately starting from the first line according to the size of the output window. When one page cannot be fully output, you can use the "Enter key" to turn down the line and the "Space key" to turn down the page. To exit the viewing page, press the "q" key. In addition, more can also be used with the pipe symbol "|" (pipe), for example: ls -al | more

  • The syntax of more is: more file name
  • Enter goes down n lines, needs to be defined, default is 1 line;
  • 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 outputs the file name and the line number of the current line;
  • v calls the vi editor;
  • ! The command calls Shell and executes the command;
  • q exitmore

The function of less is similar to that of more, but you cannot turn pages forward using more, you can only turn pages backward.

In less, you can use the [pageup] and [pagedown] keys to turn pages forward and backward, which seems more convenient.

The syntax of less is: less file name

Less also has a function that allows you to search for the content you want in the file. Suppose you want to find the weblogic string in the passwd file, you can do it like this:

[root@redhat etc]# less passwd

Then enter:

/weblogic

Enter

At this time, if there is a weblogic string, Linux will display the character in a highlighted manner.

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

3. Head and tail

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

The syntax of head is: head [n number] file name (number shows the number of lines)

The function of tail is exactly the opposite of head, only the last few lines are displayed

The syntax of tail: tail [-n number] file name

4.nl

The function of nl is the same as cat -n, which also outputs all the content from the first line and displays the line number.

The syntax of nl is: nl filename

5.tailf

The tailf command is almost equivalent to tail -f, strictly speaking, it should be more similar to tail --follow=name. It can continue to track files after they are renamed, making it particularly suitable for following the growth of a log file.

Unlike tail -f, it does not access the disk file if the file does not grow.

Tailf is particularly suitable for tracking log files on portable computers because it saves power by reducing disk access.

The tailf command is not a script, but a binary executable file compiled with C code. Some Linux installations do not have this command.

The above is all the relevant knowledge points introduced this time. Thank you for your learning and support for 123WORDPRESS.COM.

You may also be interested in:
  • How to view the creation time of files in Linux
  • Use the more, less, and cat commands in Linux to view file contents
  • Linux uses lsof command to check file opening status
  • Detailed examples of viewing file attributes in Linux (ls, lsattr, file, stat)
  • How to view folder sizes and sort by size in Linux
  • A simple way to view the file system block size and memory page size in Linux
  • Linux du command to view folder sizes and sort in descending order

<<:  MySQL 8.0 error The server requested authentication method unknown to the client solution

>>:  A very detailed summary of communication between Vue components

Recommend

A brief discussion on the placement of script in HTML

I used to think that script could be placed anywh...

Html Select option How to make the default selection

Adding the attribute selected = "selected&quo...

What are the benefits of using // instead of http:// (adaptive https)

//Default protocol /The use of the default protoc...

CSS realizes the realization of background image screen adaptation

When making a homepage such as a login page, you ...

Clever use of webkit-box-reflect to achieve various dynamic effects (summary)

In an article a long time ago, I talked about the...

CSS3 realizes bouncing ball animation

I usually like to visit the special pages or prod...

Advertising skills in the Baidu Union environment (graphic tutorial)

Recently, students from the User Experience Team o...

JavaScript Dom implements the principle and example of carousel

If we want to make a carousel, we must first unde...

Example of fork and mutex lock process in Linux multithreading

Table of contents Question: 1. First attempt 2. R...

Detailed Tutorial on Using xargs Command on Linux

Hello everyone, I am Liang Xu. When using Linux, ...

Vue+openlayer5 method to get the coordinates of the current mouse slide

Preface: How to get the coordinates of the curren...

Simple tips to increase web page loading speed

The loading speed of a web page is an important in...

A summary of detailed insights on how to import CSS

The development history of CSS will not be introd...