Linux in cat more less head tail command difference

Source: Internet
Author: User

1, Cat display file connection file content tool;

Cat is a text file viewing and linking tool. To view the contents of a file, it is simpler to use cat, which is the name of the cat directly behind it.

Like what:
[Ro[email protected] ~]# Cat/etc/fstab

In order to facilitate the novice brother to grasp the tool flexibly, we say a little more commonly used parameters;
1.0 CAT grammatical 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

1.1 Cat View File content instance:

[[email protected] ~]# Cat/etc/profile Note: View the contents of the profile under the/etc/directory;
[[email protected] ~]# cat-b/etc/fstab Note: View the contents of the/etc/directory, and number of non-blank lines, line numbers starting from 1;
[Email protected] ~]# cat-n/etc/profile Note: All rows (including blank lines) of profile in/etc directory are numbered output display;
[Email protected] ~]# 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

For files with great content, cat can be routed through the pipeline | To more tools, and then to a page-by-page view;
[Email protected] ~]# Cat/etc/fstab/etc/profile | More


1.2 Cat's creation, connection file function instance;

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;


2, more file content 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

2.1 More syntax, arguments, 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.2 More parameter application example;
[[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;


2.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;


2.4 Other commands by means of pipe and more combined use examples;

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


3. 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;


3.1 Less syntax format:

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

3.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;


4, head tool, display the first few lines of file content:

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


5, tail tool, display the last few lines of file content:

Tail is the number of lines that are displayed before 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

Linux in cat more less head tail command difference

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.