Copy the contents of one file to the end of another file in linux

Copy the contents of one file to the end of another file in linux

Problem description:

For example, the content of file 11 is:

hello
The content of file 22 is:
world

Copy the contents of file 22 to the end of file 11. The effect of file 11 is:

hello
world

Solution:

cat 22 >> 11

>> means to add

> means redirection, which will overwrite the original content

Tips:

To clear the contents of a.txt file and make the file size 0 without deleting the file, you can:

cat /dev/null > a.txt

Knowledge point expansion:

Linux outputs the end of a file to another file

">" redirection overwrites the original file; ">>" appends to the end of the file.

1. To redirect standard output, you can use the ">" symbol, for example:

dir my_dir > filelisting.txt

Will redirect the standard screen output of the dir command to the text file filelisting.txt

2. To redirect standard error, you can use the structure "2>", for example:

dir my_dir 2> errorlisting.txt

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

3. dir my_dir > file_and_error_listing.txt 2> & 1 (&1 means the same as before) The above command first redirects the standard output to the 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 more command (which automatically pauses when the output fills the screen): dir | more

5. Use the "tee" command to write standard output to the file and the screen at the same time: dir | tee filelisting.txt

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

When , you can redirect the output to /dev/null.

7. The following command directs both standard output and error to files

#ls /dev &> filename

"&" here represents standard output and standard error. Both normal output and error information are written to filename.

8. To redefine a file identifier, you can use the i>&j command, which means redirecting the file identifier i to j. You can understand "&" as "get address"

Please see the following example

#exec 5>&1

Indicates that file identifier 5 is directed to standard output. This command is usually used to temporarily save standard input.

Linux tee command function description: reads standard input data and outputs its contents to standard output and files.

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

Additional note: The tee command reads data from the standard input device, outputs its contents to the standard output device, and saves it as a file; if no file is specified after tee,

It only outputs its contents to the standard output device. tee only supports single or double output, similar to the T-type pipe used by plumbers.

parameter:

-a or --append Append to an existing file instead of overwriting it.
-ii or --ignore-interrupts Ignore interrupt signals.
--help Online help.
--version Display version information.

example:

make 2>&1 | tee make.log

command > filename redirects standard output to a new file
command >> filename redirects standard output to a file (append)
command 1 > filename redirects standard output to a file
command > filename 2>&1 redirects both standard output and standard error to a file
command 2 > filename redirects standard error to a file
command 2 >> filename redirects standard output to a file (append)
command >> filename 2>&1 redirects standard output and standard error to a file (append)

Summarize

The above is what I introduced to you about how to copy the contents of a file to the end of another file in Linux. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to copy files between two Linux servers and log in without password
  • Detailed explanation of cp command and scp command for file copying in Linux
  • Linux copy file command cp usage explanation
  • Two methods to copy files between different users in Linux
  • Tips for copying files with scp in Linux without entering a password

<<:  Chrome plugin (extension) development guide (complete demo)

>>:  Detailed steps to install MySQL on CentOS 7

Recommend

Use of MySQL truncate table statement

The Truncate table statement is used to delete/tr...

MySQL 8.0.18 uses clone plugin to rebuild MGR implementation

Assume that a node in the three-node MGR is abnor...

JavaScript to achieve skin effect (change background)

This article shares the specific code of JavaScri...

Usage and best practice guide for watch in Vue3

Table of contents Preface🌟 1. API Introduction 2....

Ideas and practice of multi-language solution for Vue.js front-end project

Table of contents 1. What content usually needs t...

How to install and deploy gitlab server on centos7

I am using centos 7 64bit system here. I have tri...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

Vue echarts realizes horizontal bar chart

This article shares the specific code of vue echa...

Detailed explanation of the use of MySQL group links

Grouping and linking in MYSQL are the two most co...

How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

Today, I logged into the server and prepared to m...

Nginx routing forwarding and reverse proxy location configuration implementation

Three ways to configure Nginx The first method di...

How to change the domestic source of Ubuntu 20.04 apt

UPD 2020.2.26 Currently Ubuntu 20.04 LTS has not ...

Example code for implementing hexagonal borders with CSS3

The outermost boxF rotates 120 degrees, the secon...