SoFunction
Updated on 2025-04-13

Five ways to create files in Linux

1. Introduction

This article introduces five ways to create files in Linux systems, including using the touch command, creating files with a text editor, creating files with an echo command, creating files with a cat command, and creating files with redirect symbols. Comprehensively understand and master various common methods of creating files in Linux systems.

The importance of file operations:

  1. In Linux systems, almost everything exists in the form of files, including devices, directories, processes, etc., so file operations are the core of system management.

  2. Many application configuration files and system configuration files exist in the form of text files.

  3. In Linux, many tasks can be automated by writing scripts, so file operations are crucial for scripting.

  4. Software development requires the operation of the file when processing source code files, configuration files, log files, etc.

  5. In Linux systems, it is very common to store and manage data through files, including database files, log files, user data, etc.

2. Use the touch command to create a file

The touch command is a command used to create an empty file or modify the timestamp of an existing file. Basic syntax:

touch [Options] file name

Options are optional and can be used to set the file's timestamp and other properties. The file name indicates the file name to create or modify the timestamp. The touch command can create multiple files at the same time, just follow the file name after the command.

Example: Create an empty text file named "" in the current directory.

touch 

This will create an empty file named "" in the current directory. If the file does not exist, the touch command will create an empty file; if the file already exists, the touch command will update the access and modify the timestamp of the file.

To create multiple files at the same time, just list all file names in the command:

touch   

This will create, and three empty files in the current directory.

The touch command has some options to facilitate more control over the file's timestamp. Common touch command options and usage:

  • -a, --atime: Updated file access time.
touch -a 
  • -m, --mtime: Update the modification time of the file (default option, if no options are specified, the touch command updates the modification time by default).
touch -m 
  • -c, --no-create: No files are created.
touch -c 
  • -t, --time: Use the specified timestamp to update the file time.
touch -t 202201011200.00 

-r, --reference: Use the timestamp of the reference file to update the file time.

touch -r reference_file.txt 

3. Create a file using a text editor

vi and nano are two common text editors. They are used a lot on Unix and Linux systems and provide powerful text editing functions in the command line environment.

Vi is an old-fashioned text editor with very powerful features and flexibility. The Vi editor has many commands and modes, such as command mode, insert mode, and last line mode. Some basic Vi commands:

  • i: Enter the insertion mode and insert text at the cursor
  • Esc: Exit the insert mode and return to command mode
  • :w: Save the file
  • :q: Exit Vi editor
  • :wq: Save and exit

In contrast, the Nano editor is easier to get started, providing a simple command line interface and common shortcut key operations. Basic operations of Nano editor:

  • Ctrl + O: Save the file
  • Ctrl + X: Exit Nano Editor
  • Ctrl + G: Get help
  • Ctrl + W: Search for text

Create a file using vi:

  1. Type in the command linevi Command, and press Enter to create a new file and enter the vi editor.
  2. Enter the command mode of the vi editor: Press the “i” key to enter the insertion mode.
  3. Type text content in insert mode.
  4. After completing the input, press the “Esc” key to exit the insertion mode.
  5. Use commands in command mode to save and exit the file, such as entering ":wq" and press Enter to save and exit.

Create a file using nano:

  1. Type in the command linenano Command, and press Enter to create a new file and enter the nano editor.
  2. Start typing text content directly in the nano editor interface.
  3. After completing the input, press the “Ctrl + O” key to save the file.
  4. Then press the "Ctrl + X" key to exit the nano editor.

4. Create a file using the echo command

echoBasic usage and syntax of commands:

Print text:

echo "Hello, World!"
# This will print "Hello, World!" on the terminal.

Print the value of the variable:

name="Lion"
echo "Hello, $name"
# This will print out "Hello, Lion".

Write text to the file:

echo "This is a line of text" > 
# This will write "This is a line of text" to a file named.  If the file already exists, it will be overwritten.

Append text to the end of the file:

echo "This is another line of text" >> 
# This will append "This is another line of text" to the end of the file.

The redirection feature of the echo command allows the output of the echo command to be sent to a file instead of being displayed on the terminal. This can be used to create a new file, write new content, or append content to existing files.

The redirect function includes two ways:

  • >: Write the output content of the command overwrite to the file, and create a new file if the file does not exist. For example:

echo "Hello, World!" > 
  1. This writes the string "Hello, World!" into a file named, creates a new file if the file does not exist, and overwrites if it already exists.

  2. >>: Append the output content of the command to the file, and create a new file if the file does not exist. For example:

echo "Additional content" >> 
  1. This appends the string "Additional content" to the end of the file, and creates a new file if the file does not exist.

5. Create a file using the cat command

The cat command (concatenate) is used to connect files and print their contents to a standard output device (usually a terminal). It can also be used to create files, append content to existing files, and merge multiple files into one file.

