How to create new files in Linux

We'll cover creating files both in the Terminal and on the Linux desktop.

Create file on desktop

If you are uncomfortable using Terminal, creating new files in a desktop environment is straightforward, using a few basic everyday applications.

File browser

Most file browsers like Thunar and Dolphin will allow you to create an empty file by right clicking on the desired folder and pressing Create empty file or a similar option from the drop-down menu.

How to create new files in Linux Picture 1

Alternatively, in the apps menu, you can often click File> Create New for options to create new files.

Text editor

Your Linux distribution will certainly include a basic text editor application. You'll start with a blank file when you open it and pressing Ctrl + S will give you a dialog to save the file at a specific location.

How to create new files in Linux Picture 2

Create file in Terminal

Many Terminal commands in Linux allow you to create files quickly and efficiently. We will discuss some of these commands below.

touch

One of the most basic Linux commands, touch, will create a new file or if the filename you specified already exists, update the file's last modified date.

In the directory where you want to save your file, type:

touch filename.txt

Alternatively, create multiple files with one command by simply placing a space between each filename:

touch filename1.txt filename2.txt filename3.txt

You can check if the file you created exists or not with this command:

ls

Since you cannot edit files with the touch, this command is better suited for quickly creating multiple files for later editing.

Redirect operator (>)

Right braces are used in many commands to redirect output to a specific file. You will see it used with other commands later in this article.

However, you can import it without a specific command to create an empty file.

> filename.txt

Note, however, that the redirect operator itself will overwrite any existing files that used that name.

echo

The echo command will output in Terminal any input you give it. However, it is also possible to create a new file and (optionally) save a line of text inside it.

To create a new blank file, use the following command:

echo -n > filename.txt

To create a new file with a single line of text use:

echo "File text" > filename.txt

Be sure to put quotes around your text!

cat

The cat command (short for concatenate) is most commonly used to combine or read files. However, it can also easily create new files with text in it.

cat > filenname.txt

The redirect operator is here to redirect the cat's output to the specified file, the output is whatever you enter next. When you've finished writing the contents of the new file, press Ctrl + D to save it.

printf

The printf command is similar to the echo command, but has a bit more formatting capabilities.

For example, you can create a file with two lines of text using the following single command:

printf 'Some text Some more text' > filename.txt

fallocate

Fallocate allows you to create a file in Linux of a specific size. It is mainly useful for test purposes, such as assessing hard drive write speed.

Use fallocate with the following command:

fallocate -l 10MB filename

Replace "filename" with whatever name you want to call your file.

The "-l" option indicates that you want a specific capacity and the "10MB" argument indicates what the size is. You can also use larger byte capacities, like GB and TB. You can also use M instead of MB to specify mebibyte instead of mega byte.

vim

Vim is a Terminal-based text editor that will launch when you specify a filename:

vim filename.txt

While vim is running, press the I key to start typing. When you're done, press Esc and type : wq , then press Enter to save and exit.

nano

GNU nano is another text editor similar to Vim, but is probably a bit more user-friendly.

You can quickly create and start editing files with the following command:

nano filename.txt

Type whatever you want into the file, then press Ctrl + S to save and Ctrl + X to exit.

5 ★ | 1 Vote

May be interested

  • Manage folders in Unix / LinuxManage folders in Unix / Linux
    a directory is a file whose only task is to store the name and related file information. all files, which can be regular, special or directory files, are kept in folders.
  • Is it possible to run .exe files on Linux?Is it possible to run .exe files on Linux?
    you don't need to trade the appeal of windows software for the stability, security, ease of customization (and even great classic appearance) of linux. in this article, you will be shown how to run windows exe (possibly executable) files and software using the linux operating system. these methods can be applied to any linux distribution, including ubuntu, kali linux, centos and many others.
  • How to create USB Boot Live Kali LinuxHow to create USB Boot Live Kali Linux
    the most popular and fastest way to apply kali linux is to run it from a usb drive. this method has many advantages. to do this, we first need to create a bootable usb drive from an iso image of kali linux.
  • The Cat command in LinuxThe Cat command in Linux
    the cat command (short for 'concatenate') is one of the most frequently used commands in operating systems like linux / unix. the cat command allows users to create one or more files, view file contents, join files, and redirect output in a terminal or file.
  • How to Create an ISO File in LinuxHow to Create an ISO File in Linux
    this wikihow teaches you how to turn a group of files into an iso file on a linux computer. you'll use the linux command line to do this. gather your iso files in the home directory. place any files that you want to turn into an iso file...
  • How to Create and Use ISO Files on LinuxHow to Create and Use ISO Files on Linux
    learn how to create, mount, or burn iso images using linux. most linux distributions come with software to create, mount, or burn iso images. using these steps, you will learn to do this, and maybe even understand how it works. create the...
  • How to create and delete folders in Linux TerminalHow to create and delete folders in Linux Terminal
    folders are essential for your linux operating system because they contain files you may use often or that the system needs to function correctly.
  • How to Find Files in LinuxHow to Find Files in Linux
    if you don't know how, finding files in linux can be quite difficult. here, the best way is to use a few different terminal commands. they are much more powerful than the simple search features on other operating systems, and when you know how to use them well, you will have complete control over your files.
  • How to copy and rename files in LinuxHow to copy and rename files in Linux
    there are more ways to copy and rename files on linux than just cp and mv. try some commands and other ways suggested below.
  • How to create an ISO file on LinuxHow to create an ISO file on Linux
    you may know that an iso file can be burned to a cd / dvd or usb drive, but do you know that you backup or store your files and folders into an iso file? will be better? with an iso file, you can burn it to a cd / dvd as a backup or simply mount it as an external drive and access files from within.