Let's check out this post and learn how to create a file Linux computer. Here we have listed five easy ways to help you organize files only on Linux. But before we begin, make sure you have access to the command line or a terminal window and a user account with Sudo privileges.
How to Create a File in Linux?
Using the Linux touch command to create files
To create files in Linux with the touch command, you can first specify the touch command and then use the name of the file in the command and type it on your terminal window like this:
$ touch filename
Along with the command and filename, you will have to include the file extension that you want to generate. For instance, if you are prepared to produce a JPEG file, you will have to use the .JPEG extension after the file name.
With the touch command, you can create multiple files simultaneously as well. You can modify the timestamp of the file you just created using the following command:
The stat file_name command will help you verify the timestamp of the file. You can access the file, change it, and update the changes. If you want to change the access time or the last time when the file was accessed, use the following command:
touch -a filea
If you want to change the modify time only or the time when the file was modified, use the following command:
touch -m filea
This is only the touch command and its usage. But keep an eye for more commands to come.
Command to create a file in Linux: Cat Command
Cat command is another popular Linux command tool that helps you generate many files simultaneously. But you cannot edit a file using the cat command still you can do other operations such as this:
To create and write texts into your file, you can use:
This command will generate a new file (Linux make file) in the write mode if it can't find the file in its current directory. If you find any file with the same name in the current directory, you can be sure that the file is duplicated.
It would be best to press ctrl+d to save the file after writing something inside it. After writing the file, you can view what you have written with the cat file1 command. The command will show the content inside the file on your terminal window. You can also add the content of two files into a new file or an old file using this command:
cat file1 file2 > file3
This command will create a new file 3 with the content of file 1 and file 2. if there is another file named file 3 present in the computer, then that file will be duplicated with the file 1 and file 2 content. But if you want to add the content of file 2 at the end of the file, you can use the
cat >> file2
This command will create a new file 2 if that file doesn't already exist in the current directory. If any file named file 2 is present, it will be opened.
Otherwise, you may copy the elements of the file with
cat file1 > file2
With this command, you can create a new file2 with the data of file 1 in case file 2 is not exist. But if there is a file with file name 2 existing, it will duplicate the file with the content of file 1. But you can reverse the content of the files to their previous position using tac file1.
Creating Files on Linux with vi Command
With the vi command, you can especially edit files, and it is widely used by developers who want to edit the texts of a file. You can also create a file using vi file_1 and exist the file by pressing the escape key or the :wp command followed by hitting Enter. To create a file using vi commands, type:
This command will create a new file1 and open the file on your vi text editor only if file 1 is not available in the current work directory. If the file with the file name is present in the present directory, the command will open the file on the text editor itself.
Edit Files with Nano and Gedit Command
Nano command might be present on your computer, and if it is, you can edit files with the nano editor. Type nano file_1 and the file will be opened. You can exit the text editor using ctrl+x.
Linux users mostly use CLI to write and edit text files. You can edit text files on your Ubuntu computer without using Nano or vim but using the gedit text editor. Gedit refers to the GNOME text editor that you can find on GNOME desktop systems, including CentOS, Debian, Red Hat, Fedora, Ubuntu, etc. You can create, edit, and write files with gedit. Create files on gedit with,
The preceding command shall form a different file 2 that you can write on the gedit text editor. But it will only create the file if the file is not available in the current directory. If there is another file with the name file 2 in the same directory, it will be opened on the gedit text editor. If you generate or edit a file on gedit, the file will be opened and closed if you don't edit the file or write additional comments. You can use the terminal while the gedit editor is running, and launching the text editor, you can run the following command:
gedit file_2 &
The command will launch geditor in the background. And you can use the command line prompt and geditor at the same time.
Linux MV command
There is another command that you can use to move files within your Linux system, but with the mv command, you can create new content as well. You can create new files with content from another file on your system.
Use the mv file_2 file_3 command. The command will create a new file 3 by mixing the content of file 2 if file 3 is non-existent. If there is a file 3 in the current directory, that file is a duplicate of file 2. The above command will copy the content from file 2 to file 3 and remove the content from the previous file.
In this tutorial, we went through the steps on how to create files in Linux systems. We covered 5 different commands that you can use to create files and folders in Linux systems like Ubuntu, Debian and CentOS.