Hey are you new to the Linux Operating system? Don’t have any idea how to create a file in Linux, How to Open a File or Edit a File in Linux?
If so then, you will learn all these things in this tutorial.
Creating a File on Linux is the most simple thing, each and every Linux user should know How to Create a file in Linux.
What will you learn in this tutorial?
- Creating File in Linux using cat command
- Creating File in Linux using the touch command
- Create File in Linux using Graphical User Interface
- Edit existing files
before you learn how to create a file in Linux, you should learn How to Create a Directory in Linux.
I don’t know either you already know or don’t but I am amusing that you are a new Linux user.
In order to create New Linux directory, you have to use mrdir command with the directory name you want to create.
$ mkdir <directory_name>
Suppose you want to create a Directory “Project-B” then you will have to use the following command-
$ mkdir Project-B
We have a separate tutorial about creating and copying directories – How to copy directory in Linux
Create file on Linux
Creating files on Linux is as simple a task as creating files in windows or mac machines. There are mainly two ways to create a File on Linux as well as Mac and Windows also.
- Command Line Interface
- Graphical user Interface
Sometimes if you are using a headless Linux server then you will not get the graphical user interface’s file manager so you can not create a file using your mouse pointer.
How to Create File on Linux using Terminal?
It is very important to learn how to create Files on Linux using a terminal also known as a command-line interface.
There are mainly two commands to create file on Linux that are-
Before you create a File-
Before creating a file on Linux make sure there is no any other file at the same location with the same file name.
If you try to create a file whose file name already exists on the same location then you will not get any error but also you will not be able to create a new file.
How to check location in the terminal?
If you want to check the location in your terminal then you will have to run pwd command.
When you run pwd command in the terminal then you will be shown the current directory.
If you want to check what files and directories are already available on the location then you have to ls command.
ls command -It shows the list of files and directories on the current location.
Here, you can see I run the ls command on my Linux machine and I got the list of files and directories as my output.
Creating a File on Linux using touch command
The touch command is used on Linux to create an empty file. Using the touch command we can only create a file with no content. In order to add content in the file with have to edit the file in any Linux file editor.
You should touch the command only if you do not have any data to store in the file while the creation of the file.
$ touch <FileName>
Suppose you want to create a file called MyEmptyFile using the touch command then you will have to run the following command-
$ touch MyEmptyFile
Creating Multiple File Using touch Command
Sometimes you may need to create more than a file at the same time. Using touch command you create more than one file at same time.
$ touch file1 file2 file3 .... fileN
I hope, Now you are able to create files on Linux using the touch command. Now, we will learn how to create files using the cat command on Linux. After that, I will explain how to edit files on Linux.
Before we proceed ahead, I would like to tell you that we have also a tutorial about how to remove directories and files in Linux.
Creating files on Linux using cat Command
The cat command is the one of most frequently used commands on Linux. It has been used for multiple operations. You can use the cat command for creating new files, viewing the content of a file, and concatenate files.
Isn’t it a very useful Linux command?
Well, lets see how to create files using the command-
$ cat >fileName
Suppose, you want to create a new file into your machine named “Hello” then you have run the following command-
$ cat >hello
When you run this command then you will be asked to add some contents inside the file.
Add something you want to add inside the file and the press control key + D to save the content and exit from the file.
Note: The cat command is used with the redirection operator to create a new file.
Once you have created a file using the cat command on your Linux machine then run-
$ cat fileName
To view the content inside the file.
Create a File on Linux Without any Command but Using Redirection operator
Yes, there is one more way to create files on Linux. And creating files using this way you don’t need any Linux command.
In order to create a file without using any command, you will have to use redirection operator (>).
It is super simple to create files on Linux using a redirection operator. Just use the redirection operator followed by the file name.
$ > <filename.extension>
Let’s take an example, Suppose you want to create a file ‘helloworld.txt’ then you will have to use the following command-
$ > helloworld.txt
However, you can create a text file using a redirection operator followed by the file without adding a file extension.
$ > filename
if you run the above command without any file extension then It will automatically create a file with a txt extension.
Easy way to edit a file on Linux
By now, you have learned how to create a file on Linux operating system. Now, you need to learn how to edit files.
In order to edit files, you need to have a installed file editor on your Linux Machine.
How can you Install a File Editor on Linux?
In order to install a file editor on your Linux, first of all, you have to decide which editor you want to install.
The most popular and the most commonly used file editors for Linux are-
You can install any of these tools.
I am installing Vim on my Ubuntu Linux Machine.
In order to install vim on your computer, you have to run sudo apt install vim if you are using Ubuntu or Debian but if you are using Centos or Red Hat then you will have to run sudo yum install vim-enhanced -y.
Once you run the command, you will be asked to enter the password. Enter the password to continue the installation process.
Now you will receive the following message-
After this operation, 34.6 MB of additional disk space will be used. Do you want to continue? [Y/n]
Press Y to continue.
Within a few seconds, Vim will be installed on your machine. And would be ready to use.
How to edit a File using Vim Editor?
To edit an existing file on Linux using Vim you will have to run the following command-
$ vim filename
Let’s take an example, suppose there is a file on your machine named “helloworld” which is a text file.
To edit this file you will have to run the following command-
$ vim helloworld
In order to use Vim Editor, you need to know Vim Command. There is separate tutorial about Vim Command on our website you can learn it also.
In this tutorial, you will learn how to create files on your Linux machine using the command-line interface. Also, I have covered how to edit files using Vim utility.