Linux Cat Commands

Source: Internet
Author: User

How to use: Cat [-ABEENSTTUV] [--help] [--version] FileName

Description: Connect the file string to the basic output (screen or add > FileName to another file)

Parameters:
-N or--number the number of rows for all outputs starting from 1
-B or--number-nonblank and-n similar, except for blank lines not numbered
-S or--squeeze-blank when you encounter a blank line that has more than two consecutive lines, replace the blank line with one line
-V or--show-nonprinting

Example:
Cat-n textfile1 > Textfile2 Add the file contents of the Textfile1 and enter the Textfile2 file.
Cat-b textfile1 textfile2 >> textfile3 Append the contents of the Textfile1 and Textfile2 to the textfile3 after adding the line number (blank line not added) to the file.

Example:
Add a line number to the Textfile1 file and enter the Textfile2 file.
Cat-n textfile1 > Textfile2
Append the contents of the Textfile1 and Textfile2 files to the textfile3 after adding the line number (blank line not added).
Cat-b textfile1 textfile2 >> textfile3
Cat/dev/null >/etc/test.txt This is for emptying/etc/test.txt archive content
Cat can also be used to make image file. For example, to make a soft disc image file, put the floppy
cat/dev/fd0 > OUTFILE
Conversely, if you want to write the image file to a floppy disk, please call
Cat Img_file >/dev/fd0

Note:
1. OUTFILE refers to the image file name of the output.
2. Img_file refers to image FILE.
3. If the device is written back from image file, the device capacity needs to be equivalent.
4. Usually used in the production of the boot magnetic chip. Cat has three main functions: Go
1. Display the entire file at once. $ cat FileName ~,y6;e
2. Create a file from the keyboard. $ cat > FileName 0#!7
Only new files can be created and existing files cannot be edited. : z)
3. Merge several files into one file. $cat file1 file2 > file "t?=\

Parameter: P:ds
-N or--number the number of rows for all outputs starting from 1 \R5
-B or--number-nonblank and-n are similar, except for blank lines not numbered |,3s
-S or--squeeze-blank when you encounter a blank line that has more than two consecutive lines, replace the blank line with one line!s
-V or--show-nonprinting TDS? O

Example: ' @uq *y
Cat-n textfile1 > Textfile2 Add the file contents of the Textfile1 to the file, and then enter the Textfile2. D
Cat-b textfile1 textfile2 >> textfile3 Append the contents of the Textfile1 and Textfile2 to the textfile3 after adding the line number (blank line not added) to the file.; L-#pj

Example: +hu
Add a line number to the Textfile1 file and enter Textfile2 in this file z]g=
Cat-n textfile1 > Textfile2 V "z&r3
Append the contents of the Textfile1 and Textfile2 files to the textfile3 after adding the line number (blank line not added). rg.7n
Cat-b textfile1 textfile2 >> textfile3 1m?1o
Cat/dev/null >/etc/test.txt This is for emptying/etc/test.txt archive content J2fx
Cat can also be used to make image file. For example, to make an image file of a floppy disc, put the floppy disk back and hit {ZQ
cat/dev/fd0 > OUTFILE%w~ik.
Conversely, if you want to write the image file to a floppy disk, please hit GK "D
Cat Img_file >/dev/fd0 E

Note: ©newboys--newboys General forum G
1. OUTFILE refers to the image file name of the output. +cmcct
2. Img_file refers to image FILE. Ew
3. If the device is written back from image file, the device capacity needs to be equivalent. $;28
4. Usually used in the production of the boot magnetic chip. <s


13.9. Using cat to manipulate files

Red Hat Linux has a tool program that helps you keep a short list, collect these lists, and even give you a little information about your system.

This tool is called cat, which is shorthand for concatenate (chain) , which means merging files.

The Cat command also displays the contents of the entire file on the screen (for example, type cat filename.txt). If the file is longer, it will roll over on the screen very quickly. To prevent this effect from happening, use the cat filename.txt | less command.

Together with the pipe (|) and the less command, the file is displayed page by page. You can then use the UP or DOWN ARROW keys to move forward or backward through the page numbers. For more information about using pipelines to combine two different functions, see section 13.10.

13.9.1. Using re-orientation

Re-orientation is to enable the shell to change the standard output it determines, or to change the target of the standard output.

To redirect standard output, use the > symbol. Placing the > symbol after the Cat command (or after any tool and application written to the standard output) redirects its output to the file following the symbol.

For example, using cat itself will output what you have entered on the screen as if it were repeating the line you just typed. The following example shows that cat repeats every line you enter:

Figure 13-5. Cat Command

To redirect cat output to a file, type the following command at the shell prompt (press the [Enter] key to take you to a blank line):

Cat > Sneakers.txt

Figure 13-6. Redirect output to a file

Press the [Enter] key to go to a blank line, and then use the [Ctrl]-[d] key to exit cat.

Notice the difference in Figure 13-6? There is no duplicate display here. This is because cat's standard output has been re-directed. The redirect is a new document you just made, called Sneakers.txt .

When you run the Cat command, you will find files in your directory (if you want to enumerate Wu ┪? ls).

As shown earlier, you can use cat to read files. At the prompt, type:

Cat Sneakers.txt

Be careful when you redirect the output to a file, because you will easily overwrite an existing file! Unless you want to replace the file, make sure you create a different file name than the one that already exists.

Then redirect the output to another file and call it home.txt. In this example, type the command cat > Home.txt, and then press [Enter] followed by:

bring the coffee home
Take off Shoes
Put on sneakers
Make some coffee
relax!

Now, on a blank line, use the [ctrl]-[d] key combination to exit cat.

Next, use cat to connect home.txt and sneakers.txt , and then redirect the output of two files to a call saturday.txt New file (you will find an example in Figure 13-7). Type the following command:

cat Sneakers.txt home.txt > Saturday.txt

Figure 13-7. Connection file and redirect output

You can see that cat has added home.txt to the end of sneakers.txt .

13.9.2. Post-complement standard output

You can use output redirection to add new information after an existing file. This is similar to your use of the > symbol, where you are telling the shell to send information outside of the standard output.

However, when you use >>, you are adding information to the file instead of replacing the file contents completely.

The best way to explain this is to show you the instance. We'll use the two files we just created (sneakers.txt and home.txt) and then connect them using the post-complement output symbols. You want to add the home.txt information to the sneakers.txt information, so type:

cat Home.txt >> sneakers.txt

Now, use the cat sneakers.txt command to check. The final output shows the contents of the home.txt at the end of the file:

buy some sneakers
Then go to the coffee shop
Then buy some coffee
Bring the coffee Home
Take off Shoes
Put on sneakers
Make some coffee
relax!

The command you type home.txt the output of the file to the file sneakers.txt .

With the post-fill output, you save yourself time (and some disk clusters) by leveraging existing files without having to create new files.

Now compare the results of file sneakers.txt and file saturday.txt , and you'll see them exactly the same. To make a comparison, type:

Cat sneakers.txt; Cat Saturday.txt

The contents of these two files will be displayed-first the sneakers.txt file, then the saturday.txt file (as shown in Figure 13-8).

Figure 13-8. concatenating commands and comparing files

13.9.3. REDIRECT Standard input

Not only can you redirect standard output, you can also redirect standard input.

When you use the redirect standard input symbol <, you are telling the shell that you want to use a file as input to the command.

Use a file that you have created to demonstrate this concept. Type:

Cat < Sneakers.txt

Because the less-than sign (<) you use to separate the cat commands and files, thesneakers.txt output is read by the Cat command.

Figure 13-9. REDIRECT Standard input

Linux Cat 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.