In this tutorial, we’re going to show you how to create a file in Linux. The easiest way of doing this is through the CLI, but you can also do it via the GUI. We’ll include step-by-step instructions for both methods.
TL;DR: you can create a new empty file by using the touch command:
Or skip to the GUI instructions if you don’t want to use the Terminal.
What you’ll need
- Access to the Terminal (you can open it with CTRL + ALT + T)
- Write permissions for the directory and user you’re going to work with
All the command examples below are just the most basic examples that create a file, all commands have more options.
How to create an empty file with the touch command
The touch command has a lot of options, you can check our detailed touch command tutorial if you need to learn more.
The most basic syntax to create an empty file is:
touch [options] <filename>
How to create an empty file with the > operator
You can just use the > operator followed by the file name to create a new empty file. Example:
How to create a file with the cat command
You can also use the cat command to create a file, among other things. A basic example is:
cat > filename.txt
After you run this command, you can start typing the file’s content in a single line, and when you are done press CTRL + D to save the file.
If you want to create a file with multiple lines, you’ll need to use:
cat << END > filename.txt
After entering that command, you can enter as many lines of text/content as you want, you can even use special characters. Write something, hit Enter to go to a new line, and keep doing that as long as you want to. To save and close the file, write “END” in a new line and hit Enter.
You can replace “END” with whatever word you want to in the command.
How to create a file with the echo command
To create a file with the echo command, use this example:
echo 'content of the file' > example.txt
This command will add whatever is in the ‘ quotes to the file you specify after the > operator.
How to create a file with the printf command
Similarly to echo, you can use the printf command to create a file with some content:
printf 'one line of text\n another line of text' file.txt
Note that this command doesn’t use the > operator.
How to create a file with a CLI text editor (nano)
You can create a file by using a CLI text editor.
To create a file with nano, just run the following command:
Add whatever you want to the file and save and close the file by pressing CTRL + X, then write Y and press Enter.
Nano is preinstalled on most Linux distros.
How to create a file with a large size
Sometimes you may need to create a file with a larger size (gigabytes).
To create a 4GB file, you can use the fallocate command:
fallocate -l 4G thisfileis4gb.txt
You can replace 4G with whatever size you need.
You can also use the dd command to create a large file:
dd if=/dev/zero of=thisfileis2gb.txt bs=1 count=0 seek=2G
The command above will create a 2GB file.
Bonus: How to display all the files in the current directory
If you want to get a list of all the files in the current directory, just use the ls command:
Bonus: How to view the contents of a file
You can view a file in the CLI using the cat command. Just enter “cat” followed by the file name:
and you will get a result with whatever the file contains.
How to create a file with a GUI text editor
If you’re not a fan of the CLI, you can use your GUI (Graphical User Interface) to create a new file. All Linux distros come with a text editor pre-installed.
On Ubuntu, to open the text editor, you need to press the “Windows” key or open the “Show Applications” menu (1) and start typing “text” (2). The Text Editor app icon will appear. Click on that app (3) to open a new Text Editor window.
Start entering the content for the new file (1). Once you are done, click on Save (2) and choose the location where you want to save the file.
How to create a new file by right-clicking anywhere and selecting New Document
You can create a new file by right-clicking anywhere and selecting New Document. Depending on the distro (file manager) you’re using, such an option might not be available for you.
For example, with newer versions of Ubuntu, there’s no “New Document” option when you right-click somewhere:
To enable that option, you need to create a file/document in the Templates folder (Home/Templates). You can create that file with your favorite GUI text editor (from the previous section), or you can use any of the commands in this post.
To do it via the CLI, you need to run this command in the Terminal:
touch ~/Templates/Empty\ Document
Once you run that command or create the file via the GUI text editor app, the new Document option will be available when you right-click somewhere:
The great thing about the Templates folder is that it works for any type of document, including .odt documents (from LibreOffice Writer). You just need to create a Writer document and save it in the Templates folder. Once you do, a new option will appear:
And that’s about it. Now you know how to create files on Linux. With multiple options, including CLI and GUI options. If you need any help or have any tips, leave a comment below.