" and ">>" symbols; 4. Use cp Command to create a file; 5. Use cat to create a file."/> " and ">>" symbols; 4. Use cp Command to create a file; 5. Use cat to create a file.">

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

There are several ways to write files in Linux

藏色散人
藏色散人 Original
2023-03-10 10:07:21 4719browse

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

touch yyTest.ini

1.2 Create two at the same time File

touch test1.txt test2.txt

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

touch test{0001..2000}.txt

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

touch yyTest.ini

2. vi and vim

vi test.txt
vim touch.txt

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

> test.ini

3.4 ls creation File (write the result to the file)

ls > test.ini
ls >> test.ini

3.5 grep Create file (write the result to the file)

ps -ef | grep java >test.ini
ps -ef | grep java >>test.ini

3.6 echo Create file (write the result to the file)

echo $PATH > test.ini
echo $PATH >> test.ini

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: //m.sbmmt.com/linux-502041.html

5. Use cat to create files

5.1 Simply use >, >>

cat > test.ini
cat >> test.ini

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

cat >> test.ini <2
2
2
eof

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

cat >> test.ini <1
1
exit

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!

Statement:
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