How to Create a File in Linux Using Terminal/Command Line


Creating a new file on Linux is straightforward, but there are also some amazing and clever techniques


In this tutorial you will learn how to create a file from a Linux terminal.


  • Accessing a command-line/terminal window (Ctrl-Alt-F2 or Ctrl-Alt-T)A user account with sudo privileges (optional for some files/directories)

Creating new Linux files from the Linux

command line It is designed to create any file that you specify, even if it does not already exist. A smart feature is that you can create a file directly, without needing to open an app first.

Here are some commands to create a file directly from the command line.


a file with the touch command

The easiest way to create a new file on Linux is by using the touch command


In a terminal window, type the following:

touch test.txt

This creates a new empty file named test.txt. You can view it by entering:


The ls command lists the contents of the current directory. Because no other directory was specified, the touch command created the file in the current directory.

If there is already a file with the chosen name, the touch command will update the timestamp.

Create a new file with

the redirect operator

A redirect operator is a name for a character that changes the destination where the results are displayed


Straight angle bracket >

This symbol instructs the system to generate the results as specified below. The destination is usually a file name. You can use this symbol by yourself to create

a new file: > test2.txt

This creates a new empty file. Use the ls command to enumerate the contents of the current directory and locate the test2.txt file.

Create file with

cat command

The cat command is short for concatenate. It can be used to generate the contents of multiple files, a file or even part of a file. If the file does not exist, the Linux cat command will create it.

To create an empty file with cat, type the following: cat

> test3.txt

Note the redirection operator. Typically, the command displays the contents of test2.txt on the screen. The redirect operator tells > the system to place it in the test2.txt file.

Verify that the file has been created: ls The

system should now have test.txt, test2.txt, and test3.txt

in the list.

Create File with

echo command The echo command will duplicate what you specify in the

command and place the copy in a file.

Enter the following

: echo ‘Random sample text’ > test4.txt

Verify that the file has been created:


You should see the test4.txt file added to the list. Use the cat command to display the contents of the new file: cat

test4.txt The

system should display random sample text (or whatever you entered

with the echo command).

Create file with

printf command

The printf command works like the echo command and adds some formatting functionality. To

add a single line of text, type: printf ‘First line of textn’ test5.txt

To add two lines of text, separate each line with the option

n: printf ‘First line of textn Second line of text’ test6.txt

You can use the cat command on any of these files to display their contents.

Using text editors to create a file

All Linux distributions have at least one text editor. Some have multiple editors. Each editor has different strengths and characteristics. This will show you three of the most popular.

Vi Text Editor

Vi is the oldest text editor on Linux. It was created together with the Linux operating system to directly edit text files. Since you’re unlikely to see a Linux distribution without it, it’s a safe editor to know.

To create a file with Vi, type the following:

vi test7.txt

The screen will change. You are now in the text editor. Press the letter i to switch to insert mode, then type a few words to test it.

To save and exit, press Esc:x and press Enter.

Vim Text Editor

You may have noticed that the Vi editor was not very user-friendly. Vim is a newer version, which stands for Vi editor, Modified.

Use vim to

create a new text file: vim

test8.txt This screen

will look similar to the Vi editor screen. Press i to insert text and type a few words. Save the file and exit by entering

: Esc :wq Enter

(Escape, colon wq, then Enter.)


Text Editor Nano

is a newer

and much easier to navigate text editor. Create a new

file by entering the command:

nano test9.txt

By default, Nano puts you directly into edit mode. It also displays a useful list of commands at the bottom of the screen.

Enter text, then press Ctrl+O to save your changes.

Press Ctrl+X to exit the editor.


You now have several options to create new files on Linux from the command line. Next, learn how to copy files and directories on Linux to manage your files more efficiently.

Contact US