In this tutorial, I will show you three ways you can use to create a file on Linux via CLI.

Create a file with touch

The first way is via touch command. Even though touch's original purpose is to update a file's timestamp to the current date and time without modifying it, we can use it to create an empty file. In fact, that's what most people use for file creation.

Example:

touch file.txt

The above touch command will create an empty file (0 bytes).

You can create multiple new empty files with touch.

touch file1.txt file2.txt file3.txt file4.txt

With touch command you can't enter any text in the file.

Create a file with redirection sign (>)

The second way is via redirection sign >. Using the redirection operator with no command preceding it will truncate an existing file or create a new, empty file.

> myfile.txt

If we use the redirection symbol alone without prefixing it with any command, then it will create an empty file (0 bytes) and remove the file's content if a file already exists with the given name.

Create a file with echo or printf

The third way is via echo or printf commands:

echo "" > myfile
# or
printf "" > myfile

Create a file with vi editor

The vi editor is the most popular command-line editor and is the default editor in most Linux distributions. It's used to create and edit files on the command line. I will just mention it here, but you need to know your way around this editor. Here is how to create a file with vi.

vi <filename>

This will just open file in vi editor. To actually create a file you need to save it in from the editor with : + w

That's it. If you know any other way, let me know in the comments.