The terminal-savvy person is mostly looking for ways to get rid of the mouse. In addition, they would not want to leave the command line and go somewhere else to carry out any of their daily technical activities. There is always a way to do almost all of your things right in the Terminal. So why the creation of text files should be different! Using the Terminal makes certain tasks more efficient and even faster. The command line tools do not consume too many resources and, thus, form excellent alternatives to widely used graphical applications, especially if you are stuck on old hardware.
Creating a text file is one of the tasks you can depend on only your keyboard on a Debian system. Three Linux command line commands are at your service for creating text files. These include:
- Cat team
- Touch team
- Standard redirection character
Let’s look at these commands in this article to create some sample text files. The commands and procedures mentioned in this article ran on the Debian 10 Buster system. Since we will create text files using the Debian command line – Terminal; You can access it through the Launcher app search as follows:
The application launcher can be launched using the Super / Windows key on the keyboard.
The cat command is very useful when working with text files in Debian. This helps you achieve three main goals:
- Create text file
- Printing the contents of a text file in your terminal
- Print the contents of a text file to another text file
Here we look at the first use of the cat command; creating a text file through the command line.
Enter the following command in your terminal:
$ cat > "filename.txt"
After entering this command, the following prompt will not appear; instead, a cursor appears to enter text for the file you just created.
In this example, I created a text file using the following command, and then entered a sample text:
$ cat > SampleTextFile.txt
Once you have entered all the text, press Enter to go to the next line, and then use the Ctrl + D key to tell the system that you are finished with text input. Then a regular command line will appear to continue further operations.
Then you can use the ls command to see that your newly created text file will be there on the system.
Using the cat command, you then view the contents of the file as follows:
$ cat "filename.txt"
You can see that the cat command shows the text that I wrote when creating the example file:
Another way to quickly create a text file through the Terminal is to use a touch command. However, the touch command does not allow text to be entered into the file during creation. After creating the file, you can enter text through your favorite text editor. You may prefer the touch command to the cat command in one scenario; when you want to create multiple files at once with a single command.
Let’s first see how to first create a separate file using the Linux touch command:
$ touch “filename.txt”
$ touch sampletouchfile.txt
Use the ls command to see if a newly created file exists on your system.
Create multiple files at once using touch command
As mentioned above, the touch command takes the lead in the cat command because you can create multiple files at once through the first. To do this, use the following syntax:
$ touch “filename1.txt” “filename2.txt” “filename2.txt” ….
For example, in the following command, I created three files at the same time using the touch command:
$ touch sampletouchfile1.txt sampletouchfile2.txt sampletouchfile3.txt
I also checked for the presence of three files using the ls command in the above example.
If you want to edit any of the files created using the touch command, you can use any of your favorite text editors. Here I use the Nano editor to enter text into one of the files I created. I used the following command to open the file through the Nano editor.
$ nano sampletouchfile.txt
Then I entered the text and saved it by pressing Ctrl + X and then pressing Enter.
Using the standard redirection character
The standard redirection character is commonly used when redirecting the output of a command to a file. However, it can also be used to create a single text file. The only difference is that when creating a new file, we do not specify any commands before the redirection symbol.
The difference in using a standard redirection character to create a text file is that unlike the ca command, you cannot enter text in this way. In addition, unlike a touch command, you can only create one file at a time using the redirection symbol.
Use the following syntax to create a text file through this character:
$ > “filename.txt”
You can then use the ls command to see if a newly created text file exists on your system.
You can enter text into a file through your favorite text editor. In the following example, I use the Vim editor to edit the file with the following command:
$ vim MyTextFile.txt
When you save and exit the file, this content will be saved in your text file.
In this article, we learned about the three main ways to quickly create text files using the Linux command line. You can now drop the mouse and use only the keyboard to complete the simple task of creating a text file in Debian.
Quickly create a text file through a Debian terminal