Basic syntax:

cat [Options] [document]

Common options:

  • -n: Show line number
  • -b: Show line numbers for non-blank lines
  • -e: Display the "$" symbol at the end of each line
  • -A: Show all special characters in full

use:

The easiest way to use it is to pass the file name as a parameter to the cat command, which will display the contents of the file to the terminal.

cat filename

Create a new file or redirect content to the file.

cat > 
This is a new file with some content

Append content to an existing file: Use the redirect symbol ">>" to append the output content of the cat command to an existing file.

cat >> 
This is additional content

Merge the contents of multiple files into one file.

cat   > 

Use the cat command to create a new file and write contents through redirection. Example:

  • Create a new file and write the contents:

cat > 
This is a new file created with the cat command.
  • Press Enter after the content. Then press the Ctrl+D key combination to save and exit. This creates a file named in the current directory and writes the specified content into it.

  • You can also write content to a new file in one line, as shown below:

cat >  <<END
This is a new file created with the cat command.
Some additional content.
END
  • Here document (<<END) is used to write the content to a new file and end the Here document at END. This also creates a file named and writes the specified content into it.

Knowledge extension: Some other cat command usage examples.

Show file content and line number: Show file content and line number before each line.

cat -n filename

Merge the contents of multiple files into one file: merge the contents of the sum into one file.

cat   > 

View the file content in the terminal and display it page by page, and continue reading by pressing the space bar.

cat filename | more

Merge the contents of multiple files and display the line number: merge the contents of the same and display the line number at the same time.

cat -n   > 

Visible special characters and ending characters: Display all special characters and ending characters in the file contents to help debug and analyze text files.

cat -A filename

6. Create a file using redirect symbols

Redirect symbol>and>>Used to write the output content of commands to a file in Linux and other Unix-like systems. The difference between them is:

  • Symbol (single greater than sign): This symbol creates a new file, and if the file already exists, it will be overwritten. If using the commandcommand > , it will write the output of the command to the file, create the file if the file does not exist, and overwrite the original content with the new output if the file already exists.

  • >> Symbol (double greater than sign): This symbol is appended to output to the end of the existing file. If using the commandcommand >> , it appends the output of the command to the end of the file, and creates the file if it does not exist.

Suppose there is a file named, runecho "Hello" > , it creates a new file and writes "Hello" into it. However, if runecho "World" >> , it appends "World" to the end of the file instead of overwriting the existing content.

Create a new file named and write "Hello, World!":

echo "Hello, World!" > 

Create a new file named and write multiple lines of text:

cat > 
This is a note.
It spans multiple lines.
Press Ctrl+D to finish and save.

Add content to an existing file (append to the end of the file):

echo "This is additional content" >> 

Uses of redirect symbols:

  • Writes the output of the command to a file for later viewing or processing.
  • Read content from a file and enter it into a command or program.
  • Append the output of the command to the end of the existing file, rather than overwriting the contents in the file.

Notes:

  • When using the > symbol, if the target file already exists, it will be overwritten and the contents in the file will be lost. Be careful to avoid accidentally overwriting important files.
  • When using the >> symbol, it appends the output to the end of the file instead of overwriting the contents in the file.
  • When the standard error output of the command needs to be redirected, you can use2>Symbol. For example,command 2> Save the error output of the command to a file.
  • You can also merge the standard output and standard error output of a command into the same file and use&>Symbol. For example,command &> Save both the standard output and standard error output of the command to a file.
  • The redirect symbol can also be used with the pipeline operator, passing the output of one command to another command and writing the result to a file.

7. Summary

Applicable scenarios:

  1. Creating files and writing contents using the echo command is suitable for quickly creating simple file content, especially single-line text or short content.

  2. Use the cat command to create a file and write multi-line text to be suitable for situations where multiple lines of text need to be entered, especially long text or content that needs to be entered manually.

  3. Appending content to the end of an existing file is suitable for situations where additional content needs to be added to an existing file, rather than overwriting the entire file.

  4. Writing the output of a command to a file using the > symbol is suitable for creating new files or overwriting existing file contents, but care is required to avoid accidentally overwriting important data.

  5. Appending the output of a command to the end of an existing file using the >> symbol is suitable for situations where the output needs to be appended to the end of an existing file.

Notes:

  • When using redirect symbols, be careful to avoid accidentally overwriting important files. It is recommended to back up files before performing overwrite operations.
  • When entering multiple lines of text using the cat command, use Ctrl+D to end the input to ensure that the text input is complete.
  • When using append symbols, make sure the target file already exists, otherwise a new file will be created.

The above is the detailed content of the five methods of creating files in Linux. For more information about creating files in Linux, please pay attention to my other related articles!