preface

Take advantage of the weekend, review the Bird brother Linux private house dishes, see the file content review part, make a note, ha ha, I hope to help you oh.

cat

Cat: displays all contents of the file starting from the first line

Parameters that

Cat [-abentv] parameter: A: is equivalent to -vet parameter, can list some special characters, but not blank lines -b: lists the line number, only for non-blank lines, blank lines do not mark the line number -e: to display the end of the line breaking character $-n: Print the line number, along with the blank line, as opposed to the -b argumentCopy the code

Sample demo

Example 1:

Check the contents of cattest. TXT

[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# cat cattest.txt 
test cat command
jaywei

# # # # #
Copy the code

Example 2:

View the content of cattest. TXT and display the line number

[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# cat -n cattest.txt 
     1	test cat command
     2	jaywei
     3	
     4	# # # # #
Copy the code

Applicable scenario

  • Cat is short for Concatenate. Its main function is to continuously display the contents of a file on the screen.
  • Cat is suitable for a file with a small number of lines, such as within 40 lines.
  • If it is a normal DOS file, you need to pay special attention to some strange symbols, such as line breaks and [Tab], to display, you need to add parameters such as -a.

tac

Tac: From the last line, you can see that TAC is the reverse form of cat

Sample demo

[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# tac cattest.txt
# # # # #

jaywei
test cat command
Copy the code

Applicable scenario

  • Tac is the opposite of CAT, which is displayed on the screen continuously from the first line to the last line, whereas TAC is displayed on the screen backwards from the last line to the first line.

head

Head: Displays the content at the beginning of the file, in behavior units. The default is the first 10 lines at the beginning of the file

Parameters that

head [OPTION]... FILE... -n< number of lines > Number of lines to display -q Hide the file name -v Display the file name -c< bytes > Number of bytes to displayCopy the code

Sample demo

Display the first 12 lines of the sentinel.conf file

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# head -n 12 sentinel.conf
# Example sentinel.conf

# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
Copy the code

tail

View the contents of the file, also in action units, 10 lines by default, looking forward from the end. The -f parameter is used together with the -f parameter when listening for Java dynamic logs.

Parameters that

Tail [parameter] [file]-fCyclic reading -q Does not display processing information -v Displays detailed processing information -c< number > Number of bytes displayed -n< number of lines > Displays n lines at the end of the fileCopy the code

Sample demo

Sample a

Display the last 12 lines of the sentinel.conf file

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# tail -n 12 sentinel.conf
# <role> is either "leader" or "observer"
# 
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
Copy the code

Example 2

Continuously monitor the contents of sentinel.conf

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# tail -f sentinel.conf
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh<== wait until you type [CTRL]-c before leaving the tail commandCopy the code

Example 3

Continuously detect the contents of sentinel.conf and match the redis keyword. To match the keyword, use grep. Tail is also used with grep.

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# tail -f sentinel.conf | grep redis
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh<== wait until you type [CTRL]-c before leaving the tail commandCopy the code

Applicable scenario

  • tial -fIs used to dynamically listen to Java logs, often used in development tuning, it is generally followedgrepUse with a partner.

more

More: Displays file contents page by page

Parameters that

more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..] Parameter: -num: number of rows to display at one time. -P: not scroll to display each page, but clear the screen first and then display the content. -c: similar to -p, display the content first and then clear other old data-sWhen there are more than two consecutive blank lines, replace with a blank line +/pattern: Searches for the pattern before each document is displayed, and then displays -u starting after the pattern: +num: displays fileNames from line num: displays documents whose contents are to be displayedCopy the code

Common Operation Commands

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# more sentinel.conf 
# Example sentinel.conf. (middle omission)...# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
--More--(29%)

Copy the code

Take a closer look at the example above. If more is followed by a file with a larger number of lines than the screen output, something like this appears. The focus is on the last line, which shows the percentage currently displayed and where you can enter some useful commands. In the more program, you can use some common operation commands:

  • Space bar: represents the next page
  • Enter: Scroll down a line
  • / string: indicates that in this display, search down for the keyword “string”
  • :f: Immediately displays the file name and the number of lines currently displayed
  • Q: Indicates that the more file is not displayed immediately
  • B or [Ctrl]-b: page back, but this is only useful for files, not pipes.

The most common ones are: press Q to leave, press space to scroll down, press B to scroll back, and/string search, as shown in the demo below

Sample demo

Sample a

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# more -10 sentinel.conf
# Example sentinel.conf. (omitted)...# Before doing that MAKE SURE the instance is protected from the outside
--More--(4%)
Copy the code

View the sentinel.conf file in separate pages, showing 10 lines on one page. Press spacebar to go to the next page,

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# more -10 sentinel.conf
# Example sentinel.conf. (omitted)...# protected-mode no
# port <sentinel-port>
# The port that this sentinel instance will run on
--More--(7%)
Copy the code

Press B to go back to the previous page

# *** IMPORTANT ***. (omitted)...# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
--More--(5%)
Copy the code

Press Q to leave more immediately

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# 
Copy the code

Example 2

If you want to search for the sentinel keyword in the sentinel.conf file, you can do so

[root @ iZ2zehkwp9rwg4azsvnjbuZ redis - 4.0.7]# more -10 sentinel.conf
# Example sentinel.conf. (omitted)...# Before doing that MAKE SURE the instance is protected from the outsideAfter /sentinel enters /, the cursor automatically runs to the bottom line and waits for inputCopy the code

After you type /, the cursor will run to the bottom line and wait for your input. After you type the string and press [Enter], more will start to search down for the string. To query the same string repeatedly, press n. Finally, when I don’t want to watch more, I press Q to leave more.

# Before doing that MAKE SURE the instance is protected from the outside/sentinel ... skipping# protected-mode no

# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379

# sentinel announce-ip <ip>
# sentinel announce-port <port>
#
/
...skipping
# Example:
#
IP # sentinel announce - 2

# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
--More--(23%)
Copy the code

Applicable scenario

  • More Use large log files to view data page by page.

less

Less is similar to more, but less is more flexible than more.

  • If you use less, you can use the lower and lower buttons to browse files.
  • In addition, you can have more query functions in less. You can look up as well as down.

Common Operation Commands

  • Space bar: scroll down a page
  • [pageDown] : Scroll down a page
  • [PageUp] : Scroll up a page
  • Enter: Scroll down a line
  • Y: Scroll forward one line
  • / string: The ability to search down for “string”
  • ? String: The ability to search up “string”
  • N: Repeat the previous search (and/or? The relevant)
  • N: Repeat the previous search in reverse (and/or? The relevant)
  • Q: Leave less
  • B Turn back a page

Sample demo

Sample a

In the sentinel.conf file, search for the sentinel keyword as follows

less sentinel.conf 
Copy the code

Type a backslash /, enter the keyword sentinel, and press Enter

To repeat the previous search, press n. To reverse the previous search, press n

Example 2

Linux dynamically views log files using tail -f, but we can also use less+ f.

Less + file-name + command F = tail-f + file-name
Copy the code

We often use the tail -f +grep keyword to dynamically search for error logs, or we can use less. Enter shirft+g to reach the end of the file

And then you type? , enter a search keyword, such as sentinel, press Enter, and then press n to search up. Especially when the log file dynamic refresh too fast, sly smile.

Applicable scenario

  • Less is suitable for viewing files with large logs, page by page, and is more flexible than MORE. It can also view logs dynamically. I usually use it to view Java logs.

summary

This article summarizes several Linux commands for viewing file logs, cat, tac, head, tail, more, and less, of which less is really good for daily development log viewing and highly recommended.

Reference and thanks

  • Birdman’s Linux Home Dish
  • Linux command of | novice tutorial

Personal public account

  • If you are a good boy who loves learning, you can follow my public account and study and discuss with me.
  • If you feel that this article is not correct, you can comment, you can also follow my public account, private chat me, we learn and progress together.