A Terminal-savvy person is mostly looking for ways to ditch the mouse. Also, they wouldn’t want to leave the comfort of the command line and go somewhere else to do any of their daily technical activities. There is always a way to do almost all of your stuff right inside the Terminal. So, why should creating text files be any different! Using the Terminal makes certain tasks more efficient and even faster. The command-line tools do not use too many resources and thus form great alternatives to the widely used graphical applications, especially if you are stuck up with older hardware.
Creating a text file is one task for which you can depend only on your keyboard on a Debian system. Three commands from the Linux command line are at your service for creating text files. These include:
- The cat command
- The touch command
- The standard redirect symbol
Let us explore these commands in this article to create some sample text files. The commands and procedures mentioned in this article have been run on a Debian 10 Buster system. Since we will be creating the text files using Debian command line-the Terminal; you can access is through the Application Launcher search as follows:
The Application Launcher can be launched through the Super/Windows key on your keyboard.
The cat Command
The cat command is very helpful when dealing with text files in Debian. It helps you in achieving three basic purposes:
- Creating a text file
- Printing contents of a text file in your Terminal
- Printing contents of a text file to another text file
Here, we will explore 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 next prompt will not appear; rather the cursor will display for you to enter the text for the file you just created.
Example:
In this example, I have created a text file through the following command and then entered some sample text:
$ cat > SampleTextFile.txt
Once you have entered all the text, hit Enter to move to the next line, and then use the Ctrl+D control to tell the system that you are done with entering the text. The usual command prompt will then appear for you to move on with further operations.
You can then use the ls command to see that your newly created text file will be there in the system.
$ ls
Through the cat command, you then view the contents of the file as follows:
$ cat "filename.txt"
Example:
You can see that the cat command shows the text I wrote while creating my sample file:
The touch command
Another way of quickly creating a text file through the Terminal is by using the touch command. The touch command, however, does not let you enter text in the file at the time of creation. After creating the file, you can enter the text through your favorite text editor. You might prefer the touch command over the cat command in one scenario; when you want to create multiple files at once through one command.
Lets us first see how to create a single file first through the Linux touch command:
$ touch “filename.txt”
Example:
$ touch sampletouchfile.txt
Use the ls command to see if the recently created file now exists on your system.
$ ls
Create multiple files at once with the touch command
As mentioned above, the touch command takes the lead on the cat command because you can create multiple files simultaneously through the former. Use the following syntax to do so:
$ touch “filename1.txt” “filename2.txt” “filename2.txt”….
For example, in the following command, I have created three files at once through the touch command:
$ touch sampletouchfile1.txt sampletouchfile2.txt sampletouchfile3.txt
I also checked the presence of the three files through the ls command in the above example.
If you want to edit any of the files you created through the touch command, you can use any of your favorite text editors. Here I am using the Nano editor to enter text to one of the files I created. I used the following command to open the file through the Nano editor.
$ nano sampletouchfile.txt
I then entered the text and saved it through pressing Ctrl+X and then by hitting Enter.
Using the Standard Redirect Symbol
The standard redirect symbol is usually 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 while creating a new file we do not specify any command before the redirect symbol.
The difference of using the standard redirect symbol for creating a text file is that unlike the ca command, you can not enter text in this way. Also, unlike the touch command, you can only create one file at a time through the redirect symbol.
Use the following syntax in order to create a text file through this symbol:
$ > “filename.txt”
You can then use the ls command to see if the newly created text file now exists on your system.
You can enter text in the file through your favorite text editor. In the following example, I am using the Vim editor to edit the file through the following command:
$ vim MyTextFile.txt
When you save and exit the file, your text file will have those contents saved.
Through this article, we have learned three basic ways to create text files quickly through the Linux command line. You can now avoid the mouse and use only the keyboard in order to perform a simple task of creating a text file in Debian.