How to Create a Text File Using the Command Line in Linux
Last Updated :
28 Aug, 2024
The Linux command line is a powerful tool that allows you to manage your system with precision and speed. One of the most common tasks is creating a text file, which can be achieved using several commands and methods.
Here we will learn some quick ways to create a text file from the Linux Command Line or Terminal.
1. Using the ‘touch’ command
The command is quite simple to type and makes it quite easy to create several text files at once. The commands are as follows:
touch filename.txt
Just type the word touch followed by the name of the file you like to give it, and you have created an empty text file inside of a terminal. You can add the file names of the file you would like to create at once with space in between each filename. The command below creates three empty files at once using the touch command and you can create as many files as you like.
touch file1.txt file2.txt file3.txt

touch command

touch command for creating multiple files
Note: The ‘touch’ command is perfect when you need to create several empty files quickly without adding any content.
2. Using the Standard Redirect Symbol (>)
Creating a text file in the terminal is a process that requires minimal effort. The command involves using the standard redirect symbol (>) followed by a spacebar and the desired file name.
> filename.txt
If you want to create several text files at once, then you can add the redirect symbol after the previous filename and chain the command repeatedly to create multiple empty files.
> file.txt > file2.txt > file3.txt
The above command creates three empty text files. The redirect symbol is quite time-saving if you just want to create a single text file. It gets quite longer than the touch command to create multiple empty text files.

Using the redirect symbol for creating files.
Note: While this method is quick for creating single files, it can become tedious when creating multiple files, especially compared to the ‘touch’ command.
3. Using the ‘cat’ Command
This method is incredibly simple and user-friendly. To create a new text file using this method, all you need to do is type is “cat” followed by two redirect symbols (>>) and the desired file name.
While it is not necessary to use the ‘>>’ symbols, you can also use a single ‘>’ symbol. However, caution must be exercised when using a single ‘>’ symbol, as it can inadvertently overwrite the existing content in the text file if the file already exists.
The below command creates an empty yet edited file as it prompts the user to create a text file and type in the file at the same time. So, if you do not want to edit the file, simply press ‘CTRL+C’ and it will simply exit and create an empty file.
cat >> file.txt
But, if you would like to add some text to the file, you can type in after this, like this:
cat >> new.txt
This is some text in the file from command line.
To stop editing and saving in the file, simply type ‘CTRL+C’, it will create, save and exit the file. So, this method is quite a time-saving method if you want to edit the text file very quickly. The following command will append the text to the pre-existing file.
On the other hand, if you use a single redirect symbol(>) it will overwrite the content of the file, so you only prefer using double redirect symbols for safety reasons.

Using cat command to create the file.

Using cat command to create and write a file.
Note: This method combines the functionality of the touch and redirect symbol commands. This is useful if you want to create and add content to the file in one step without opening a text editor.
4. Using ‘echo’ and ‘printf’ Commands
The ‘echo’ command, similar to the ‘cat’ command but more versatile, is commonly employed to display text on the terminal. However, its functionality extends beyond that, as it can also be used to write content to a file or create an empty file. To accomplish this, the echo command is utilized in conjunction with double redirect symbols (single “>” can also be used) followed by the desired filename.
echo >> filename.txt
If you want to create multiple files at a time, you can chain up the command as in previous methods.
echo >> file1.txt >> file2.txt >> file3.txt
We can also add functionality to the ‘echo’ command to quickly create and write to the text file just like ‘cat’ command.
echo -e 'This will be the text in the file \n this is the new line' >> file.txtÂ
The above command can be highly customizable as it uses the properties of the echo command to make it quite versatile to write the text in the file, but using a new line character every time can be annoying as well.Â

using the echo command to create files.

Using echo command to create and write to a file.
Similar to the ‘echo’ command, we have the ‘printf’ command as well. The print command does the same thing as the echo command but in a C style rather than shell-style editing.
printf "" >> filename.txt
printf "" >> file1.txt >> file2.txt >> file3.txt
printf "This is some text here \n The second line \n The third line" >> file.txt

Using printf command to create files.

Using printf to create and write to files.
5. Using Command-Line Text Editors(Vim, nano)
This is the most time-consuming method and not the fastest, yet the method can be useful for Linux beginners. If you want to heavily edit a text file, you can use command-line text-editors such as Vim, nano, and there are other options as well. But most people use nano as it is simple to use and quick to go. Vim can also be used but most beginners find it difficult to use, so we’ll stick with nano for this example.
nano filename.txt
vim filename.txt
We are now in the nano editor(or vim). You can type in the stuff you require and simply type ‘CTRL+S’ to save and ‘CTRL+X’ to exit. In Vim it is a bit different. We won’t make a vim guide here, so you can check out the ‘Nano text editor in Linux‘ or ‘Getting started with vim‘ article from geeks for geeks.

Using Nano to create and write files.
Note: This method is more suited for creating and editing text files with complex content, especially when formatting and multiple lines are involved.
Conclusion
In this article, we discussed several quick and efficient methods for creating and manipulating text files from the Linux command line. These methods, including the touch command, redirect symbol, CAT command, echo/printf commands, and command-line text editors like Vim and nano, offer different approaches to meet various needs. Each method can be used differently depending on the situation and the case used. Not every method will be the fastest, yet these were some of the fastest ways to create a Text File Using the Command Line in Linux.
Similar Reads
How to Create a File in the Linux Using the Terminal?
In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read
How to View the Content of File in Linux | cat Command
The cat command in Linux is more than just a simple tool, it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's see the details of some frequently used cat commands, understanding each example alo
7 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file witho
8 min read
How to Create a VIM File in Linux CMD
In Linux, there are many ways to open and create files, but Vim stands out as a powerful and versatile text editor. It comes pre-installed on most Linux systems and allows you to create, edit, and manage files directly from the terminal. In this article, we will explain you, how to create a new file
3 min read
How to Save Output of Command in a File in Linux?
Linux has a robust command-line interface (CLI) that makes system interaction efficient for users. Saving a command's output to a file is among the most frequent activities in Linux. When working with big outputs, generating logs, or needing to save command results for later use, this is quite helpf
4 min read
How to Compress Files in Linux | Tar Command
File compression is a fundamental task in managing and transferring data efficiently on a Linux system. The Tar command, short for Tape Archive, is a powerful tool that allows users to create compressed and archived files. In this comprehensive guide, we will explore the various options and examples
11 min read
How to Append Text to End of File in Linux?
On Linux, while working with files in a terminal sometimes we need to append the same data of a command output or file content. Append means simply add the data to the file without erasing existing data. Today we are going to see how can we append the text in the file on the terminal. Using >>
2 min read
How to Create Directory in Linux | mkdir Command
In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help y
7 min read
How to Copy Files and Directories in Linux | cp Command
The cp (copy) command is your go-to tool in Linux for duplicating files and folders quickly. Whether youâre backing up data, organizing files, or sharing content, cp lets you copy items between locations while keeping the original intact. The cp command requires at least two filenames in its argumen
8 min read
How to Create a File in CMD
We all know that one can easily create files using GUI options, but what if you can use a faster way to create files through the command-line interface? This is not a necessary but useful skill for anyone working with Windows. Whether you're automating tasks, working on scripts, or preferring using
5 min read