Instructor: Okay, so we know how to find our way
to the desired directory in terminal,
but what if we need to create a file
or a sub folder in another directory?
How can we do that?
Well first let's open up our terminal.
And by the way, another way that you can open terminal
instead of right clicking and going open terminal here,
is going up here to this icon which says,
terminal emulator and clicking on it.
By default, this will be inside of your home directory,
which we know to be this sign.
And if we type the PWD command,
we get slash home slash Mr. Hacker directory.
We know that our desktop directory
is also inside of this directory, so let's go there.
CD desktop, press enter,
and now we are in the desktop directory.
Let's get back to the files.
To create a simple empty file,
we can use a command called touch.
If I simply just type touch file
and let's call it file one, press enter.
This will create the file named file one
and it'll have no contents inside of it.
We can also see that file one has been created
as it is right now on our desktop,
since we created it inside of the slash desktop directory.
And if we also type ls, we can see that the file
is in our desktop directory.
To make sure this file is indeed empty,
we can use the command cat
and this command, once executed on the file,
writes out all the contents that are inside of that file.
Let's try it out.
So we specify cat and then file one,
press enter, and this will give no results
since as we already mentioned,
the touch command creates empty files.
If we want to, for example,
put something inside of that file,
we can use a command echo.
You simply just type it like this, echo
and to put something, you specify after the echo,
let's say today is a really good day.
So we specified an entire sentence
and all we need to do
to put this sentence inside of this file,
is to specify this arrow to the right
and after it specify the name of the file
that we want to put this sentence in.
So echo, today is a really good day,
into the file one, press enter, and if we cat it once again,
with the cat command, so cat file one.
We will see that now it outputs exactly
what we've written to that file, using the echo command.
Okay, that's all good, but there is an easier way
and more practical way to do all of this.
We can use a text editor
to write things inside of a file
and an easy text editor that we can run from terminal
is called nano.
So let's try to do the same thing we just did using nano.
If we type nano and after nano
comes the name of the file that you want to edit,
and since we want to edit a new file
that we haven't created yet, we can simply just type name,
let's call it file two and press enter.
This will open this empty window
where we can type anything we want.
We can type file content here and it can be anything.
We can type for example text here,
but we could also type code if we wanted to.
Let's start with text first.
Let's write just Hello World as a text.
To save this we press control O together, then enter to save
under this name, and then control X to exit the nano editor.
If we now type cat and then file two, and by the way,
we can notice that file two has been created
along file one on our desktop, but if we cat file two,
we can see the output of Hello World.
So we manage to do it only using one tool
which is nano, instead of using two tools
which are touch and echo,
but I also mentioned we can do the same thing with programs.
For example, how can we create
Python program using nano and terminal?
First we need to open a file.
So let's type nano and then file three dot py.
And in this case we add dot py,
because that is an extension for Python programs.
Then inside of it, after pressing enter,
we're going to run a simple command
which is print and then Hello World between the quotes
and you will see that some stuff changes colors.
This is because the nano editor recognizes this
as a Python program.
What we did right here,
in case you're not familiar with Python,
is we just ran a simple function that will print out
the string that is in between the quotes.
So this will just print out Hello World.
Let's save it with control O, then press enter
and then control X to exit.
And luckily for us
Python is already installed in Kali Linux.
So all we need to do to run this program
is to type Python three and then the name of the file,
file three dot py,
press enter and the program will execute.
Here it printed out Hello World, cool right?
You'll also notice that the icon
is different than these two icons.
This is also, once again,
because this is recognized as a Python program,
it even has the Python icon.
Now that we know how to create files,
and execute Python programs, the next question would be,
how can we create directories.
To create a directory, we can use the command mkdir,
which stands for make directory.
To check it out, let us run mkdir
and name a directory folder.
So press enter, and if I type ls,
we will see that we got our files as well as a directory,
which is different color and it is called folder.
Remember we differentiate them by color and also
if we try to change directory to the folder, it'll work.
It'll even give us the path of slash desktop slash folder.
Great, in that folder you can do pretty much
anything you want.
You can create sub folders or create files.
Whatever you want to do, you can.
To move for example, our Python program
from the desktop directory to our folder directory,
we can use command mv, which stands for move.
We must first navigate back to desktop directory.
So let's do it using cd
and then two dots, to go one directory back.
And from it we run the command mv, file three dot py,
into the folder.
So we specify and move.
Then the second parameter is what we want to move
which in our case is file three dot py.
And the last parameter is where we want to move it.
We want to move it inside of our folder,
so press here enter, and you will notice
that the file three dot py disappeared
from our desktop directory.
We can also check that by typing ls,
and notice that our Python program is no longer here.
But if we go to the folder directory and type ls here
we will see it here because we moved it.
Here it is file three dot py.
Now this is a handy command and you will use it a lot.
Besides moving the file, we also want to see,
how we can copy a file
and we can do it using the cp command.
This does the exact same thing as move command,
but it doesn't move it from original directory
to desired directory, it just copies it.
So let's try to copy file three dot py
and call it file four dot py.
If I typed cp, which stands for copy, file three dot py
and I type file four dot py, right after it.
This will create an exact copy
of our file in the same directory.
If I press enter and type ls, here it is,
we got file three dot py and file four dot py.
If you want change whether they're exactly the same
we can cat the content of file three dot py,
and cat the content of file four dot py.
And we can see they're both the same Python programs.
And last command that we want to cover
regarding files is the command rm.
This command deletes files and directories.
And let's say we try to delete file four dot py.
That is our copy of our Python program.
We can do it by typing rm and then file four dot py.
Now be careful with this command,
since once you delete the file,
there is no trash bin where you can retrieve it.
It is gone.
If I type enter and type ls,
you will see our file four dot py is no longer there.
Okay, but how do we delete a directory?
Let's first create a directory
inside of our folder directory.
And to do that we type mkdir and let's call it folder two.
Folder two is a sub directory of our folder.
If I type ls, here it is.
And let's say we did create this directory by mistake
and we want to delete it.
Can we use rm, folder two?
Well, we can try it.
Hmm cannot remove folder two and it will tell us
that it is a directory.
So how do we delete it?
Well, we'll remove it in the same way we remove files.
We just add an option at the end of the command.
So type rm, folder two
and then at the end add space and then dash r,
press enter and this will delete our directory.
Also, double check what you're deleting
with this command since if you go to our slash directory,
which is directory containing all folders
and files in system,
and if you were to type, for example, in that directory rm,
then the star sign and then dash r, this command
would delete entire Kali Linux machine
with all of its files.
So always pay attention
what exactly are you deleting in Linux
and from which directory are you deleting?
Since inside of the Linux
you will not be stopped in deleting anything you want.
You can easily delete a crucial file
for the operating system,
and make your Kali Linux machine unworkable.
That is also a reason why we are practicing
with virtual machine.
So let's, oops.
I actually ran this and you will notice that the results
of this command, if I type ls,
now our folder is completely empty.
So I actually ran this by mistake and keep in mind,
if I actually ran this from the slash directory,
which is our route directory,
I will delete entire Kali Linux system.
For now on, it is good
since I only deleted our Python program
so it is not a big deal.
Okay, great.
We learn bunch of the commands in this lecture.
Now I got the practice test for you
that you can try to do for the next lecture.
Inside of our folder directory,
since I deleted my Python program,
I want you to create the file three dot py, once again,
you can type anything you want inside of it.
You can use the print Hello World statement that we used.
And what I want you to do using the commands you learned
in this video is to copy that file
in the desktop directory from our folder directory.
So you create it inside of this directory
and I want you to copy back to the desktop directory.
A hint is that it can be a little tricky,
once you copy file from some directory to directory.
Anyways, just try it,
and we're going to see the solution in the next video.