You may create a file inside the home directory of your Linux PC by executing terminal or shell commands like echo, cat, touch, and printf. Files may also be created using various text editors like nano, vim, and vi.
Alternatively, you can also create a file on a Linux-based operating system (OS) using the desktop File Manager. You may think of the Filesystem in Linux as the core map, within which the system stores files in the permanent memory.
Throughout this guide, you will learn how to create files on Linux from the desktop file manager. You will also learn the various commands you can use to clear a file in Linux and how to execute each of them.
Types of Files on Linux
Regardless of the Linux distribution, you are using, almost all the operations will be carried out in files. To allow for easy handling of these files, Linux offers directories (also referred to as folders). The filesystem is then managed in a tree-like structure. In the leading Linux distributions, such as Kali Linux, the file hierarchy includes three different types of files, including:
This is the common type of files on any Linux/UNIX system and includes such things as images, text files, and binary files. Regular files on Linux may be created by executing the touch command on the terminal. They feature ASCII (Human Readable text), program data, programs data, and executable program binaries.
This a type of file on Linux that stores a list of file names, and their associated information. They are the equivalent of folders in a Windows PC. Any Linux system will have multiple directories. For instance, the root directory(/) is the base of the filesystem.
/bin is the location for the essential user binaries, while the /home/ is the default storage location for your home directories. You will also come across the /boot directory, which stores the boot files on your Linux system. If you wish to create a new directory on Linux, you need to execute the mkdir command on the terminal.
These are the files that represent physical devices attached to your Linux computers, such as a printer, and other input/output computer accessories. Devices/special files on the Linux system are normally used for device Input/Output(I/O).
Such files are displayed as either directories or files on the Linux file system. In Unix, you are likely to have two special files for each device: the block, and character special files.
How to Create a File in Linux Using a Command Line
To allow for enhanced control of the system, Linux allows you to manipulate the various files on your system using a series of command lines. This option allows you to create a file in Linux, without having to open an application like File Manager first.
Instead, you only need to execute the right command or series of commands on the terminal window. For this operation to work, you need to have the relevant write permissions on the parent directory. As such, you first need to log into the Linux OS as the root user or add sudo before the commands.
You are likely to get a ‘permission denied’ error when you try to create a file on Linux without write permissions on the parent directory. Next, you need to open a terminal window on your PC, by pressing the Ctrl + Alt + F2 or Ctrl + Alt + T keys combination.
Thereafter, you may execute the following commands on the terminal to create a file:
Method 1: How to Create a File in Linux Using touch
The touch command on Linux is often used to update timestamps on the existing files, and directories. It may also be used to create a new, empty file on any Linux distribution. In this regard, you should just run the touch command followed by the name of the file you would like to create on the terminal, as follows:
If executed successfully, this command will create a new, empty file titled test.txt. If there happens to be another file with the same name in your system, the touch command will simply update its timestamp rather than create a duplicate file.
To view the file you just created, run the Is command as follows:
ls – l test1.txt
If you wish to create multiple files at once using the touch command, you only need to specify their names in the command line, separated by a space. This is the command you should execute, in this case:
touch file1.txt file2.txt file3.txt
This operation will create three files of the same type, named file1.txt, file2.txt, and file3.txt. This command may also be used to create multiple files of different types at once. In this case, the command line should be as follows:
touch test4.txt test.odt
When executed on a Linux terminal, the above command will create two different files titled test4.txt and test.odt.
Method 2: Create a File on Linux Using the echo Command
When executed on a Linux terminal, the echo command usually prints strings that are then passed as arguments to the standard output. Well, this operation may be redirected to a file as well. In order to create a file using the echo command, you need to specify the contents of the file in the command line as well.
To create a file on Linux, execute the echo command, followed by the content you would like to include in the file, and then use the redirection operator. This is the command structure:
echo ” File content” > test6.txt
Running this command will create a file titled test6, with ‘File content’ as its content. If you would rather create an empty file on Linux using the echo command, run the following command instead:
echo > file1.txt
Method 3: How to Create a File in Linux Using the Cat Command
Cat is one of the commonly used commands in Linux. Normally, the command may be used to create a file, display the contents of a file, display line numbers, and concentrate the content in multiple Linux files.
The following steps will help you create a new file in Linux by executing the cat command in the terminal:
Step 1: Create a New Directory
To begin with, you should create a new directory on your Linux distribution. To do this, you should just execute the mkdir command on the terminal, as follows:
mkdir New directory
Once created, you need to rename the new directory using any name you prefer. For the purpose of this guide, we will name the new directory, New_directory. To rename the directory, run the following command:
Step 2: Execute the cat Command to Create a File in this Directory
To create a new file, run the cat command as follows:
cat > test.txt
This command will create a text file and redirect you to the editor mode. At this point, you will need to enter your desired text into the editor, and then press the Ctrl + D keys simultaneously. This will save the content and close the editor, and take you back to the command line in the terminal.
To view the contents of the file you just created, run the following command:
Method 4: How to Create a File in Linux Using the Redirect (>) Symbol
In Linux distributions, the > symbol is referred to as the redirection operator. It offers an option to redirect the command output to a file rather than to a terminal screen. The redirect symbol may also be used to create a new file.
In this regard, you need to execute a command with the redirect symbol, followed by the file name in the command line. Using a single redirect symbol (>) in the command line overwrites the existing file. The >> operator will append the output.
If executed in a command line along with the file name, and file extension, the system will create a zero kb file within the local directory. If you would like to create a file using the redirect symbol, just run this command in the terminal:
While using the redirect symbol to create a new file, you should be very careful not to overwrite an existing file on your system.
Method 5: Create a File on Linux Using the printf Command
The printf command in Linux-based operating systems is normally used to display the given string, number, and several other format specifiers. To do this, you also need to specify the contents of the file you are creating in the command line. The command, in this case, should be as follows:
printf ” File content” > test7.txt
To view the contents of the file you just created, run the following command:
ls -l test7.txt
The printf command in Linux may also be used along with a format specifier, ordinary characters, or an escape sequence. Some of the format specifiers that are commonly used with this command include:
- The %s specifier – this is a string specifier used to give out a string output.
- %d specifier – an integer specifier that is used to show integral values
- %b specifier – this one is similar to a string specifier, but allows you to interpret an escape sequence using an argument.
- %f specifier – this specifier is used for the output of the floating-point values
- %x specifier – a specifier that is commonly used for the output of lowercase hexadecimal values. It may also be used for integers and padding the output.
How to Create a Large File in Linux
You will need to create relatively large data files on your Linux system, such as for testing applications. Such a data file will come in handy in such cases as when you want to test the write speed of your drive. In such a case, you will need to runs a series of commands in your terminal, as follows:
Method 6: Creating a Large File on Linux Using the dd Command
In most cases, the dd command on Linux is used to convert, or copy files. You may also execute it on the terminal to create a large data file. Suppose you would like to create a 1GB file and name it 1G.test, then you would have to run this command:
dd if=/dev/zero of=1G.test bs
Method 7: Create a Large File using the Fallocate Command
Fallocate is a command-line utility that is usually used to allocate large disk space to files in Linux. If you wish to create a 1GB file using this command, you need to run the following on your terminal:
fallocate -l 1G 1G.test
This will create a new file that is 1GB in size and is named 1G.test.
How to Create a File in Linux Using a Text Editor
The various Linux Text editors, such as Vi, and Vim may be used to create as well as edit files on the system. If you happen to indicate a file that does not exist in the system while using a text editor, the tool will first create the file.
The following methods will illustrate how you can create a file on Linux using the various text editors:
Method 8: Using the Nano Editor
Nano is one of the leading text editor apps for most Linux distributions. If this is your preferred text editor, open the terminal and run the following command:
This command will launch the nano text editor on your Linux machine. Now enter your desired text into the editor before pressing the Ctrl + X keys combination. You should then type Y to confirm these file changes.
Finally, press the Enter key to exit the editor. To view the file information for this file, run the following command in the terminal:
ls -l test9.txt
if you would rather view the contents of the file, you should run this command instead:
Method 9: How to Create a File in Linux Using Vim
The Vim editor may also be used to create files in Linux. If you do not have the Vim text editor installed on your computer, run the following command on your terminal:
sudo apt install vim
Once the Vim text editor has been installed successfully, you can then use it to create a file by executing this command line:
This will launch the Vim editor on your machine. Now press the I key to navigate to the insert mode of the Vim text editor. Enter the desired file content into the editor and then press the Esc key preceded by :wq to save and close the file.
To have a look at the file information, execute the following command:
ls -l test8.txt
To view the contents of the file you just created, run the following command on the terminal:
Method 10: Using the Vi Text Editor
If you only have the Vi text editor on your Linux machine, you need to launch it by executing the following command:
Once launched, press the I key on the keyboard to navigate to the insert mode of the Vi editor. You should then enter your desired content into the Enter key before pressing the Esc key. Now press :wq to save and exit the file you just created from the editor.
You can view the file information for the new file by running:
ls -l test10.txt
To see the content within this new file, you should execute the following command instead:
If you are not yet comfortable with running command lines in your terminal, you may use the GUI file Manager utility tool to create files on Linux. Alternatively, you may run the relevant command lines on the terminal to create one.
Different commands may be used to create a file on Linux, including cat, touch, echo, and printf. Unlike the touch command, the cat command allows you to specify the contents of the file you are creating in the command line.
Other commands like fallocate are essentially useful when creating a large data file on Linux. Based on the nature of the file you are creating, the above-described methods will help you create files and directories on any Linux distro.