In this article we will be looking at various methods on how to create a file in Linux using terminal. We’ll be demonstrating file creation using text editors and also with some in-built commands and tools. If you’re new to the world of Linux, you probably heard (or even witnessed with your own eyes) that experienced and seasoned users use command line interface (CLI) rather than graphic user interface (GUI), harnessing the power of Linux. They use it on a daily basis, even for the simplest of tasks such as creating a new file.
Now, the really interesting part is that there isn’t only one solution to accomplish that task, but there are numerous ways and it’s up to you to choose which ways suits you better. Also, apart from demonstrating file creation of various types, we also covered the methods on how to create multiple file in Linux with one command – link to >>>POST<<<, and how to delete files too – link to >>>POST<<<.
Create a file in Linux using text editors(VIM/NANO)
First that comes to mind are text editors, of course. You have a variety of them to choose between and most popular ones are Vi / Vim, Nano, Pico, Emacs and Joe.
Create a file in Linux using VIM
Let’s see first how to create a file in Linux using VIM.
You can start it with a simple command:
vim hello.txt (or however you want to name your file)
Once inside, press Insert so you can start typing. For this demo we’ll use the famous “Hello World!”.
When you’ re done typing, press Esc and then type:
:wq (yes, colon is a must)
Congratulations, you created your first file in Linux. On top of that, you learned also how to quit Vim.
Type the “ls” command to see is the file saved.
Create a file in Linux using NANO
Similarly, if you decide to use Nano, you would type:
To exit use CTRL+X after which you’ll be asked to save the changes to the file.
More or less it’s the same with the rest of file editors so it’s needless to show them all.
Create a file in Linux using cat command
Apart from file editors, we have various commands to create a file and one of them is cat (not to be mistaken for feline, it’s just shorter for concatenation). To use a cat command we type:
cat > hello3.txt
“>” symbol is used so the system will know that we plan to populate the file. You’ll notice that cursor switches to beginning of line and you can start typing:
After you’re done use combination CTRL+D to save and exit. To see the content of file, we’ll type:
You can even append the text using:
cat >> hello3.txt
Another way of creating file is just using “>” symbol, like we did with cat command, only this time without cat command:
Saving the file is also in same way – CTRL+D.
You can even use “>> hello4.txt” to append a file.
But to see contents, you’ll have to add a cat command:
Create a file in Linux using the touch command
Another command for file creation is “touch”.
As we can see, with this command file is only created. To populate it, you’ll have to use another methods.
Create a file in Linus using echo and printf
You can even use echo command to create a file:
echo “Well hello there again.” > hello6.txt
And appending is also used with “>>” symbol.
echo “World will never be the same again\!” >> hello6.txt
If you plan to use exclamation mark (!) in sentence, you’ll have to use it with backslash () to escape a special character, because echo treats it like an operator.
To check the contents of using echo command:
There is one more command which can be used to achieve the same result. It’s “printf” command. Its use is similar to “echo”:
printf ‘Hello world yet another time!’ > hello7.txt
And appending is also similar like with the echo:
printf ‘And goodbye!\nSee you some other time!’ >> hello7.txt
And the final result looks like:
And that completes our list of commands to demonstrate various methods on how to create a file in Linux. In all examples we created files with extension .txt, but it’s worth mentioning that files can be created even without extension or any other extension for that matter. I hope the post was informational and of use to you.
Thank you for your time…