
Creating a file in Linux is basic requirement for users. In Linux, we can create a new file using the command line or from the desktop file manager. This tutorial explains how to create a new file in Linux using the command line.
Prerequisite
Make sure your logged in user have write permission on parent directory.
Before going to creating a new file, you can check the existing files and directories list using ls command
.
Create File with Touch Command
The touch command will create a new empty file and update the timestamps. It will work same for existing files as well. Simply run the following touch command to create a new file:
touch test.txt
In above command replace text.txt
with your real filename. If the file is exists in directory it will just update the timestamps other it will create and update timestamps.
You also can create multiple file in a single command, give file names separated by space:
touch test1.txt test2.txt test3.txt
Creating a File with cat Command
Generally, the cat command is used to read but it also can be used for creating new files. To create a new file run the cat command followed by the redirection operator >
and the name of the file you want to create. Press Enter
type the text and once you are done press the CRTL+D
to save the files.
cat > test1.txt
Create a File with echo Command
The echo command prints the strings that are passing as arguments to the standard output, which can be redirect to a file.
To create a new file run the echo
command followed by the text you want to print and use the redirection operator >
to write the output to the file you want to create.
echo "Test line" > test.txt
For empty file just run:
echo > test1.txt
Creating a File with the Redirection Operator
You can create a file using redirect symbol, which is usually used to redirect the output of a command to a new file or to another command. The >
operator will overwrite an existing file, while the >>
operator will append the output to the file.
For example, to create a empty new file, simply run the following command in your terminal:
> test1.txt
Conclusion
You learned how to create a new file in Linux using the command line with different methods.
If you have any question or feedback, leave a comment below.
Leave a Reply