How to Create a Text File in Linux Terminal
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.
There are several different ways to create a file in Linux. The Linux Command Line or Terminal is most likely the fastest. This is a crucial skill for any user, but especially for server administrators, who need to create text files, scripts, or configuration files quickly for their jobs.
Let's proceed to the guide on four standard techniques for creating a text file on the terminal.
And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.

File Creation in Linux Can be Frustrating Sometimes
Prerequisites for File Creation in Linux Copy link
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
touchfor file creation,echofor printing text,catfor viewing file contents, and text editors likenano,vim, orvifor editing files directly. -
Text Editing Utilities: Ensure your system includes text editing tools:
nanofor command line simplicity,vimfor advanced configurations, or graphical options likegeditfor user-friendly navigation. -
Directory Management Expertise: Develop familiarity with directory navigation commands like
cdfor changing the working directory andlsfor listing directory contents. This knowledge streamlines your workflow and avoids potential errors.
Using the touch Command Copy link
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.
- Type the command:
touch filename.txtStart with "touch" command
Replace "filename" with the name you picked for the file. If the file with the same name already exists, the access and modification timestamps will be updated without affecting the content of the file. If not, a blank file with the specified name will be generated.
-
Press Enter—if it is successful, there will be no output.
-
Use the
lscommand to list the directory content and verify file creation.
"LS" command is also important of you want to generate text file in Linux
Using the echo Command Redirection Copy link
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"Echo" command is also important in the process
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
catcommand to display the content.
"Cat" command can help you to display your file you just created
Using the cat Command Redirection Copy link
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.txtThis is what you'll see after "cat" command
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
catand saves the content. -
Run the
catcommand to check that the file has been created and contains the desired content.
This is how you can check how your file in Linux is created
Using printf for Advanced File Creation Copy link
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
printfto 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
catcommand 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.txtUsing a Text Editor Copy link
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 Copy link
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.
"Vim" command is one of the key steps in file creation
-
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.
Simple command to finish your work
Nano Copy link
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
nanowith the desired filename as an argument. It will open a new buffer for editing the filefilename.txt.
Nano is useful in you want to fix something in your text file
-
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.
Click "yes" to exit
Emacs Copy link
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.
"Emacs" is more flexible text editor
-
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.
Don't forget to install necessary command in Linux
To resolve this, install the text editor first using the command:
apt-get install vimapt-get install nano apt-get install emacsGedit Copy link
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.txtGedit 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
lscommand or review its content withcat.
Linux File Creation Recommendations Copy link
-
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 likevimornano. These tools will help you reduce errors when working with files in Linux, as well as boost productivity. -
Use
printffor creating files requiring structured content, such as configuration files or scripts with precise formatting needs.
Conclusion Copy link
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.
Frequently Asked Questions (FAQ) Copy link
How do I create an empty text file in Linux? Copy link
The standard command is touch. Simply run: touch filename.txt This creates a blank file immediately.
How do I create a file and add content at the same time? Copy link
You can use the echo command with the redirection operator (>). echo "Hello World" > filename.txt This creates the file and puts "Hello World" inside it.
How do I create and open a file for editing? Copy link
Use a terminal text editor like nano or vi. When you run: nano filename.txt Linux will open a blank editor screen. Once you type your text and save (Ctrl+O in nano), the file is created on your disk.
What is the fastest way to create a file? Copy link
The redirection symbol alone is the quickest method for creating an empty file:> filename.txt This tells the shell to redirect "nothing" into a new file, creating it instantly.
How do I create a large file for testing? Copy link
Use the fallocate command. For example, to create a 1GB file instantly:fallocate -l 1G bigfile.img
How do I view the content of a text file? Copy link
Use the cat command to print the text to your terminal: cat filename.txtFor longer files, use less filename.txt to scroll through pages.