A file is a container that stores data. In Linux, all the data is organized in the form of files and directories. Linux supports multiple types of file formats such as text files, image files, and audio files. In linux we cannot create files through GUI, the only method to create files is through the command line terminal.
This blog post will give you a brief description of how to make a file in Linux from the command line.
How to Make a File in Linux from the Command Line?
There are multiple methods to make a file in Linux from the terminal or command line. Some of them are demonstrated below:
- With Touch Command
- With Cat Command
- With Redirection Operator
- With Echo Command
- With Printf Command
- Using Text-Editor
Method 1: How To Make a New File in Linux from the Command Line Using touch Command
The syntax of the touch command is listed below:
$ touch filename.txt
It can be seen that the new file named new_file.txt has been created successfully.
Method 2: How To Make a New File in Linux from the Command Line Using Cat Command
The cat command is often used to read and view a file’s contents but it can also be used to create a new file.
The syntax of the cat command is listed below:
$ cat > filename.txt
Execute the above command and it will prompt you to write the content within that file. In our scenario, we have written “Welcome to Ubuntu. Here is a Beginner Guide for Ubuntu”. Now press Enter and shortcut key CTRL+D to save the file.
It can be seen that the new file workfile.txt has been created successfully.
To view its content, simply open the file.
By looking at the above screenshot, we can see that the new file and its content have been created and saved successfully.
Method 3: How To Make a New File in Linux from the Command Line Using Redirection Operator
To make a new empty file in the current folder and to save its contents, the redirection operator (>) is used. The syntax of the redirection operator is listed below:
$ > filename.txt
By looking at the screenshot below, we can see that the new file named Sampleworkfile.txt has been created successfully.
Method 4: How To Make a New File in Linux from the Command Line Using Echo Command
The echo command is one of the most frequently used commands in linux. The echo command creates a new file by taking a string as a parameter.
The syntax of the echo command is shown below:
$ echo "filecontent" > filename.txt
It can be seen that the cursor moved to the next line so the file main.txt has been created successfully.
To view the file’s content, open the file.
By looking at the above screenshot, we can see that the new file and its content have been created and saved successfully.
Method 5: How To Make a New File in Linux from the Command Line Using printf Command
The printf command functions just like the echo command, but it also provides more formatting options.
The syntax of the printf command is demonstrated below:
$ printf "filecontent.\n" > filename.txt
The above code depicts the following output.
By looking at the above screenshot, we can see that the file and its content have been created.
Method 6: How To Make a New File in Linux from the Command Line Using Text Editor
We can create and edit files using text editors such as vim, nano, etc. They allow us to easily create and manipulate text files.
The syntax is shown below:
$ nano filename.txt
Or
$ vim filename.txt
The execution of the above command opens up the nano editor as shown below.
Now write any content you want and press CTRL+ S to save and CTRL+ X to exit the nano editor
Note: This method is not preferred because it’s slow and time-consuming.
Conclusion
We can create text files with touch, cat, echo, printf commands, redirection operator, and text-editors, etc.
This article demonstrated multiple methods to create and manipulate linux text files