Commands to view file contents under Linux

Commands to view file contents under Linux

Command to view file contents:

cat displays the content starting from the first line and outputs all the content

tac displays the contents in reverse order from the last line and outputs all the contents

more According to the window size, the actual file content page by page

less is similar to more, but has the advantage of being able to page forward and searching for characters

head only displays the first few lines

tail only shows the last few lines

nl is similar to cat -n, output line number when displayed

tailf is similar to tail -f 

1. cat and tac

The function of cat is to continuously output the contents of the file to the screen starting from the first line. However, cat is not commonly used, because when the file is large and the number of lines is large, and the screen cannot fit all, only a part of the content can be seen.

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

 

The function of tac is to reverse the file from the last line and output the content data to the screen. We can find that tac is actually cat written in reverse. This command is also not commonly used.

tac syntax: tac filename.

 

2.more and less (commonly used)

The function of more is to start the file from the first line, according to the size of the output window, the appropriate output file content. When one page cannot be fully 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 viewing page, press the "q" key. In addition, more can also be used with the pipe character "|" (pipe), for example: ls -al | more

more syntax: more filename

Enter down n lines, need to be defined, the default is 1 line; 

Ctrl f scroll down one screen; 

Spacebar to scroll down one screen; 

Ctrl b returns to the previous screen; 

= output the line number of the current line; 

:f output the file name and the line number of the current line; 

v Invoke vi editor; 

The ! command invokes the Shell and executes the command; 

q quit more

 

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

Less can use the [pageup] and [pagedown] keys to turn pages forward and backward, which looks more convenient.

Less syntax: less filename

Less also has a function that can search the file for what you are looking for. Suppose you want to find whether there is a weblogic string in the passwd file, then 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 highlight the character and display it.

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

 

3.head和tail

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

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

 

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

Syntax of tail: tail [-n number] file name

 

4.nl

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

Syntax of nl: nl filename

 

5.tailf

 

 The tailf command is almost equivalent to tail -f, and should be more similar to tail --follow=name strictly speaking. It can also continue to track when the file is renamed, especially suitable for the tracking of log files (follow the growth of a log file). Unlike tail -f, it does not access the file on disk if the file is not growing (It is similar to tail -f but does not access the file when it is not growing. This has 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 extremely useful for monitoring log files on a laptop when logging is infrequent and the user desires that the hard disk spin down to conserve battery life.). The tailf command is not a script, but a binary executable file compiled with C code. Some Linux installations do not have this command. This article provides a method on how to compile and install the tailf command.

Let’s talk about the difference between the two:

1. tailf always reads from the beginning of the file bit by bit, while tail -f starts to read from the end of the file

2. When the tailf check file grows, the file name is used, and the stat system call is used; while tail -f uses the opened file descriptor; Note: tail can also achieve a similar effect of tracking file names; but tail Always use the fstat syscall, not the stat syscall; the result: by default tail doesn't know about tail's files, but tailf does.

 

 Common parameters

 

Format: tailf logfile

 

Dynamically track the log file logfile, and initially print the last 10 lines of the file.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324692643&siteId=291194637