How to Create a File in Linux?
When you are using Linux operating system or any flavour of Linux like Ubuntu, CentOS, etc and you want to create a new file, you can do so using the touch
command, or the cat
command or using editors like vim and nano, etc which may or may no be available in your Linux installation, but you can try.
In this tutorial, we will cover all these ways starting from the most basic ones and then moving on to some specific scenarios for creating files in Linux.
Using the touch
Command:
If you just want to create a file with no content in it, then you can use the touch
command to create a file in Linux.
If you want to create a file with name studytonight.txt, then run the following command:
touch studytonight.txt
Executing this command will give no output, and will just create a new file with name studytonight.txt in the current directory. You can verify that, by running the ls
command to list down the files present in the directory.
ls -l
-rw-rw-r-- 1 nobody nobody 0 Jul 2 16:11 studytonight.txt
We can also create multiple files using the touch
command:
touch file1.txt file2.txt file3.txt
If you then want to add any content/text to this file, you can use the Vim or Nano editor to do so.
Using the cat
Command
Now let's see the second way of creating a file, this time with some content in it. If you want to create a file in Linux, with some content in it, then using the cat
command is a good option.
cat > studytonight.txt
Then type in the content that you want to add to the file, like "Hello World!" or anything else, and then press CTRL + D to save the file and exit.
Again, you can use the ls
command to see whether the file got created or not. And to see the content of the file you can again use the cat
command but without the angular bracket.
cat studytonight.txt
Hello World!
Using Code editor like Vim or Nano
We can also use the available code editor in your Linux OS to create and edit files. To check whether you have these editors installed, type in nano
or vim
in your terminal.
Using Nano Editor to create a File:
To use nano editor to create a new file in Linux, use the following command:
# nano /path/to/file/name-of-file.extension
nano test.txt
The above command will open the Nano editor to create a new file, now you can type the text you want to add to your file. Once you are done, press CTRL + X, this is the command to exit, the editor will ask you to save the file, then press Y key to confirm and your new file will be saved.
Using Vim Editor to create a File:
To use the vim editor to create a new file in Linux, use the following command:
vim test.txt
# or vi text.txt also works
The above command will create an empty file, to insert text into it, first press I, then enter the text, once you are done, press ESCAPE to exit the insert mode, then enter :wq! and hit ENTER. Yes, this is a bit difficult to use.
Conclusion:
In this article we covered different ways of creating a file in Linux. You can create an empty file or file with some text, even edit an existing file using the editors we just discussed, Nano and Vim.