Learning Center
Linux

How to Create a Text File in Linux Terminal

21 Jan 2026
JC Brian Refugia
JC Brian Refugia

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.

Text file creation in Linux

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:

  1. 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.

  2. Operational Terminal Interface: Confirm that your terminal application is accessible and fully operational. The terminal serves as your primary gateway to executing commands.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

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:

  1. Open your terminal emulator.

  2. Type the command:

Image5

Start 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.

  1. Press Enter—if it is successful, there will be no output.

  2. Use the ls command to list the directory content and verify file creation.

24a059f9 C535 4422 8f95 16b7dca87d8a

"LS" command is also important of you want to generate text file in Linux

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:

  1. Open your terminal emulator.

  2. Type the command:

Image8

"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.

  1. Press Enter.

  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

"Cat" command can help you to display your file you just created

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.

  1. Open your terminal emulator.

  2. Type the following command:

74a3a2f1 976f 44d4 Ba84 Cf082370f6c1

This 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.

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

  2. Enter the text you want in the file. Press Enter after each line.

  3. Press Ctrl + D when you are done. This signals the end of input to the cat and saves the content. 

  4. Run the cat command to check that the file has been created and contains the desired content.

738f5fb6 Fed2 4c4a 8ffd 5760919a9f5c

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.

  1. Open the terminal.

  2. 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
  1. 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:

  1. Open vim, with the desired filename as an argument.

Image12

"Vim" command is one of the key steps in file creation

  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

Simple command to finish your work

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. Run nano with the desired filename as an argument. It will open a new buffer for editing the file filename.txt.

0c16ba2d 440f 4824 Be3a A36b2ea47a6e

Nano is useful in you want to fix something in your text file

  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

Click "yes" to exit

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.

  2. Start typing and editing the filename.txt

Image7

"Emacs" is more flexible text editor

  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.

5239213a 9fb4 42e4 8806 Abda7f2d4a81

Don't forget to install necessary command in Linux

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

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

Gedit
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.

  1. 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.

  1. Input Your Text: Type or paste your desired content into the editor.

  2. Save the File: Save your work with Ctrl + S or select File > Save. If creating a new file, specify a filename and a location.

  3. Verify: Return to the terminal and confirm the file exists with the ls command or review its content with cat.

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 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.

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.