Table of contents
How to create files in linux:
1. Use the touch command;
2. Use vi and vim commands;
3. Use the >, >> commands;
4. Use the cp command;
5. Use the cat command.
1、touch
1.1 Create a file
touch test.sh
1.2 Create two files at the same time
touch test1.sh test2.sh
1.3 Create files in batches (such as creating 10 files)
touch test{01..10}.sh
1.4 Change the file test.sh time to the current time (test.sh already exists)
touch test.sh
2. vi and vim
vi test.sh
vim test.sh
3. Use >, >>
3.1 >
Overwrite the original file directly without any prompt
> test.sh
3.2 >>
Append at the end of the original file, the content of the original file will not be overwritten
3.3 ls create file (write result to file)
ls > test.sh
ls >> test.sh
3.4 grep to create a file (write the result to a file)
ps -ef | grep java > test.sh ps -ef | grep java >> test.sh
3.5 echo creates a file (writes the result to a file)
echo $PATH > test.sh echo $PATH >> test.sh
4. Use cp to create files
A file is created as long as the target file is a new file
Copy test.sh to test2.sh in the current directory
cp test.sh ./test2.sh
Copy the folder to the /home/cj directory
cp -r test.sh /home/cj
Copy the file test.sh to the /home/cj directory again, forcing overwriting
cp -f test.sh /home/cj
Copy the file test.sh to the /home/cj directory again and ask whether to overwrite
cp -i test.sh /home/cj
Copy the file test.sh to the /home/cj directory, and copy the modification time and access rights
cp -p test.sh /home/cj
5. Use cat to create files
5.1 Simple use >, >>
cat > test.sh cat >> test.sh
In fact, > and >> are also practical, but there is a difference that after typing the above command, you will enter the edit mode of test.sh, you can directly enter the content you want to write, and finally press Enter, ctrl+z to exit Edit Mode Autosave
5.2 cat combined with eof
cat >> test.sh << eof >2 >2 >2 >eof
eof can be used as a delimiter, stop input when encountering the next delimiter; same case
5.3 cat combined with exit
Same as eof
cat >> test.sh << exit >1 >1 >1 >exit
This article refers to: How to create files in linux-linux operation and maintenance-PHP中文网