How to Create a File in Linux Using The Command Line [6 Easy Methods]

Create a File in Linux Using Terminal Command Line

Making a new file in Linux is easy, and you can use one of the several applications to create a file in a specific format. 

However, the fastest way to create a file in Linux is still through the terminal or command line. It is a fundamental skill for all users, especially server admins, who need to quickly create text files, scripts, or configuration files for their work. 

This guide will take you through the process of creating files using command line tools like touch and echo and text editors like nano, Vi, and Vim. Whether you’re a beginner or an experienced user, mastering this skill is crucial for optimizing Linux operations. 

Let’s start with the prerequisites for the process. 

Table Of Contents

  1. Prerequisites
  2. How to Create a File in Linux Using the Command Line
    1. Method 1: Use the touch Command to Create a Text File
    2. Method 2: Use the Redirect Operator to Create a New Empty File
    3. Method 3: Use the cat Command to Create a New File
    4. Method 4: Use the echo Command to Create a New File
    5. Method 5: Use the printf Command to Create a New File
    6. Method 6: Use Text Editors to Create a File in Linux
  3. Conclusion
  4. FAQs

Prerequisites

Before you learn how to create a file in Linux using the command line, you’ll need:

  • A command line environment or terminal window.
  • An account with sudo privileges. Note that it’s not always a strict requirement for creating some files and directories.

How to Create a File in Linux Using the Command Line

At its heart, Linux is all about using the command line to speed up essential tasks such as creating files. As such, Linux offers several methods of carrying out this essential task. 

Let’s discuss several popular ways of creating files in Linux from the command line. 

Method #1: Use the touch Command to Create a Text File

The most straightforward approach to generating a new blank text file in Linux is to use the touch command.

Launch the terminal and enter the following command to create a file without any contents in the current directory (since we aren’t mentioning a specific location in the following command):

# touch testfile.txt

Next, use the ls command to verify that the touch command has created the testfile.txt:

# ls

touch test file

Method #2: Use the Redirect Operator to Create a New Empty File

A redirection operator is a special character that alters where the standard output is shown in the command mode. Usually, the redirection operator is represented by a right-facing angle bracket (>).

You can use this operator to specify a file where the system should redirect the output. As you can guess, you can use this method to create a new blank file with the name textfile1.txt in the current working directory.

# > testfile1.txt

Next, use the ls command to list the items in the current directory and see that the file has been successfully created.

# ls

testfile 1

Method #3: Use the cat Command to Create a New File

In Linux, the cat (concatenate) command is typically used to view the contents of one or more files or segments of a file. For our purposes, if you specify a non-existent file, the cat command will create it.

Launch the terminal and enter the following command: 

# cat > testfile3.txt

Note that we used the redirection operator to redirect the output of the cat command to the textfile3.txt. This alters the command’s usual behavior of displaying the file in the standard output. The redirection operator instructs the system to redirect the output to the file. Since this file doesn’t exist, the command first creates it. 

You can verify the file creation with the ls command:

testfile 3

Method #4: Use the echo Command to Create a New File

The echo command duplicates the provided text and saves a copy in a file. Let’s see this command in action. The following command will create a file named textfile4.txt and save the provided text in it. 

# echo ‘We are RedSwitchestest’ > testfile4.txt

Again, use the ls command to verify that the new file has been created. 

# ls 

If you wish, you can use the cat command to print the contents of the textfile4 in the terminal. 

# cat testfile4.txt

testfile 4

Method #5: Use the printf Command to Create a New File

The printf command works like the echo command and brings additional formatting capabilities. 

Run the following command in the terminal to create a file named textfile5.txt and add a line to it:

# printf 'Hello World, We are RedSwitches\n’ testfile5.txt

You can use the \n separator to add multiple lines to the file. To demonstrate this, use the following command:

# printf 'Hello World, We are RedSwitches\n Bare Metal Server Providers’ testfile6.txt

Use the cat command to view the contents of these files:

# cat testfile5.txt

# cat testfile6.txt

Method #6: Use Text Editors to Create a File in Linux

