Z Linux Less/more and Head/tail commands

Source: Internet
Author: User
Tags log log

The purpose of the cat command is to connect files or standard input and print.

1. Display the entire file at once: Cat filename

2. Create a file from the keyboard: cat > FileName can only create new files and cannot edit existing files.

3. Merge several files into one file: Cat file1 file2 > file

There are other commands that can be more flexible to view the contents of a file than cat.

More commands

The more command, which functions like cat, the Cat command is the entire contents of the file displayed on the screen from top to bottom. More will be a page-by-page display to facilitate users to read pages, and the most basic instruction is to press the blank key (space) on the next page, press the B key will be back to a page, but also the function of the search string. The more command reads the file backwards from the front, so it loads the entire file at startup.

1. Command format:

more [-DLFPCSU] [-num] [+/pattern] [+ linenum] [file ...]

2. Command function:

The more commands and the cat function are the same view of the contents of the file, but the difference is that more can be viewed by the page to view the contents of the file, but also support the function of direct jump.

3. Command parameters:

+n starting from joys N rows

-N defines the screen size as n rows

+/pattern searches for the string before each file is displayed, and then starts from the first two lines of the string

-C clear the screen from the top and then display

-D hint "press space to continue, ' Q ' To quit (press SPACEBAR to continue, pressing Q key to exit)", disable the ringing function

-L ignores Ctrl+l (page break) characters

-p to page a file by clearing the window instead of scrolling, similar to the-C option

-S displays a contiguous number of empty rows as a row

-U Remove the line from the contents of the file

4. Common Operation Commands:

Enter down n rows, which need to be defined. Default is 1 rows

CTRL+F Scroll down one screen

Space bar scroll down one screen

Ctrl+b Back to 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

5. Command instance:

Example 1: Display the contents of the file from line 3rd

Command:

More +3 Log2012.log

Output:

[email protected] test]# cat Log2012.log

2012-01

2012-02

2012-03

2012-04-day1

2012-04-day2

2012-04-day3

======[[email protected] test]# more +3 Log2012.log

2012-03

2012-04-day1

2012-04-day2

2012-04-day3

======[[email protected] test]#

Example 2: Find the first line from a file with the "Day3" string, and start displaying the output from the first two lines there

Command:

More +/day3 Log2012.log

Output:

[Email protected] test]# more +/day3 Log2012.log

... skipping

2012-04-day1

2012-04-day2

2012-04-day3

2012-05

2012-05-day1

======[[email protected] test]#

Example 3: Set the number of rows per screen

Command:

More-5 Log2012.log

Output:

[Email protected] test]# more-5 Log2012.log

2012-01

2012-02

2012-03

2012-04-day1

2012-04-day2

Pressing CTRL+F or SPACEBAR will display the next screen of 5 items, and the percentage will change as well.

Example 4: Column A directory of files, because the content is too much, we should learn to use more to page display. this has to be combined with the plumbing |.

Command:

Ls-l | More-5

Output:

[Email protected] test]# Ls-l | More-5

Total 36

-rw-r--r--1 root root 308 11-01 16:49 Log2012.log

-rw-r--r--1 root root 10-28 16:54 Log2013.log

-rw-r--r--1 root root 127 10-28 16:51 Log2014.log

lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log log.log

-rw-r--r--1 root root 10-28 17:02 log.log

-rw-r--r--1 root root notoginseng 10-28 17:07 log.txt

Drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

DRWXRWXRWX 2 root root 4096 10-28 14:47 test3

DRWXRWXRWX 2 root root 4096 10-28 14:47 test4

Description

Each page shows 5 file information, press CTRL+F or SPACEBAR will display the next 5 file information.

Less command

The less tool is also a tool for paging through files or other output, which is a very powerful tool for Linux to view the contents of a file. The usage of less is more resilient than more. In more, we have no way to turn front, can only look back, but if use less, you can use [PageUp] [PageDown] and other key functions to go to look at the file, more easily used to view the contents of a file! In addition, in less you can have more search function, not only to search down, but also to search upward.

