Home > Operation and Maintenance > Linux Operation and Maintenance > There are several ways to write files in Linux

There are several ways to write files in Linux

藏色散人
Release: 2023-03-10 15:25:40
Original
4794 people have browsed it

There are 5 ways to write files in Linux, which are: 1. Create a file through the touch command; 2. Create a file using the vi and vim commands; 3. Use ">" and ">> " symbol to create a file; 4. Use the cp command to create a file; 5. Use cat to create a file.

There are several ways to write files in Linux

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

5 ways to create files in Linux

1. touch

1.1 Create one file

<p>touch yyTest.ini<br></p>
Copy after login
Copy after login

1.2 Create two at the same time File

<p>touch test1.txt test2.txt<br></p>
Copy after login

1.3 Create files in batches (such as creating 2000 files)

<p>touch test{0001..2000}.txt<br></p>
Copy after login

1.4 Change the file yyTest.ini time to the current time (yyTest.ini already exists)

<p>touch yyTest.ini<br></p>
Copy after login
Copy after login

2. vi and vim

<p>vi test.txt<br>vim touch.txt<br></p>
Copy after login

3. Use >, >>

##3.1 >

to directly overwrite the original file without any prompt

3.2 >>

Append to the end of the original file and will not overwrite the content of the original file

3.3 Directly use > to create an empty file

<p>> test.ini<br></p>
Copy after login
3.4 ls creation File (write the result to the file)

<p>ls > test.ini<br>ls >> test.ini<br></p>
Copy after login
3.5 grep Create file (write the result to the file)

<p>ps -ef | grep java >test.ini<br>ps -ef | grep java >>test.ini<br></p>
Copy after login
3.6 echo Create file (write the result to the file)

<p>echo $PATH > test.ini<br>echo $PATH >> test.ini<br></p>
Copy after login
4. Use cp to create a file

As long as the target file is a new file, the file will be created. For detailed explanation of the cp command, please see this blog post: https://www.php.cn/linux-502041.html

5. Use cat to create files

5.1 Simply use >, >>

<p>cat > test.ini<br>cat >> test.ini<br></p>
Copy after login
In fact, the practical ones are also > and >>, but there is one difference Yes, after typing the above command, you will enter the editing mode of test.ini. You can directly enter the content you want to write, and finally press ctrl z to exit the editing mode and save automatically

5.2 cat combined with eof

<p>cat >> test.ini <<eof<br/>2<br/>2<br/>2<br/>eof<br/></p>
Copy after login

eof can be used as a delimiter, stop typing when encountering the next delimiter; uppercase and lowercase characters are the same

5.3 cat combined with exit

Same as eof

<p>cat >> test.ini <<exit<br>1<br>1<br>exit<br></p>
Copy after login
Related recommendations: "

Linux Video Tutorial"

The above is the detailed content of There are several ways to write files in Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template