How to create an empty file from the bash shell or command line in Linux? Can you tell me the Linux command to create an empty file in Linux?
The empty file means files with zero bytes and in a plain text format. You can create an empty file from the command line using various methods.
- Use the touch command to create a blank file
- Use redirection with stdout
- Use echo command to create a empty file
- How to use printf to create a blank file
1. Use the touch command to create a blank file
The Linux touch command is used to create a file without any content. The file created using the touch command is empty with zero bytes. This command can be used when the user doesn’t have data to store at the time of file creation. Touch Linux command can also be used to change and modify the timestamps of a file.
Syntax:
touch filename
Example:
touch myfile.txt
2. Use redirection with stdout
We can redirect a command’s output (stdout) to a file for creating an empty file.
Syntax 1: (create)
command > output.txt
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten. If the file does not exist, it will create a file.
Example:
echo "first data 1" > myfile.txt
Syntax 2: (create and append)
command >> output.txt
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file. If the file does not exist, it will create a file.
Example:
echo "first data 2" >> myfile.txt
3. Use echo command to create a empty file
The echo Linux command writes its arguments to standard output, followed by a newline. If there are no arguments, only the new line is written.
Syntax:
echo -n > filename
Example:
echo -n > myfile.txt
4. How to use printf to create a blank file
Linux command printf prints a formatted string to the standard output.
Syntax:
printf '' > filename
Example:
printf '' > myfile.txt