Reference article:
Various ways to create files on Linux systems
When it comes to creating files on a Linux system, here are some more detailed explanations of some common methods:
-
Use the touch command:
touch filename
- This command is used to create a new empty file, or to update the access and modification timestamps of an existing file. If the specified file does not exist, it will be created.
-
Using a text editor:
- Command-line text editors: such as vi, nano, etc. By running
vi filename
ornano filename
opening a file and editing the contents in it. - Graphical interface text editor: such as gedit, Sublime Text, etc. Open the editor and use its interface to create and edit files.
- Command-line text editors: such as vi, nano, etc. By running
-
Use the echo command:
echo "content" > filename
- Write the specified content to the file through the echo command.
>
The symbol means to redirect the output to a file, or create a new file if it does not exist. Note that this overwrites existing content in the file. If you want to append content without overwriting the file, use>>
the notation.
- Write the specified content to the file through the echo command.
-
Use the cat command:
cat > filename
- Create a new file using the cat command combined with redirection. After running the command, enter the contents of the file line by line. Press Ctrl+D to end the input and save the file. If the file already exists, it will be overwritten.
-
Use the cp or mv command:
cp source_file destination_file
ormv source_file destination_file
- Use the cp command to copy files, or use the mv command to move files. If the target file does not exist, a new one is created. The source file can be an existing file or directory.
-
Use file redirection notation:
command > filename
- Create a file by redirecting the output of a command into a file. After running the command, the output will be written to the specified file. For example,
ls > filelist.txt
write the file list of the current directory into the filelist.txt file. Note that this also overwrites existing content in the file. If you want to append content without overwriting the file, use>>
the notation.
- Create a file by redirecting the output of a command into a file. After running the command, the output will be written to the specified file. For example,
-
Use the file compression command:
- Use the tar command:
tar -cvf archive.tar files
Create a tar archive by running the command and pack the specified files into the archive. - Use the zip command:
zip archive.zip files
Create a zip archive by running the command, and add the specified files to the archive.
- Use the tar command:
These are common ways to create files on Linux systems, you can choose the method that suits you according to your needs.