Create a file in Linux

Learning how you can create a file in Linux is an essential skill to have, especially if you’re using Linux regularly.

There are various ways and commands you can use to create a file in Linux.

If you’re worried about the learning curve, don’t be. To create a file in Linux is a straightforward process, and anyone can learn how to do so quickly.

In this guide, we’ll share with you how you can easily create a file in Linux using the command line.

Getting Started

Before we dive into the commands to use, let’s cover the prerequisites needed to create a new file in Linux. There are two prerequisites:

  • Sudo privileges
  • Access to the command line or terminal window

You can access the command line by opening your terminal window (Ctrl + Alt + T or Ctrl + Alt + F2). As for sudo privileges, this is useful when you create certain files or directories.

How to Create a File in Linux With the Touch Command

Creating a file in Linux using the touch command is the simplest way to create a new file.

Apart from creating a new file, the touch command also allows you to update the timestamps found on existing files or directories.

To create a new file in Linux, simply type the command touch followed by the new file you want to create:

touch text sample.txt 

If the file ” text sample.txt” doesn’t exist, the touch command will create a new file called “text sample.txt.” On the other hand, if “text sample.txt” already exists, this will prompt the touch command to update the existing file’s timestamp.

If you want to create multiple files at once, simply type:

touch file.txt file 1.txt file 2.txt file 3.txt 

To see the file you just created, we use the ls command to list all the contents found in the parent or current directory. To list a file using the ls command, simply type:

ls 

When creating a file using the touch command in your Linux system and no directory was specified, this will automatically create a file in your current directory.

How to Create a File Using the Cat Command

The cat command is primarily used to display the contents of files, a single file, or a part of the file. You can also use the cat command to create a new file.

This works if the file you want to create doesn’t exist, and the cat command will automatically create a new one.

For example, to create a file with the name text file 1.txt, we run the cat command with the (>) redirection operator symbol in the command line:

cat > text file 1.txt 

After you’ve successfully created “text file 1.txt”, make sure to save the file in your Linux system by pressing Ctrl + D.

Saving your files is a step you should never forget.

To verify you have “text file 1.txt” in your Linux system, you can use the ls command to check. Similarly, you can use the cat command to open “text file 1.txt” by typing in the command line:

cat text file 1.txt

How to Create a File With the Echo Command

Unlike the cat command, using echo will duplicate what you specify in the echo command, and place a copy into the file.

The echo command is useful for duplicating a file or text and placing it into a specific file.

To run the echo command in Linux using the command line, this will also require the (>) symbol and the name of the file.

For example, if you want to place the text “Random files” onto a new file called “sample files 1.txt”, then the echo command will be as follows:

echo 'Random files' > sample files 1.txt 

We then verify if the file was successfully created by typing:

ls 

Once you see the file “sample files 1.txt”, we can check its contents in Linux using the cat command:

cat sample files 1.txt

Output: 
Random files

Using the printf Command

The printf command also works like the echo command. If you create a file with printf, you can add formatting to it.

For example, if you want to create a file with two separate texts, you can separate the two texts by adding two lines of text.

To create a file with the printf command, we use the \n option:

printf 'first line of text\n second line of text' sample.txt

The\n option signifies that the first text will be on the first line and the second text on the second line.

How to Create a File Using Redirection Operator

The basic command of creating a file through the redirection operator is as follows:

> text sample.txt

The redirection operator carries the (>) symbol to signify output redirection. The (>) symbol is useful if you want to redirect the output of a command to a certain file.

For example, if we use the ls -al command, this will give us all the files and directories found in the home directory.

If we want to redirect the contents displayed with the ls -al command, we use the (>) followed by the file name you want the contents to be redirected to.

Let’s say that the contents found in ls -al are “Hello World,” and we want to redirect the contents to the file text sample.txt, so we type:

ls -al > text sample.txt 

When we check to see the contents in the text sample.txt file using the cat command, we find this:

cat text sample.txt

Output: 
Hello World 

Take note though, that using (>) symbol will overwrite any contents found in the file, so if the file “text sample.txt” contained contents, using the command (ls -al > text sample.txt) would delete the contents.

This means you should be careful when you create a new file using the redirection operator. If you don’t want to overwrite the file and simply add to the contents found in the existing file, we use the (>>) symbol.

Using the same example above, if text sample.txt contains the contents “Hello World” and we simply want to add the contents “Hello world again” and not overwrite the file, we type:

echo Hello world again >> text sample.txt 
cat text sample.txt

Output: 

Hello World
Hello world again 

How to Create Files Using Text Editor

Creating files in Linux isn’t just limited to the command line. After all, Linux distributions will have at least one text editor.

The most popular are Vim, Vi, and Nano. In this section, we’ll show you how to create files in Linux using a text editor.

Vim Text Editor

Vim is a modified version of Vi, which was the oldest text editor in Linux.

To create a file with Vim text editor in Linux, type:

vim sample.txt

Press “i” to insert any text and then save the file by typing Esc :wq then hit “Enter.”

Vi Text Editor

Vi, being the oldest text editor in Linux, is safe to use, considering how long it’s been around and was designed specifically for editing text files.

To create a file with Vi, type:

vi sample.txt

Don’t forget to press “i” to enter insert mode to type the command and create the file.

After you create the file, be sure to save, and exit by pressing “Esc” then type “:x” and hit “Enter.”

Nano Text Editor

Nano is much more modern and easier to use and navigate through.

Instead of pressing “i” to enter editing mode, Nano automatically puts you in editing mode by default.

To create a file, simply type:

nano sample.txt 

After you’ve created the file, press Ctrl + 0 to save the file and then Ctrl + X to exit or log out of the editor.

Final Thoughts

We hope this guide was able to help you create new files on Linux through various commands and ways! For any more concerns and questions, feel free to let us know in the comments section below!