When working on the terminal, or command line, there is often a need to know how to create a file in Linux. Whether you need to create a script file, a config file or text file, it is a common activity for a Linux admin.
This article will look at some of the different ways you can create a file in Linux using command line tools and text editors.
Let’s start by looking at some of the native bash tools we can use to create files.
How to Create a File or Multiple Files using the Touch Command
One of the simplest and quickest ways to create a file on Linux is to use the touch
command. Touch is present on all Linux distributions, and allows us to update the timestamp on an existing file (without otherwise modifying it), and can create new, empty files.
You can create a new file using touch
with the following syntax:
$ touch file1.txt
You can also use touch to create multiple files at the same time, simply by listing more filenames after the touch
command:
$ touch file1.txt file2.txt file3.txt
Or, a more efficient way of doing this would be to specify a range for the number used in the filename. This will allow us to create multiple files. For example if we wanted to create five files at the same time we could use:
$ touch file{1..5}.txt
This will result in the following:
-rw-rw-r-- 1 cloud_user cloud_user 0 Jan 7 10:02 file1.txt
-rw-rw-r-- 1 cloud_user cloud_user 0 Jan 7 10:02 file2.txt
-rw-rw-r-- 1 cloud_user cloud_user 0 Jan 7 10:02 file3.txt
-rw-rw-r-- 1 cloud_user cloud_user 0 Jan 7 10:02 file4.txt
-rw-rw-r-- 1 cloud_user cloud_user 0 Jan 7 10:02 file5.txt
We can even take this a step further. For example:
$ touch file{{a..z},{A..Z},{0..99}}.txt
This should have given some ideas of how you can use touch
to quickly create a file or multiple files. Notice though, these files are all empty. You can see in the directory list above that the filesize is 0 bytes
. Later on in this article we will look at some ways to create files with content.
TIP!
Touch
is a quick way to ensure the file system you are working on is writable. If you are troubleshooting and think your filesystem may be in a read only state, or if you think the storage isn’t mounted correctly then runtouch
on an existing file on that filesystem. If an error is returned then you know the filesystem likely isn’t writable.
Using the Redirect Operator
Redirection in the Linux terminal allows you to send the output from one command to a file (or another command). There are a couple of ways to perform redirection. Using >
will redirect output and overwrite or create a file, whilst >>
will append the output to an existing file. We can use the first one to create an empty file easily by running it by itself (e.g. redirecting no output to a file). For example:
$ > file.txt
This is the quickest (or at least the method with the least amount of typing!) way to create a file on Linux. However, use caution! As mentioned earlier, the redirect operator will overwrite an existing file if it already exists, so by running this against an existing file, it would effectively replace it with an empty file.
Lets move on to looking at creating some files with content.
Create a file using the Echo Command
Now that we have spoken about how redirection works, we can look at how we can use it with the echo
command to create a new file with content. Like touch, the echo command is available on all Linux distributions and is used to echo text to the terminal. For example:
# echo "hello world"
hello world
As you can see, our hello world
has been echoed back to the screen. Now, we can use the redirect to create a new text file, with hello world
written to it:
# echo "hello world" > file1.txt
Now if we view the file1.txt file using cat
, we can see that the words have been written to the file:
# cat file1.txt
hello world
By redirecting the output of the echo
command into a new file, we have created a new file and written to it with one command.
How to Create a File using the cat Command
In the example above I used the cat
command to view the file1.txt file I had just created. The cat
command is often used for viewing files, but it can also be used to create files, and is used in a similar way to the echo
command.
# cat > file1.txt
This will create a new file and open it for writing. You will be able to type into the console, then once done press CTRL-D
to close the file. It will contain whatever you typed in.
You can also use cat
along with the EOF
statement to send a chunk of text to the new file. For example:
# cat > /tmp/sysctl.conf << EOF
net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
EOF
This is particularly useful when used with a script to create a config file and then populate it.
Note – the use of EOF in the example above is known as a Here document, or Heredoc
Create a New File on Linux using PowerShell
PowerShell isn’t mentioned that much with Linux as yet, but you can install PowerShell on Linux and it certainly has a lot of uses. And, you can use it to create a new file! And the syntax will work just as well to create a new file on Windows systems:
PS /root> new-item -Name file5.txt
OK! So hopefully that has shown you a few ways you can create text files quickly and easily from the Linux command line. Let’s have a quick look at text editors, which allow you to create files and edit them easily.
Creating a New File using vi
vi
has been around for a long time, as long as Linux itself, but it is still going strong and is the go to editor for many. T create a new text file using vi
and open it for editing you can run:
# vi file1.txt
Type i
to go into the insert text mode, and then type a few words, such as Hello World
!
Once done, press ESC
then type :wq
to write the file and close vi
. That’s how you create a new file using vi
.
Note: If you prefer to use
vim
instead ofvi
, the commands listed above will work in the same way.
How to Create a New File using Nano
Nano
is another popular text editing program available on most Linux Distributions. Nano is a simple text editor, that doesn’t have the learning curve that vi or vim may have, so is a great option if you are new to Linux. To create and open a file for editing using nano
you can run:
# nano file2.txt
Once it has opened, you can start writing your content immediately:
Once you have finished editing, hit CTRL-X
to exit nano
. You will be prompted to save your changes before nano
exits.
Conclusion
In this tutorial you have learnt how to create files in Linux using a number of different methods.
We have covered how to create a file or multiple empty files using the touch
command. Then went on to showing how you can create new text files with content using redirection and the echo
and cat
commands.
Finally we had a quick look at how you can create new files using the popular text editors, vi
and nano
. Hopefully this will have covered your needs for creating new files on Linux.