The difference between the cat, more, less, tail, head commands in Linux

Source: Internet
Author: User
Tags syslog

One, cat display file connection file contents of the tool

Cat is a text file (view) and (connection) tool, often used in conjunction with more, and the more different is that cat can merge files. To view the contents of a file, it is simpler to use cat, which is the name of the cat directly behind it.

such as: Root@ubuntu:/# cat/etc/passwd

1. CAT syntax structure:

Cat [Options] [file] ...

Options
-A,--show-all equivalent to-vet
-B,--number-nonblank number of non-null output lines
-e equivalent to-ve
-E,--show-ends displays at the end of each line
-N,--number all line numbers for the output
-S,--squeeze-blank does not output multiple lines of blank lines
-T vs.-VT equivalence
-T,--show-tabs to display the ^i character as a
-U (ignored)
-V,--show-nonprinting uses ^ and M-references, except LFD and TAB
--HELP Display this help message and leave

2. Cat View File content instance:

[Root@localhost ~]# cat/etc/profile Note: View the contents of the profile under the/etc/directory;
[Root@localhost ~]# cat-b/etc/fstab Note: View the contents of the/etc/directory, and number of non-blank lines, line numbers starting from 1;
[Root@localhost ~]# cat-n/etc/profile Note: A numbered output display of all rows (including blank lines) of the profile in/etc directory;
[Root@localhost ~]# cat-e/etc/profile Note: View the profile content under/etc/and append the $ symbol at the end of each line;

Cat plus parameter-N and NL tools, file content output at the same time, each line will be preceded by a line number;
[Email protected] ~]# cat-n/etc/profile
[Email protected] ~]# Nl/etc/profile

Cat can display the contents of multiple files at the same time, for example, we can display the contents of two files simultaneously on a cat command;
[Email protected] ~]# Cat/etc/fstab/etc/profile

Catfor files with great content, it can be piped | To more tools and then page-by-page view;
[Email protected] ~]# Cat/etc/fstab/etc/profile | More

3, Cat's creation, connection file Function Example:

Cat has the ability to create files, after creating the file, to end with EOF or stop;

[[email protected] ~]# cat > Linuxsir.org.txt << EOF Note: Create Linuxsir.org.txt file;
> I'll test the cat creation file and enter content for the file; Note: This is for the linuxsir.org.txt file input;
> North-South Test; Note: This is for the Linuxsir.org.txt file input content;
> EOF Note: Exit Edit status;

[email protected] ~]# Cat Linuxsir.org.txt Note: We look at the contents of the Linuxsir.org.txt file;

Let me test the cat creation file and enter the content for the file;
North-South south-north test;

Cat also has the ability to append content to existing files;
[email protected] ~]# Cat Linuxsir.txt Note: View the existing file Linuxsir.txt content;
I am Beinannanbei from linuxsir.org. Note: The content line
I am writing a document for the cat command

[email protected] ~]# cat >> linuxsir.txt << EOF Note: We append content to the Linuxsir.txt file;
> I'll test the cat's ability to append content to the document; Note: This is the content of the recovery
> OK?
> ok~
> North South
> EOF Note: Exit with EOF;

[email protected] ~]# cat linuxsir.txt Note: Check the contents of the file to see if the recovery was successful.
I am Beinannanbei from linuxsir.org.
I am writing a document for the cat command

Let me test cat's ability to append content to a document;
Ok?
ok~
North and South

Cat connects the contents of multiple files and outputs them to a new file;
Suppose we have sir01.txt, Sir02.tx and Sir03.txt, and the contents are as follows;

[email protected] ~]# cat Sir01.txt
123456
I am testing

[email protected] ~]# cat Sir02.txt
56789
Beinan tested

[email protected] ~]# cat Sir03.txt
09876
linuxsir.org Testing

I want to connect the Sir01.txt, Sir02.txt, and sir03.txt three files through cat (that is, the contents of the three files together) and output to a new file sir04.txt.

Note: The principle is to connect the contents of three files, then create a sir04.txt file and write the contents of several files to Sir04.txt. In particular, if you enter an existing Sir04.txt file, the Sir04.txt content will be emptied.

[Email protected] ~]#Cat Sir01.txt sir02.txt sir03.txt > Sir04.txt
[Email protected] ~]# more Sir04.txt
123456
I am testing
56789
Beinan tested
09876
linuxsir.org Testing

Cat appends one or more existing file contents to an existing file
[email protected] ~]# cat Sir00.txt
Linuxsir.org Forever

[email protected] ~]# cat sir01.txt sir02.txt sir03.txt >> sir00.txt

[email protected] ~]# cat Sir00.txt
Linuxsir.org Forever
123456
I am testing
56789
Beinan tested
09876
linuxsir.org Testing

Warning: We want to know that > means to create,>> is append. Don't get mixed up. It is not a joke to make a mistake;

