Text files can be viewed and edited using any text editor that exists in Linux. However, before learning the editing part, we must have a basic understanding of different ways that can be used to create plain text files. Depending on the requirement, different methods can be used for text file creation. The most popular ones are described next.
Create a text file using the cat command
The cat
command can be used to create a text file if we immediately want to add some text to a new blank file. The syntax of the cat
command to create a file is as follows:
$ cat > demo.txt
After pressing Enter, we will return to the prompt and we can directly start inserting text into our file. Once you are done entering text in the file, press Ctrl + D to mark the end of the file and return to the prompt, as shown in the following screenshot:
If you want to use the cat
command to create a file from a bash script, we have to use an operator known as a here document. It can be any arbitrary string that can be used to mark the beginning of a file and end when repeated in a new line, as shown in the following screenshot:
Create an empty text file using the touch command
The original purpose of the touch
command is to update a file’s timestamp to the current date and time without modifying it. The touch
command can also be used to create an empty file of size 0 bytes. We cannot enter any text in the file with the touch
command, but we can create multiple new files with a single command. It is quite often used to create files that are intended to be used in future. The syntax of the touch
command is as follows:
$ touch <path_for_empty_file>
Examples of the touch
command are shown in the following screenshot:
Create a text file using the redirection symbol (>)
We can also create a text file using the redirection symbol (>
), which is used to redirect the output of a command to a file. If we use the redirection symbol alone without prefixing it with any command, then it will create an empty file of 0 bytes and remove the file’s content if a file already exists with the given name, as shown in the following command line:
$ > demo.txt
If we prefix the redirection symbol (>
) with any command, then it creates a new file, which contains the output of the command preceding the redirection symbol, as shown in the following screenshot:
We can create one file at a time with the redirection symbol.
Create a text file using the echo or printf command
Sometimes, it is necessary to create a short file that doesn’t require us to invoke the full text editor. In those scenarios, the echo
or printf
command is used with the redirection operator to create an empty file, or a file with a single line. This method of creating a file can be used in scripts also. Use the echo
and printf
command is shown in the following screenshot:
Create a text file using the vi editor
The vi editor is the most popular command-line editor and is the default editor in most Linux distributions. It has three modes of operation, which will be discussed later on in this chapter. To create a file with the vi editor, follow these steps:
vi
<filename> and pressEnter- PressIto enter Insert Mode and add your text
- Keep on adding the custom text to the file until you are done with it
- Press the Esckey to enter Command Mode
- Type
:wq
and pressEnterto save and exit
The following screenshot displays a file being created in vi editor:
Besides this method, there are other ways of creating a text file, such as using nano
, ed
, joe
, emacs
, or pico
editors. You can try your hand at using them too.