Linux based operating systems are known for their users’ heavy use of command line for performing not only complicated automation but also the most trivial of tasks. However, with the steady growth of Linux distributions in the home desktop market, the onus is on the developers to make the graphical interface as lay user friendly as possible.
Today, we will see various ways to perform a simple and trivial task; creating a new file, in Linux using the command line as well as the GUI.
Create File using Touch Command
The touch command in Linux is used to create an empty file and the syntax for creating an empty file is as easy as:
$ touch sample_file
To create multiple files at once, pass multiple file names as arguments:
$ touch sample_file1 sample_file2 sample_file3
The touch command will throw an error if any of the files already exist.
You can also create the file in another directory by specifying the full path to the directory location.
$ touch ~/Downloads/sample_file1 sample_file5 ~/Documents/sample_file6
Create File Using Vim and Nano Editors
Vim is a very popular text editor in Linux distributions. Although the command line utility nano is the one available by default, users generally prefer Vim.
Install Vim in Debian and its derived distributions by running:
$ sudo apt install vim
In RedHat and its derived distributions, install Vim with Yum:
$ sudo yum install vim
Creating a new file is quite easy using text editors. You can create an empty file by creating a new file for writing and save it without writing anything to the file.
The following syntaxes can be used to create a new file using Vim and Nano respectively:
$ vim new_filename $ nano new_filename
Once the editors open with ‘new_filename‘, users can now write to the file if needed. If writing to file is done, or if the file is to be left empty, do the following to save the file:
- In Vim, press the ‘Escape‘ key, type ‘:wq’, and hit Enter to save the file and exit.
- In Nano, simply press Ctrl + O and hit Enter to save the file. Press Ctrl + X to exit.
Create File Using Redirection Operator
A redirection operator is an operator used in the Linux command line to write output to a file or to read input from a file.
We use the former of these two operators
('>') to create a new file with output from a command. For example, to write the output of command ls to a new file, run:
$ ls > sample_newfile $ cat sample_newfile
To create an empty file using this, simple echo command and empty string and redirect it.
$ echo '' > sample_newfile2
Create File Using File Manager
Lastly, we will see how to create a new file from the GUI. Open Nautilus either by running the command ‘nautilus’, from the left-hand side dock or from the Applications menu, depending on your Linux distribution.
Go to the folder you want to create the new file in. For example, let’s create a new file in the folder ‘Documents’.
Right-click on the empty space in the folder display and click on ‘New Document’ -> ‘Empty Document’.
Note: In newer versions of Ubuntu, the option ‘New Document’ might be missing. To fix this simply run the following:
$ touch ~/Templates/Empty\ Document
Right-click on the new file, click ‘Rename’, and enter a new name for the file.
We learned about multiple ways to create a new file in Linux. There are obviously, even more ways to create a new file, and every individual application usually has the ability to create a new file of its respective format. Eg. Image editors export files to image formats like JPEG and PNG, audio editors to MP3, M4A, etc.
Let us know in the comments below how you go about creating a new file in your Linux system!