3 Ways to create files in Linux real quick
Creating files in any operating systems is as obvious as breathing air to live. Here I show you 3 very common and most easy ways to create and write contents in that files. Let's just jump into it.
echo
echo
commands is a built-in Linux feature that prints out arguments as the standard output. echo
is commonly used to display text strings or command results as messages. But we can make files with appended contents also with it.
Creating files with echo
We can create files and add contents with echo
in two different ways.
Single redirect and double redirect.
Single redirect or (>)
Single redirect is used for short notes or description, and it can't be appended. It usually beneficial when you want to note some crucial text as fast as possible.
echo "Linux is awesome and fun to learn." > linuxislove.txt ⏎
File named linuxislove.txt has been created with a cool sentence. Now you can display the contents of your file by using cat
command. Here how:
cat linuxislove.txt ⏎
Output:
Linux is awesome to learn
One crucial thing to remember is we can't append content in our files made from echo
commands using single redirect.
As you try to write like this:
echo "But also very crucial these days.">linuxislove.txt ⏎
When you write like this in hope that it will append as next sentence, but it will not. echo
just overwrite the content inside and add new content "But also very crucial these days." omitting everything previously added.
cat linuxislove.txt ⏎
Output:
But also very crucial these days.
You can see that our previously added text is overwritten by this words.
But you can append sentences by using double redirects. Follow along.
Double redirects or (>>)
Double redirects come into play when you want to append texts in your files. Using echo
you can do that. Here how:
echo "Linux is awesome and fun to learn.">linuxislove.txt ⏎
You can display the result by using cat
command.
cat linuxislove.txt ⏎
Output:
Linux is awesome and fun to learn.
Now the best part appending with echo
.
echo "But also very crucial these days." >> linuxislove.txt ⏎
Checking output
cat linuxislove.txt ⏎
Linux is awesome and fun to learn.
But also very crucial these days.
cat
cat
or concatenate files are used to print on the standard output, generally used to display file contents as you can see in our echo section where I used cat a lot to display/read the contents of linuxislove.txt file. But we can also use cat to create files with content inside by using redirect (>) or (>>) to write the content in it. cat
is very similar to echo
in terms of creating files, only difference is in their core functionality. echo
is print as it is the message and cat
is used to print the contents of any file inside. Both use single redirect and double redirect to insert texts inside files.
echo
vs cat
So you don't get confused here is simple example to show you their core difference in their core functionality.
echo linuxislove.txt ⏎
Output:
linuxislove.txt
cat linuxislove.txt ⏎
Output:
Linux is awesome and fun to learn. But also very crucial these days.
Now you see the difference. It's huge difference isn't it.
echo
is used to display what is written as it is, as name suggest (echo whatever written next). While cat
is used to read the contents of file if any.
Creating files with cat
As I mentioned before when its time to create files and add contents with echo
and cat
, the differences is in their interfaces.
Single redirect or (>)
To create a file, we follow the cat command with a redirect, denoted with symbol (>)
, and name for the file we want to create. Here's an example.
cat > myFavOSisUbuntu ⏎
The Linux Kernel today has well over 30 million lines of code as of 2022.
The most popular programming language used for the Linux Kernel is C, with nearly 98% of code written in C
When you press ENTER , Linux will go into interactive mode and wait for you to start entering content for the file. The prompt disappears, but just start typing freely with multi-line. note* you can't go to previous line by typing arrow-up key or in any ways.
When you finish typing till your heart content. Simply press Ctrl-D
to exit the prompt.
Now you see your content by using cat
command. Omit >
sign from command and Linux will spit back the contents of your file.
cat myFavOSisUbuntu ⏎
Output:
The Linux Kernel today has well over 30 million lines of code as of 2022.
The most popular programming language used for the Linux Kernel is C, with nearly 98% of code written in C
Now we try to append something with single redirect, as you might know that its not possible to append further content using only single redirect symbol (>)
. But for sake of curiosity we will do it.
cat > myFavOSisUbuntu ⏎
Appended: hope 0%
Overwritten: certainty 100%
Press Ctrl-D
to finish writing. Do cat will overwrite our file with new content or append it? Now let's see our file
cat myFavOSisUbuntu ⏎
Output:
Appended: hope 0%
Overwritten: certainty 100%
As expected cat with single redirect (>)
overwrite our file new content in it.
So what to do to append our file, now you know and guessed right, we will use double redirect (>>)
to append/add contents.
Double redirects or (>>)
cat >> myFavOSisUbuntu ⏎
Linux is everywhere.
Press Ctrl-D
, now let see our file using cat
command:
cat myFavOSisUbuntu ⏎
Output:
Appended: hope 0%
Overwritten: certainty 100%
Linux is everywhere.
Last but not least.
touch
We can use all beloved command to create files in *nix world is by using touch
command. When we see man page for touch
, it says "change file timestamps".
man touch
Output:
TOUCH(1) User Commands TOUCH(1)
NAME
touch - change file timestamps
SYNOPSIS
touch [OPTION]... FILE...
DESCRIPTION
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h is supplied.
A FILE argument string of - is handled specially and causes touch to change the times of the file associated
with standard output.
Mandatory arguments to long options are mandatory for short options too.
But simply we can create files with touch
, when we don't need to fill with contents beforehand. Just create new file.
touch fileName
Now you can edit and add content with any text editor you want. I usually use Neovim for adding contents.
Use cat
or echo
when you want to note important notes immediately, usually text with short descriptions. And use touch
to create files when you want to edit later with longer descriptions.
✧Bonus✧
You can also use text editors of your choice in terminal to create and add contents in your file at the same time(not entirely at the same time, but you can understand isn't).
For example: I use nvim
command which is nothing but Neovim a powerful text editor for power users.
You can download Neovim from Here: https://neovim.io/
nvim myFile
You will enter Neovim's editor arena where you can add/edit contents in it and save and quit with :wq
command.
View changes with cat
command as usual.
cat myFile
So, yeah that's it for now, Linux is a wonderful ocean where you can get everything you want.
Happy learning and Happy sharing.
Subscribe to my newsletter
Read articles from Mohammad Farhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mohammad Farhan
Mohammad Farhan
I am a developer from India, fall in love with computer science in 2018. I do programming stuffs on daily basis. Love to share my knowledge about computer science and technology in general. Wannabe software engineer.