Linux Gate #3 : The thing at the heart of Linux

 

 

Hello there! This article is the fourth article of our Linux series we call it the Linux Gates. 

In this series of articles, we want to provide you with exactly what you need to start your way through Linux commands.

In the previous article we've seen  two commands: ls and pwd and in the present article, we are going to see how you can create files, write into files, and destroy files.

But why creating files is that important in Linux or for your work in general?

The simple answer is, because that files are the heart machine of any program.

to convince you with that, remember some software that impresses you one time in the past or some video game that you found awesome and carefully designed and enjoyed it so much.

Well, what you probably didn't conceive at the time, is that if you break down this software or this game you'll find that it's mainly composed by files. 

I mean LOTS of files that contains text, instruction and basically how every detail of the game must work!!!

The same is true for Linux, files are the heart of this incredible tool, where you write how you imagine things to work and want them to perform.

At a first glance, this sounds like not a big deal, but truly it is a big deal in programming!

Now that we cleared this up, let's see how to create files in Linux. 

As always, their is a lots of ways you can do things in Linux, which a very good thing to have.

However, we recommend for beginners not to get lost on the variety and to know at least one way to do things as a first step.

Now, the most straightforward way to create a file named "newfile", is to use the command "touch" in the following way:

The "newfile" file is now created. This file is empty. 

Now we can use touch command to create multiple files all at once. for example, to create the following files: file1 file2 file3 .. file5 we can use the command touch like in the example below:

The previous way sounds okey, but very laborious and a brute force manner to create 100 files in a row for instance. Luckily there is another way to it as in the example below:

Now using this command, we can create as many files as we want, those files are numbered from 1 to 5 in this example but you can go to as many number as you want! 

Our aim as Outofbox-GATE is to help adapt commands for everyone to gain work efficiency in their daily job:  

The example above shows a quick way to create files elegantly and simply that is so much quicker than using your mouse. I mean "right click + create new file" one hundred times!!

Now let's see how to fill the file created. As noted above, the files created are empty files and we want to write "hello world !" in the new file created named: "newfile".

We can simply do it by calling one of the most iconic symbol used in Linux that is the redirection > and the echo command.

First, the echo command. This command is basic command that repeat it's arguments. Meaning that: echo "hi" will say hi, echo "how are you" will say "how are you" as in the examples below :

if you have a variable that is named "pi" and that stores the number 3.14. The command echo can allow you to display the content of this variable pi : 

This is a command that so basic and at the same time very vital when working in Linux. We certainly going to circle back to it in the next articles.

On the other hand, the symbol ">" redirection does a job of a filler. It files what echo says into a file and put it in there. See the example:

The file "newfile" now contains the "hello world !" phrase. That's how you quickly fill a file with a phrase if you in a rush. 

If you want to write more than just one phrase, you will need a file editor to open the file. We recommend gedit for beginners and Vim for the more advanced.

You can launch the text editor by calling the gedit command.

The "newfile" will now be opened in a new separate windows. 

When you finish writing all of your contents, you can simply click Save or Ctrl+S key on your keyboard. And your file's content will be saved.

Now, if you want to remove a given file or multiple files, you can simply use the command rm which stands for remove. Simple as that : 

and the file will be removed entirely. This command can be used for multiple files as in the following :

and the files : file1, file2, file3, file4, file5 will all be removed.

rm command can also be used for removing directories, but that's a subject matter for the next article in the Linux Gates.

Stay tuned for that !

Meanwhile, don't hesitate to follow our Blog in order to get notified for future Posts and Podcasts.  

Follow our LinkedIn page where we propose useful tips and trick about Linux : 

OutOfBox-GATE Linux LinkedIn Page

And until the next Post, SHEERS !

Comments

Read Also

Linux Gate #2 : Where to begin ?!

Linux Gate #0 : Is Linux terminal that scary ?

Linux Gate #5: How to increase your work efficiency by the use of programs?