How to Create a File in Linux

July 11, 2024

Introduction

There are multiple ways to create new files in Linux, ranging from straightforward commands to more complex techniques for automating the process.

Learn how to create a file in Linux using common commands and popular text editors.

Different methods for creating files in Linux.

Prerequisites

  • Access to a command line/terminal window.
  • A user account with sudo privileges.

How to Create a File in Linux Using Command Line

Linux is designed to create any file you specify. You can save time and streamline your workflow by creating a file directly from the command line using one of the commands below.

1. touch Command

Access a terminal window and use the touch command to create a new text file called test.txt:

touch test.txt

Since no directory was specified, the touch command created the file in the current directory. Use the following template to create a new file in a specific directory:

touch /path_to_directory/file_name

Replace the placeholders with the actual directory path and file name on your system.

Use the ls command to list the contents of the directory and confirm that the file was created:

ls
Create file in Linux using touch command.

The touch command creates a file only if it does not already exist. If the directory contains a file with the same name, touch will not overwrite the existing file but will update the timestamp.

2. cat Command

The cat command is used to output the contents of a file, several files, or even part of a file on the terminal screen. If the file doesn’t exist, the command creates it.

Enter the following command to create an empty test2.txt file:

cat > test2.txt

The redirection operator > tells the system to place the input into the test2.txt file instead of displaying the contents on the screen.

Type the text you want to include in the file and press Ctrl+D to save the input and exit the cat command.

List the files in the current directory to verify that the file was created:

ls
Using the cat command to create a file in Linux.

The test2.txt file is listed among the files in the directory.

3. echo Command

The echo command outputs text to the terminal, but it can also be used with the redirection operator > to create files and write text into them.

Enter the following command to create a file called test3.txt and write Random sample text into it:

echo 'Random sample text' > test3.txt

Since the file test3.txt does not exist, the system creates it.

List files in the current directory using the ls command:

ls
Create file in Linux using echo command.

Use the cat command to display the contents of the new file:

cat test3.txt
Using cat to print file content in Linux terminal.

In this example, the system displays the text you entered with the echo command, Random sample text.

4. printf Command

The printf command allows users to output text in the terminal. It also offers advanced formatting capabilities, like adding new lines, tabs, or other formatting characters. You can use the printf to create a new file in Linux.

Enter the following command to create a new file, test4.txt, and add two lines of text:

printf 'First line of text\nSecond line of text\n' > test4.txt

The newline character \n tells the system to start a new line while the redirection operator > instructs it to write the output to test4.txt. If test4.txt does not exist, the system creates a new file.

Use the cat command to view the content of the file:

cat test4.txt
Using the printf command to create a file in Linux.

The formatted text from the test4.txt file is displayed in the terminal.

Note: Users who work with multiple terminal instances can use Linux Screen to manage them within a single window.

5. Redirect Operator

A redirection operator > is a character in the command-line interface that changes the destination where the results of a command are displayed.

It is used in conjunction with other commands to redirect content to specific files. However, you can use this symbol by itself to create a new file in the terminal.

Enter the following command to create the test5.txt file in the current directory:

> test5.txt

Use the ls command to verify that the file test5.txt has been created:

ls
Create new file in Linux using redirect operator.

To create a file in a different directory, specify the directory path and file name:

> /path_to_directory/file_name

Replace the placeholders with the directory path and file name on your system.

Using Text Editors to Create a Linux File

Linux text editors allow users to modify files in a graphical, user-friendly environment. Editors like Nano, Gedit, and Vim have advanced text manipulation features but can also be used to create new files.

Nano Text Editor

The Nano text editor is included by default in many Debian-based Linux distributions, such as Ubuntu and Mint. To open Nano and create a new file test6 in the current directory, enter the following command:

sudo nano test6.txt

Nano creates the test6.txt file if it does not exist and puts you directly into editing mode, allowing you to start typing immediately. At the bottom of the screen, Nano displays a helpful list of commands, making it easy for new users to navigate and use its features.

Creating files in Linux using Nano.

Once you are ready to return to the terminal, press Ctrl+X, then y to save the file and changes, followed by Enter to exit the editor.

Gedit Text Editor

Gedit is the default text editor for the GNOME desktop environment. If GNOME is installed on your Linux system, you can use Gedit to create and edit files.

To create a new file test7 using Gedit, open the terminal and enter the following command:

sudo gedit test7.txt

Type or paste the content to include in the file and press Ctrl+S to save the file.

Creating a new file in Linux using Gedit.

Click the X button to close Gedit and return to the terminal window. This will create the test7.txt file with the content you entered.

Vim Text Editor

Vim is a popular Linux text editor known for its powerful features and functionality.

Note: Vim has a steep learning curve due to its extensive capabilities. Use this Vim commands cheat sheet to get a jump start.

To create a new file test8 using Vim, enter the following command:

sudo vim test8.txt

The command opens the Vim editor and creates a new file named test8.txt in the current directory. Vim starts in the command mode, where you can enter commands to manipulate text and perform various operations.

Creating a file in Linux using Vim.

To start typing text, press i to enter the insert mode, and then type a few words or sentences. Press Esc when ready to return to the command mode.

Type :wq in the command mode, then press Enter to save the file and exit Vim.

Conclusion

This guide showed you how to create files using different Linux commands and common text editors.

To manage your files more efficiently, you should also learn how to copy files and directories in Linux.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Extract or Unzip tar.gz Files from Linux Command Line
February 12, 2024

This article shows which commands best to use when compressing and decompressing files from the command line.
Read more
How to Remove (Delete) a File or Directory in Linux
July 11, 2024

This article lists the most commonly used commands and tools to remove unwanted files and directories in Linux.
Read more
How to Use mkdir Command to Make or Create a Linux Directory
December 1, 2023

The mkdir command in Linux allows users to create or make new directories.
Read more
How To Use grep Command In Linux/UNIX
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems.
Read more