Learning to create a file in Linux using many ways is a recommendable step toward speeding up the use of the operating system. For example, you may want to
- Only create an empty file,
- Create a file and open it, or
- Create and write into the new file using one command.
Whatever way you want to create a file in Linux, this tutorial will help you out. It explains how to create a file in Linux using seven commands in 11 ways. They are the
Table of Contents
- Create an empty file
- Create an empty file and open it
- Create a file and write into it
- Create a File Using a GUI
Create an empty file
Assume you want to create a file on the terminal and not open or write into it. You can use the touch, echo, and printf commands or the greater-than operator.
Use the touch command
touch is the most familiar command to create a file in the Linux terminal. It takes the file name as an input.
Assume you want to create an empty file called file.txt. First, let’s ensure the file does not exist using the ls command.
Next, append the file name to the touch command.
Lastly, rerun the ls command to confirm the file creation.
You can also check the file’s contents using the cat command.
The system returns nothing, meaning the new file is empty.
If the system throws an error, permission denied, introduce
sudo before the target command.
sudo touch file.txt
Use the echo command
The echo command is used in various shell scripts and programming languages to reveal the contents of a file. Besides, you can use it in Linux to create an empty file. All you do is combine it with the greater-than operator. Using the n-switch enables the new file to support new lines.
echo -n > file2.txt
Here, we tell the system to create an empty file called file2.txt. The file should support new lines because we have specified the n-switch using minus n, -n.
Use the greater-than operator
Apart from combining the greater-than operator with other commands, you can use it independently to create an empty file. All you do is append the target file’s name to the command. You can then start writing the contents of the file and exit using Ctrl+C.
You can then confirm the file’s creation using the
ls command, and if it is empty with the cat command.
ls cat file3.txt
Sure. We have three files, including the file3.txt file. And the new file is empty as we hoped.
Let’s see how to create an empty file using the
Use the printf command
Like the echo command, the printf command helps show the result of computations on the console in programming languages like C. In Linux, we can use it to create an empty file by supplying it with an empty string, greater-than operator, and file name.
printf '' > file4.txt
The two single quotes,
'', denote an empty string. A string is a collection of characters. A character is a letter. For instance, you can store some letters, w, o, r, d, in between single or double quotation marks.
string_name = 'word'
When the inside of the quotations holds zero characters, we refer to the container as an empty string. So, in the
printf command above, we created a new file called file4.txt and stored nothing,
Create an empty file and open it
Use a terminal editor
Apart from only creating a file, you can create the file and open it using Linux text editors like Nano, Vi, and Vim. You primarily use the editors to open and modify a file. You do that by specifying the file to open.
The editor then checks if the given file exists. If it does not exist, the editor creates it. With that knowledge in mind, we can explicitly create empty files before opening them.
For example, assume we want to create a Python file, app.py, and open it using the Vim editor. You can do that by opening the terminal and typing the file name after the editor’s name.
A screen pops up.
You can exit the Vim interface by typing
:q! or pressing the esc key followed by
:q for quitting without saving
:wq for saving and quitting a written file.
ls cat app.py
Now that you know how to create an empty file and even open it, let’s move further and write into the new file.
Create a file and write into it
Another way to use a file in Linux is to create and write into it. Besides the echo and
printf commands discussed in section 1 of this tutorial, you can apply the
mv commands as explained in this section.
Use the echo command
You can create a file and write some words by attaching the file contents, the greater-than operator, and the file name to the
For instance, let’s create file5.txt.
echo 'I am creating file5.txt and appending this string to it' > file5.txt
And confirm the file’s creation.
Yes, it was created! Does it have some content? Let’s check using the cat command.
Correct, the file has the content we created it with. Here are alternative ways to create and write into a file.
Use the printf command
This time around, we will put some characters between the quotation marks.
printf 'A string goes here' > file6.txt
Then, check the result.
ls cat file6.txt
Use the cat command
cat is one of the overworked commands in Linux. Apart from creating files, it checks file contents, concatenates the file with another, or append more contents into the new file.
Since we have seen how to check file contents using the cat command, we will now use it to create and write into files.
Create a file
Create the target file using the cat command with the greater-than operator and the file name.
cat > file7.txt
The cursor enters a new line. The new file welcomes you to write. Put some content in the new file, then press the ctrl and d keys simultaneously to save the content and exit the file.
Concatenating files means combining a file with another or others. Assume you have two files:
f2. You want to join
f2‘s contents with
f1‘s into a new file,
f3. You can do that using the cat command as shown below.
f2 then add some contents into them.
echo 'content1' > f1 echo 'content2' > f2
content2 as follows.
cat f1 f2 > f3
f3‘s existence and its contents.
ls cat f3
You should get an output like the one shown on this screenshot:
Note: A single greater-than operator rewrites file contents, whereas the double greater-than operator appends content.
Use the mv command
The mv, short for a move, command relocates file contents or renames the file. If the destination file does not exist, the command creates it.
For example, let’s create a new file called file8.txt by moving f3’s content into it.
mv f3 file8.txt
Then confirm the file’s creation.
ls cat file8.txt
We now have file8.txt with contents: content1 content2. Note: the creation of file8.txt resulted in the deletion of f3.
Create a File Using a GUI
What if you want to create a file using a GUI? That is where the GNOME editor comes in.
Use the gedit command
The GNOME editor, gedit, found in the desktop environment, helps to create and edit a text file graphically.
Use the fallocate command
Sometimes, you handle massive files, and you want to specify each file’s size during its creation. You can do that using the
fallocate command with (A) the
-l flag, (B) file size, and (C) the file name.
fallocate -l 1G lastFile.c
This tutorial taught you how to create files in Linux using seven commands in eleven ways. You can comfortably create an empty file, make a file and open or put some contents into it using the terminal or a GUI.
It would be best to familiarize yourself with the commands, as shown in this tutorial.