ii. more file contents or output viewing tools

More is one of our most commonly used tools, the most commonly used is to display the content of the output, and then according to the size of the window to display the page, and then can also indicate the percentage of the file;

[Email protected] ~]# More/etc/profile

1, more syntax, parameters and commands:

More [parameter options] [file]

The parameters are as follows:
+num starting from Num line;
-num defines the screen size for NUM lines;
+/pattern starts from the first two lines of the pattern;
-C clear the screen from the top and then display;
-D Prompt Press space to continue, ' Q ' to quit. (Pressing the SPACEBAR to continue, press the Q key to exit), disable the ringing function;
-L ignores Ctrl+l (page break) characters;
-P pages are changed by clearing the window instead of scrolling. A bit similar to the-c parameter;
-S displays consecutive empty lines as one line;
-U Remove the underline from the contents of the file to exit the more action instruction is Q

2, more parameters application examples:

[[email protected] ~]# More-dc/etc/profile Note: Display the prompt and display from the terminal or the top of the console;
[Email protected] ~]# more +4/etc/profile Note: Starting from the 4th line of profile;
[[email protected] ~]# more-4/etc/profile Note: 4 rows per screen;
[Email protected] ~]# more +/mail/etc/profile Note: Start with the first two lines in profile;

3, more action instructions:

When we look at a larger file, we use more action instructions, such as ctrl+f (or SPACEBAR) is to display a screen down, Ctrl+b is to return to the previous screen, the ENTER key can scroll down to display n rows, to pass the default is 1 rows;

We only talk about a few commonly used, and try to know it by ourselves;

Enter down n rows, need to be defined, default to 1 rows;
CTRL+F scroll down one screen;
The space bar scrolls down one screen;
Ctrl+b return to the 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 When we look at a file, want to call VI to edit it, do not forget the V action command, which is more convenient;

4. Examples of other commands used in combination with piping and more:

For example, we list a directory of files, because the content is too much, we should learn to use more to page display. This has to do with plumbing | Together, for example:
[Email protected] ~]# ls-l/etc |more

Third, less view file content tool

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, and it is a beginner, I suggest you use less. Because less content too much, we put the most commonly used to introduce;

1. Syntax format for less:

Less [parameter] file

Common parameters

-C refreshes the screen from top to bottom and displays the contents of the file. Instead of scrolling through the bottom to complete the refresh;
-F Force open file, binary file display, do not prompt warning;
-I ignores case when searching, unless the search string contains uppercase letters;
-I ignores case when searching, unless the search string contains lowercase letters;
-m displays the percentage of files read;
-m explicit read the file percentage, line number and total number of rows;
-N Enter the travel number in front of each line;
-P Pattern Search pattern, for example, in/etc/profile search word MAIL, use less-p mail/etc/profile
-S displays consecutive blank lines as a blank line;
-Q does not ring under the terminal;

For example: When we display the content of/etc/profile, let it display the line number;
[Email protected] ~]# less-n/etc/profile

2, less's action command:

After entering less, we have to learn a few actions, so that it is more convenient for us to check the contents of the file, the most should remember the command is Q, which allows less to terminate the view file exit;

Action:

The ENTER key moves down one line;
Y move up one line;
The space bar scrolls down one screen;
b scroll up one screen;
D scroll down half screen;
H less's help;
U move up the half screen;
W can specify which line to start displaying, which is displayed from the next line of the specified number, for example, 6, which is displayed from line 7th;
G jumps to the first line;
G jumps to the last line;
P n% jumps to n%, such as 10%, which means that it starts at 10% of the entire file content;
/pattern searches for pattern, such as/mail, to search for mail words in a file;
V Call VI Editor;
Q Exit Less
!command Call the shell, you can run the command, such as!ls display the current column in the current directory of all files;

As far as less is concerned, there is too much content, and it is best to look for man when using it. Here is not an example;

The Head tool, which displays the first few lines of the file contents

Head is the number of lines that appear before the contents of a file;

The usage is relatively simple;
head-n The value file name of the row;

For example, we show the first 10 lines of/etc/profile, which should be:
[Email protected] ~]# head-n 10/etc/profile

v. Tail tool that displays the last few lines of the file contents

Tail is the last number of lines that display the contents of a file;

The usage is relatively simple;
tail-n The value file name of the row;

For example, we show the last 5 lines of/etc/profile, which should be:
[Email protected] ~]# tail-n 5/etc/profile

tail-f/var/log/syslog Displays the following 10 lines of the file syslog and automatically displays the new file contents after the file contents have been increased.

Note: The last command tail is very useful, especially when monitoring log files, you can always display the new log information on the screen.

span> Source:/http my.oschina.net/junn/blog/304868

Differences in the cat, more, less, tail, head commands in Linux

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.