1. Command format:

Less [parameter] file

2. Command function:

Less is similar to more, but you can browse files at will with less, and more can only move forward, but not backwards, and no will load the entire file before viewing it.

3. Command parameters:

-B < buffer size > Set buffer size

-e When the file display is finished, automatically leave

-F forcing special files to be opened, such as peripheral code, directories, and binaries

-G only flags the last keyword searched

-I ignores case when searching

-m shows a percentage similar to the more command

-N Displays the line number of each line

-o < file name > save less output in the specified file

-Q does not use warning tones

-S displays a row of continuous empty behavior

-S line too long will be out of the partial discard

-X < number > Displays the "tab" key as a specified number space

/string: Search down the function of "string"

String: The ability to search up "string"

N: Repeat the previous search (with/or?). About

N: Reverse repeats the previous search (and/or?) About

b Turn back one page

D Turn back half a page

H Display Help interface

Q Exit less command

U scroll forward Half page

Y Scrolls forward one line

SPACEBAR Scrolls one line

Enter key to scroll one page

[PageDown]: Flip one PAGE Down

[PageUp]: Flip one page Up

4. Usage examples:

Example 1: Viewing a file

Command:

Less Log2013.log

Output:

Instance 2:ps viewing process information and displaying it via less paging

Command:

Ps-ef |less

Output:

Example 3: Viewing the history of a command using records and displaying them via less paging

Command:

History | Less

Output:

[Email protected] test]# history | Less

Scp-r tomcat6.0.32 [Email protected]:/opt/soft

CD.

Scp-r Web [Email protected]:/opt/

CD Soft

+ ls

Scp-r jdk1.6.0_16/[Email protected]:/opt/soft

Clear

Vim/etc/profile

Vim/etc/profile

CD tomcat6.0.32/bin/

ls

/shutdown.sh.

/startup.sh.

Vim startup.sh

ls

Panax Notoginseng Echo $JAVA _home

Java

All-in LS

Max LS

All clear

Cd/opt

ls

apache-tomcat-6.0.32.tar.gz CP soft/

ls

Rm-f apache-tomcat-6.0.32.tar.gz

ls

CD Soft

% ls

Tar-vzf apache-tomcat-6.0.32.tar.gz

Wuyi Tar-vzfx apache-tomcat-6.0.32.tar.gz

TAR-ZXVF apache-tomcat-6.0.32.tar.gz

ls

apache-tomcat-6.0.32 CD

ls

CD.

MV apache-tomcat-6.0.32 tomcat6.0.32

(+) LS

tomcat6.0.32/CD

ls

Example 5: Browse multiple Files

Command:

Less Log2013.log Log2014.log

Output:

Description

Input: N, switch to Log2014.log

Enter: p, switch to Log2013.log

5. Additional Notes

1. Full Screen navigation

CTRL + F-Move forward one screen

CTRL + B-Move backward one screen

CTRL + D-Move forward half screen

CTRL + U-Move backward half screen

2. Single-line navigation

J-Move one line forward

K-Move backward one line

3. Other navigation

G-Move to the last line

G-Move to the first line

Q/zz-Exit less command

4. Other useful commands

V-Edit the current file using the configured editor

H-Show help documentation for less

&pattern-Displays only the lines of the matching pattern, not the entire file

5. Tag Navigation

When you use less to view large files, you can mark them in any one place, and you can navigate to the text location labeled with a specific tag by command:

Ma-Use A to mark the current position of the text

' A-navigate to mark a

Head command

Head and tail are as simple as their name, which is used to display the beginning or end of a number of chunks of text, the head is used to display the beginning of the file to the standard output, and tail to see the end of the file.

1. Command format:

Head [parameters] ... [File] ...

2. Command function:

