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

Ubuntu 18.04 MySQL 8.0 installation and configuration method graphic tutorial

This article shares the installation and configur...

Example code of setting label style using CSS selector

CSS Selectors Setting style on the html tag can s...

Docker sets up port mapping, but cannot access the solution

#docker ps check, all ports are mapped CONTAINER ...

9 Practical Tips for Creating Web Content Pages

Content 1. Give readers a reason to stay. Make the...

Detailed explanation of dynamically generated tables using javascript

*Create a page: two input boxes and a button *Cod...

How to change the mysql password on the Xampp server (with pictures)

Today, I found out while working on PHP that if w...

MySQL 5.7.18 winx64 installation and configuration method graphic tutorial

The installation of compressed packages has chang...

WEB Chinese Font Application Guide

Using fonts on the Web is both a fundamental skill...

How to install Nginx in CentOS7 and configure automatic startup

1. Download the installation package from the off...

JavaScript Basics Variables

Table of contents 1. Variable Overview 1.1 Storag...

Getting Started with Vue 3.0 Custom Directives

Table of contents 1. Custom instructions 1. Regis...