How to Create New File on Ubuntu

In this tutorial, we will learn five different ways to create a new file on Ubuntu from both command line and GUI.

Creating a new file with nano

nano is a very minimalistic and user-friendly text editor that allows us to create new files and edit existent ones.

To install this tool on Ubuntu you can execute the following command:

$ sudo apt install nano

To create a file with this tool simply execute:

$ nano file_name

Creating a new file with touch command

touch is very simple to use, to create a file with this command, execute:

$ touch file_name

Creating a new file with cat command

cat is one of the most used commands to create and read files. To create a new file with this command execute:

$ cat > file_name

The above command will open a window for you to enter any text of your liking. You can save the text file with Ctrl + D.

Note that if you use the above command, it will delete the content of the file if it already exists.

If you want to add text to a pre-existing file without removing its content, you can execute the following command instead:

$ cat >> file_name

To view the content of a file with cat:

$ cat file_name

Creating a new file with echo

echo is another useful command and very easy to use. To create a new file execute:

$ echo > file_name

Note that the executed command will create an empty file.

If you want to create the file and add content at the same time, execute the next command:

$ echo "adding new content" > file_name

Creating a file with Vim editor

Along with nano, Vim is another popular command tool that you can use to create and edit files.

To install Vim editor on Ubuntu:

$ sudo apt install vim

To create a new file with vim execute:

$ vim file_name

Note: If you wish to enter some text to the created file. press i to open inserting mode, which will allow you to write your content in the file. To save the file, press Esc to enter command mode and then type :wq following by Enter to exit the editor.

Creating a new file via GUI

This command will allows us to add the ‘New Document’ feature to the Ubuntu right-click context menu:

$ touch ~/Templates/Empty\ Document

Opening 'New Document' in context menu

Now you can start creating new files on Ubuntu using the Graphical User Interface.

Creating a new file on Ubuntu via GUI

Reminder: When creating new files via command line, always use the ls command to verify that they were created correctly.

Using ls command to view created files

Leave a Comment

Your email address will not be published. Required fields are marked *