Every Linux distribution comes with at least one built-in command-line text editor. In addition, you can install several command-line text editors to take advantage of unique strengths and capabilities. 

We will introduce you to three of the most widely used terminal-based text editors and demonstrate how you can create a file in Linux.

Vi Text Editor (vi)

The Vi text editor is the classic non-GUI text editor on Linux. Almost every mainstream Linux distribution has Vi as one of the core packages. That’s why every Linux user, especially system admins and developers, should be familiar with the Vi text editor.

Let’s create a file in the terminal with the following command that uses vi as the text editor:

# vi testfile7.txt

The display will transform, indicating you are now in the text editor. Hit the i key to enter the insert mode, and then enter some text.

To save your changes and exit, press Esc and then 😡. Press Enter to confirm the command. 

testfile 7

Vim Text Editor (vim)

To be fair, Vi is not very intuitive, and many users tend to avoid it because of its complicated command sequences. 

Vim (short for Vi Improved) text editor is a more modern terminal-based text editor that adds user-friendliness to Vi’s capabilities.

Enter the following command to create a new text file using the Vim text editor:

# vim tesfilet8.txt

The Vim’s interface resembles Vi’s, and they share functionality and commands. 

Press i to initiate the insert mode and then type your text. To save the file and exit, enter the following command sequence:

Esc :wq! Enter

wq!

Nano Text Editor

Nano is a more contemporary text editor with a simpler and more user-friendly experience.

Follow these steps to create a new text file with nano.

# nano test9.txt

By default, Nano launches into the edit mode and conveniently presents a list of useful commands at the screen’s bottom for easy reference.

test 9

Type your text and press Ctrl+O to save the file. Next, press Ctrl+X to leave the editor.

Conclusion

Creating a file in Linux using the terminal is a fundamental skill that uses commands and command-line text editors. Whether you choose the time-tested Vi, the enhanced Vim, or the user-friendly Nano, you can start with a blank slate and create a file for your use.

You can also take advantage of the commands such as cat and echo to spin up files. In most cases, admins use these commands in scripts that need files for their operation. 

Want to experience high-resource dedicated hosting? Unlock blazing-fast performance and unparalleled reliability for your online ventures. Get started with RedSwitches and elevate your hosting experience today.

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is the terminal?

In Linux operating systems, the terminal—often called the command line or command prompt—is a textual interface that allows you to execute commands directly to communicate with the system.

Q. What’s the process for generating a new file in Linux through the command line?

Initiating a new file via the command line in Linux can be done by executing the touch command along with the desired file name. For example, to create a file named example.txt, use the command touch example.txt.

Q. What is a text file?

A text file is a type of file that contains plain text and can be opened and edited with a text editor. It contains no formatting or special coding, just the raw text.

Q. What are the steps to make a text file in Linux?

To create a text file in Linux, you can use various methods such as the touch or cat commands, or a text editor like nano.

Q. How can I create a text file using the cat command?

You can create a text file with the cat command by typing cat > filename.txt, entering your text, and then pressing Ctrl+D to save and exit.

Q. How do files differ from directories in Linux?

Files in Linux are individual data entities with a name for storage purposes. In contrast, directories serve as organizational units that hold files and further subdirectories, akin to folders containing documents in a filing system.

Q. Are there other ways to create a new file in Linux?

Yes, besides the touch command and the cat command, you can also create a file using a text editor like nano or by redirecting the output of a command to a file using the > symbol.

Q. How do I create a new text file using the nano command?

To create a new text file using nano, you can simply type nano filename.txt in the terminal, where filename.txt is the name you want to give to the file. This will open the nano text editor, and you can start typing the file’s content.

Q. How can I create a file with specific content using the command line?

There are several ways to create a file with specific content using the command line. You can use commands like echo, printf, or cat combined with input redirection to specify the content.

Q. What methods are available for creating a file with predetermined content via the command line?

To populate a file with specific content through the command line, you can employ tools such as echo or printf commands, or use the cat command alongside redirection operators to direct the desired content into a new file.

Q. Can I create a file in Linux for reading only?

Yes, you can create a file in Linux for reading only. You can create the file with the desired content and then change its permissions to make it read-only using the chmod command.