The head is used to display the beginning of the file to the standard output, and the default head command prints the first 10 lines of its corresponding file.

3. Command parameters:

-Q Hide file names

-V Show file name

-c< bytes > Display bytes

-n< rows > rows displayed

4. Usage examples:

Example 1: Display the first n rows of a file

Command:

Head-n 5 Log2014.log

Output:

[email protected] test]# cat Log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================

[Email protected] test]# head-n 5 Log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05[[email protected] test]#

Example 2: Displaying the first n bytes of a file

Command:

Head-c Log2014.log

Output:

[Email protected] test]# head-c Log2014.log

2014-01

2014-02

2014

[Email protected] test]#

Example 3: The file's contents except the last N bytes

Command:

Head-c -32 Log2014.log

Output:

[Email protected] test]# head-c -32 log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12[[email protected] test]#

Example 4: Output file except the last n rows of the entire content

Command:

Head-n-6 Log2014.log

Output:

[Email protected] test]# Head-n-6 Log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07[[email protected] test]#

Tail command

The tail command writes the file to standard output starting at the specified point. Using the-f option of the tail command makes it easy to see the log files that are being changed, TAIL-F filename will display the most up-to-date contents of the filename on the screen, and not only refresh, so you can view the latest file content.

1. command format;

tail[necessary parameters [selection parameters] [file]

2. Command function:

Used to display the content at the end of the specified file, and is processed as input when the file is not specified. Common view log files.

3. Command parameters:

-F Loop Read

-Q does not display processing information

-V displays detailed processing information

Number of-c< > bytes displayed

-n< lines > Display rows

--pid=pid is shared with-F, which means that the process ends after the id,pid dies.

-Q,--quiet,--silent never output the header of the file name

-S,--sleep-interval=s is combined with-F, which means sleep s seconds at each repetition interval

4. Usage examples:

Example 1: Displaying the end of a file

Command:

Tail-n 5 Log2014.log

Output:

[Email protected] test]# tail-n 5 Log2014.log

2014-09

2014-10

2014-11

2014-12

==============================[[email protected] test]#

Description

Displays the last 5 lines of the file

Example 2: Looping through the contents of a file

Command:

Tail-f Test.log

Output:

[Email protected] ~]# ping 192.168.120.204 > Test.log &

[1] 11891[[email protected] ~]# tail-f Test.log

PING 192.168.120.204 (192.168.120.204) bytes of data.

Bytes from 192.168.120.204:icmp_seq=1 ttl=64 time=0.038 ms

Bytes from 192.168.120.204:icmp_seq=2 ttl=64 time=0.036 ms

Bytes from 192.168.120.204:icmp_seq=3 ttl=64 time=0.033 ms

Bytes from 192.168.120.204:icmp_seq=4 ttl=64 time=0.027 ms

Bytes from 192.168.120.204:icmp_seq=5 ttl=64 time=0.032 ms

Bytes from 192.168.120.204:icmp_seq=6 ttl=64 time=0.026 ms

Bytes from 192.168.120.204:icmp_seq=7 ttl=64 time=0.030 ms

Bytes from 192.168.120.204:icmp_seq=8 ttl=64 time=0.029 ms

Bytes from 192.168.120.204:icmp_seq=9 ttl=64 time=0.044 ms

Bytes from 192.168.120.204:icmp_seq=10 ttl=64 time=0.033 ms

Bytes from 192.168.120.204:icmp_seq=11 ttl=64 time=0.027 ms

[Email protected] ~]#

Description

Ping 192.168.120.204 > Test.log &//Ping remote host in background. and output files to Test.log; This practice also allows for more than one file to monitor. Use CTRL + C to terminate.

Example 3: Displaying a file starting from line 5th

Command:

Tail-n +5 Log2014.log

Output:

[email protected] test]# cat Log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================

[Email protected] test]# tail-n +5 log2014.log

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================

Turn from a Linux command every day

Z Linux Less/more and Head/tail commands

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.