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.
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:
Open your terminal emulator.
Type the command:
touch filename.txt
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.
Press Enter. The command will return without any output if it is successful.
To verify that the file has been created, use the command ls
to list the content of the current directory.
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:
Open your terminal emulator.
Type the command:
echo “Your text content here” > filename.txt
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 (“
).
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.
To verify that the file has been created and contains the desired content, use cat
command to display the content.
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:
Open your terminal emulator.
Type the following command:
cat > filename.txt
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
Press Enter. The terminal will be waiting for input.
Enter the text you want in the file by typing it and press Enter after each line.
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.
To verify that the file has been created and contains the desired content, use the cat
command to display the content.
Start using Hostman efficient S3 storage
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
, 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:
Open vim
, with the desired filename as an argument with a new buffer for editing the file filename.txt
.
Press i to switch to Insert mode.
Start typing and editing the filename.txt
.
To save and exit, press Esc to ensure that command mode is running. Type: wq
(write and quit) and press Enter.
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:
Open nano
, with the desired filename as an argument with a new buffer for editing the file filename.txt
.
Start typing and editing the filename.txt
.
To save and exit, press Ctrl + O to write the file, confirm the filename, and then press Ctrl + X to exit Nano.
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:
Open emacs
, with the desired filename as an argument with a new buffer for editing the file filename.txt
.
Start typing and editing the filename.txt
.
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
oremacs
text editor is not installed on the system, or it's not included in thePATH
environment variable, which is a list of directories where the operating system looks for executable files.
To resolve this, install the text editor first using the command:
apt-get install vim
apt-get install nano
apt-get install emacs
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.
Hostman offers a reliable managed Linux VPS for your projects.