Log In

4 Ways to Create a Text File in Linux Terminal

4 Ways to Create a Text File in Linux Terminal
26.02.2024
Reading time: 6 min
Hostman Team
Technical writer

A text file is a type of file in Linux that contains plain text and can be opened and edited with a text editor. There's no particular coding or formatting in it. 

There are several ways to create a file in Linux. The fastest way is to use the Linux Command Line or Terminal. This is a fundamental skill for all users especially server admins, who need to quickly create text files, scripts, or configuration files for their work.  

Here are the four common methods on how to create a text file in the terminal.

1. Using the touch command

The touch command in Linux is generally used to change the access and modification times of files. If the file doesn't exist, touch creates an empty file. 

To create a text file with the touch command in the Linux terminal, follow these steps:

  1. Open your terminal emulator.

  2. Type the command:

touch filename.txt

Image5

Replace filename.txt with the name for your text file. If the file already exists, touch will update the access and modification times without changing its content. If the file does not exist, touch will create an empty file with the given name. 

  1. Press Enter. The command will return without any output if it is successful.

  2. To verify that the file has been created, use the command ls to list the content of the current directory. 

24a059f9 C535 4422 8f95 16b7dca87d8a

2. Using the redirection with echo command

The echo command is widely used to display text on the terminal. But its capabilities go beyond that; it may also be used to write content to a file or create an empty file. To do this, the echo command is used in conjunction with double redirect symbols (single > can also be used) followed by the desired filename. 

To create a text file using the echo command in Linux, redirect the output of echo to a file. Here's the step-by-step process:

  1. Open your terminal emulator.

  2. Type the command:

echo “Your text content here” > filename.txt

Image8

Replace "Your text content here" with the text you want to add to the file. Make sure the text content is enclosed in double quotations (). 

  1. Press Enter. The echo command will write the specified text to the file filename.txt. If the file already exists, it will be overwritten with the new content. If the file does not exist, it will be created.

  2. To verify that the file has been created and contains the desired content, use cat command to display the content. 

Df931fbe B2fd 408f Ae95 A53318c0426d

3. Using the redirection with cat command

In Linux, the cat command is mostly used to concatenate and show file contents. It can, however, also be used to generate a text document. To create a text file using redirection with the cat command, redirect the standard output of cat to a file. Here's the step-by-step process:

  1. Open your terminal emulator.

  2. Type the following command:

cat > filename.txt

74a3a2f1 976f 44d4 Ba84 Cf082370f6c1

Replace filename.txt with the name for your text file. With the help of this command, cat is instructed to begin receiving input from the terminal and to redirect it into the filename.txt

  1. Press Enter. The terminal will be waiting for input. 

  2. Enter the text you want in the file by typing it and press Enter after each line. 

  3. Press Ctrl + D after entering the text you want in the file. This signals the end of input to the cat and saves the content. 

  4. To verify that the file has been created and contains the desired content, use the cat command to display the content. 

738f5fb6 Fed2 4c4a 8ffd 5760919a9f5c

4. Using a text editor

There is always at least one integrated command-line text editor in Linux distributions. You can also install many command-line text editors to benefit from their distinct features and advantages. Vim, Nano, and Emacs are the three terminal-based text editors that are most widely used in Linux. 

Vim

vim, which stands for "Vi IMproved," is a very flexible and adaptable text editor. It is well-known for its modal editing, which allows for distinct modes for various functions like text entry, navigation, and editing. It allows split windows, multiple buffers, syntax highlighting, and a large selection of plugins for extra features. To create a text file using vim, follow the steps below: 

  1. Open vim, with the desired filename as an argument with a new buffer for editing the file filename.txt.

Image12

  1. Press i to switch to Insert mode.

  2. Start typing and editing the filename.txt

  3. To save and exit, press Esc to ensure that command mode is running. Type: wq (write and quit) and press Enter.

Image2

Nano

nano is ideal for short adjustments and straightforward text files because it is lightweight and requires little setup. It provides support for basic text manipulation functions, search and replace, and syntax highlighting. To create a text file using nano, follow the steps below: 

  1. Open nano, with the desired filename as an argument with a new buffer for editing the file filename.txt.

0c16ba2d 440f 4824 Be3a A36b2ea47a6e

  1. Start typing and editing the filename.txt

  2. To save and exit, press Ctrl + O to write the file, confirm the filename, and then press Ctrl + X to exit Nano.

Sqdwefrthyuy

Emacs

emacs is a powerful and flexible text editor that supports syntax highlighting, multiple buffers, split windows, and integration with external tools and programming languages. To create a text file using emacs, follow the steps below: 

  1. Open emacs, with the desired filename as an argument with a new buffer for editing the file filename.txt.

  2. Start typing and editing the filename.txt

Image7

  1. To save and exit, press Ctrl + X, followed by Ctrl + S to save the file, and then Ctrl + X, followed by Ctrl + C to exit Emacs.

Note: If a message states that "VIM command not found", "nano command not found" or "emacs command not found" in Linux, it typically means that the vim, nano or emacs text editor is not installed on the system, or it's not included in the PATH environment variable, which is a list of directories where the operating system looks for executable files.

5239213a 9fb4 42e4 8806 Abda7f2d4a81

To resolve this, install the text editor first using the command: 

apt-get install vim
apt-get install nano 
apt-get install emacs

Conclusion

Creating a file in Linux using the terminal is a fundamental skill that employs commands and command-line text editors. There are various quick and effective ways to create and manipulate text files using the Linux command line. These methods, such as using the touch command, echo command, cat command, or text editors such as vim, nano, or emacs, provide different strategies to fulfill a different demand. Users can select the method that best meets their requirements, such as creating empty files, appending text, or significantly modifying material. In summary, any of these methods enable Linux users to easily and quickly handle text files straight from the command line.


Share