In Linux, you can access and edit text files using a text editor that is designed to work with plain text. These files are not specifically coded or formatted.
Linux allows one to create a file in numerous ways. The fastest is, probably, Linux Command Line or Terminal. For all users—especially server administrators—who must rapidly generate text files, scripts, or configuration files for their work, this is a very important ability.
Let's proceed to the guide on four standard techniques for creating a text file on the terminal.
Ensure these prerequisites are met before generating files in a Linux environment using the command-line interface:
Access to a Functional Linux System: You must either have a Linux-based operating system installed on your computer or secure access to a Linux server via SSH (Secure Shell) protocol.
Operational Terminal Interface: Confirm that your terminal application is accessible and fully operational. The terminal serves as your primary gateway to executing commands.
Adequate User Permissions: Verify you can create files within the chosen directory. You may need to use sudo
(for directories with access restrictions) to escalate privileges.
Fundamental Commands Proficiency: You must get familiar with essential commands, such as touch
for file creation, echo
for printing text, cat
for viewing file contents, and text editors like nano
, vim
, or vi
for editing files directly.
Text Editing Utilities: Ensure your system includes text editing tools: nano
for command line simplicity, vim
for advanced configurations, or graphical options like gedit
for user-friendly navigation.
Directory Management Expertise: Develop familiarity with directory navigation commands like cd
for changing the working directory and ls
for listing directory contents. This knowledge streamlines your workflow and avoids potential errors.
Generally, we use the touch
command to create empty files and change timestamps. It will create an empty file if it doesn't exist already.
To create a text file in the current directory with the touch command:
Open your terminal emulator.
touch filename.txt
Change "filename" to your desired name. The timestamps for access and modification will be updated without changes in file content if the file exists already. Otherwise, an empty file is created with a given name.
Press Enter—if it is successful, there will be no output.
Use the ls
command to list the directory content and verify file creation.
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. For this, combine the echo
command with double redirect symbols (you can also use a single >
) and the desired filename.
A text file can be created by redirecting the output of the echo
command to a file. See how it works:
Open your terminal emulator.
Type the command:
echo “Your text content here” > filename.txt
Replace the text in double quotations (do not delete them) with yours to add it to the file.
After you press Enter, your text will be added to the file filename.txt
. It will overwrite an existing file, if there is one. Otherwise, it will just create a new one.
Press Enter.
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 by redirecting the standard output of cat
to a file.
Open your terminal emulator.
Type the following command:
cat > filename.txt
Replace filename.txt
with the name for your text file. This command instructs cat
to receive input rom 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. Press Enter after each line.
Press Ctrl + D when you are done. This signals the end of input to the cat
and saves the content.
Run the cat
command to check that the file has been created and contains the desired content.
Start using Hostman efficient S3 storage
The printf
utility is a powerful alternative to echo
, offering enhanced formatting options for structuring text. It allows users to create files with precisely formatted content.
Open the terminal.
Use printf
to define the text layout, incorporating formatting elements like newlines (\n
) or tabs (\t
). Redirect the output to a file using the >
operator.
Example:
printf "First Line\nSecond Line\nIndented\tThird Line\n" > formatted_file.txt
Run the cat
command to inspect the file's content and ensure the formatting matches expectations.
Append Without Overwriting: To add content to an existing file without overwriting its current data, replace >
with the append operator >>
:
printf "Additional content here.\n" >> formatted_file.txt
You can also create new files in linux text editors. There is always at least one integrated command-line text editor in your Linux distribution. But you can choose and install a different one according to your preferences, for example, Vim, Nano, or Emacs. Each of them has its own features and advantages.
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.
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:
Run nano
with the desired filename as an argument. It will open 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.
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
An intuitive text editor that supports working with plain text and has syntax highlighting for programming languages. A straightforward graphical interface makes it usable for various tasks, from quick edits to complex document preparation.
Open the Gedit Application: Launch Gedit either through the applications menu or by executing the following command in the terminal:
gedit example.txt
Gedit will create a new file if the specified one does not exist.
Input Your Text: Type or paste your desired content into the editor.
Save the File: Save your work with Ctrl + S or select File > Save. If creating a new file, specify a filename and a location.
Verify: Return to the terminal and confirm the file exists with the ls
command or review its content with cat
.
Ensure you have sufficient permissions to create files in the target directory. If they are insufficient, consider working in a directory where you have full rights (or elevate privileges with sudo
).
Check if a file with the identical name is already present before using the >
operator, as the command will overwrite existing content. To prevent data loss, opt for the append operator >>
.
Familiarize
yourself with the printf
, echo
, and text editors like vim
or nano
. These tools will help you reduce errors when working with files in Linux, as well as boost productivity.
Use printf
for creating files requiring structured content, such as configuration files or scripts with precise formatting needs.
Now you have acquainted yourself with the fundamental skill of creating a file in Linux using the terminal! Using the Linux command line, several fast and efficient methods exist to create and manage text files. Apply several techniques to meet a different requirement using the touch
, echo
, cat
, printf
commands, or text editors like vim
, nano
, gedit
, or emacs
. Users can select the method that sufficiently 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.