linux Copies the contents of one file to the end of another

  • 2021-07-01 08:38:49
  • OfStack

Problem description:

For example, the file content of 11 is:

hello
The file content of 22 is:
world

Copy the contents of file 22 to the end of file 11, and the effect of file 11 is:

hello
world

Solution:

cat 22 > > 11

> > The meaning of "append" means "append"

> It means redirect and overwrites the original content

Small Tips:

Clear the contents of the a. txt file and make the file size 0 without deleting the file to:

cat /dev/null > a.txt

Extension of knowledge points:

linux outputs the end of the file to another file

" > "Redirect overwrites the original file;" > > "Append to the end of the file.

1. Redirect the standard output by using " > "Symbols, for example:

   dir my_dir > filelisting.txt

The standard screen output of the dir command will be redirected to the text file filelisting. txt file

2. Redirect the standard error, using the structure "2 > ", for example:

   dir my_dir 2> errorlisting.txt

The above command will send the standard output to the screen. If there is no error message, no information will be written to the errorlisting. txt file. If there is an error, nothing will be output to the screen, and the file errorlisting. txt will contain the error message.

3. dir my_dir > file_and_error_listing.txt 2 > & 1 ( & The command above redirects the standard output to a text file, and then redirects the standard error to the same location as the standard output.

4. You can also use the symbol "" (pipe command) to send the standard output of one command to the standard input of another command. In the following example, the standard output of the dir command is piped into the command more (automatically paused when the output is full): dir more

5. Write the standard output to both the file and the screen using the "tee" command: dir tee filelisting. txt

6. There is also a special file under Linux/dev/null, and all the information redirected to it will disappear without a trace. When we don't need to echo all the information of the program

You can redirect the output to/dev/null.

7. The following command directs both standard output and errors to a file

#ls /dev &> filename

" & "Here it stands for standard output and standard error, where both normal output and error messages are written to filename

8. Redefine the file identifier with i > & The j command, which means to redirect the file identifier i to j, you can put " & "Understood as" taking the address "

Look at the following example

#exec 5>&1

Indicates directing file identifier 5 to standard output, which is usually used to temporarily save standard input.

Linux tee Command Function Description: Read standard input data and output its contents to standard output and files.

语  法:tee [-ai][--help][--version][文件...]

Supplementary note: tee instruction will read data from the standard input device, output its contents to the standard output device, and save it as a file at the same time; If no file is specified after tee,

The tee supports only one or two outputs, similar to the T type used by plumbers.

Parameters:

-a or--append appends to an existing file instead of overwriting it.
-i-i or--ignore-interrupts ignores the interrupt signal.
-help online help.
--version displays version information.

Examples:

make 2 > & 1 | tee make.log

command > filename redirects standard output to a new file
command > > filename redirects standard output to 1 file (append)
command 1 > fielname redirects standard output to 1 file
command > filename 2 > & 1 Redirect standard output and standard error 1 to 1 file
command 2 > filename redirects standard errors to 1 file
command 2 > > filename redirects the standard output to a file (append)
command > > filename 2 > & 1 Redirect standard output and standard error 1 to 1 file (append)

Summarize


Related articles: