How to create a new file in Ubuntu

Ubuntu provides a number of command line tools to create new files. In this Answer, we'll try the following ones to create files.

  • The touch command

  • The output redirection operator (>)

  • The cat command

  • The echo command

The touch command

touch is perhaps the most commonly used command-line tool used to create files in Unix-like systems. It is pretty straightforward and can be used to create one or more empty files in a single line:

touch filename1 filename2
Command to create files using touch

We can create files with any extension using touch. In the following example, we demonstrate creating some text and Python files using this command:

Creating a single text file
Creating a single text file
1 of 2

The output redirection operator (>)

> is called the output redirection operator and is used to redirect the output of a process to a file. The same operator can also be used to create empty files using the following syntax:

> filename
Command to create a file using the output redirection operator

Here is an example:

Creating a new file using the > operator
Creating a new file using the > operator

The cat command

The cat command, in addition to displaying the contents of a file, can also be used to create new files when combined with the output redirection operator (>):

cat > filename
Command to create a file using cat

The added benefit of the cat command is that it also allows adding content to the newly created file. After executing the command, enter the desired text and press "CTRL+C" to save the content and exit:

Creating a new file using the cat command
Creating a new file using the cat command

In the above image, we first create a new file named hello.txt, add a few lines of text, and press "Ctrl+C" to save and exit. We then verify that the file was indeed created using the ls command, and verify the contents of the file using cat hello.txt.

The echo command

echo is another command line tool that can be used to create files, with or without content. To create a file with no content, use the following syntax:

echo > filename
Command to create an empty file using echo

To create a file with a single line of text, use the following syntax:

echo text_to_add > filename
Command to create a file with 1 line of text using echo

To create a file with multiple lines of text, use the -e switch with the echo command. The -e switch allows backslash escape interpretation, and we can then use \n to create new lines:

echo -e "First line\nSecond line\nThird line" > filename
Command to create a multi lines file using echo

Below is a demonstration of creating files using the echo command:

Creating an empty file using echo
Creating an empty file using echo
1 of 3

Try it yourself

The terminal given below runs Ubuntu operating system. Click the terminal to connect and try creating various files using all of the listed commands:

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved