Now,
in the last lesson, we saw how to get up and running with our brand new Flask
server. In this lesson
I want to take it back a little bit and talk briefly about some of these
commands that we put into the terminal here,
because the terminal is actually a really powerful tool.
And it's something that's also referred to as the command line and its a way of
typing in commands one line at a time in order to control our computer.
The command line is also known as the shell.
So what exactly is a shell?
If you think about your operating system as a pistachio nut,
then the kernels of the actual pistachio, the thing that you're going to eat,
I'm really sorry about all the food analogies.
You can clearly tell I'm pretty much hungry most of the time when I'm recording
these videos, but let's get back to the topic.
So if your operating system is a pistachio,
then the actual nut is the kernel. And in computing
the kernel refers to the actual program that interfaces with the hardware.
So it's the core of your operating system. Now,
the shell of your pistachio or the shell in computing refers to
the user interface for you as a human
to be able to interact with the kernel and in turn with the hardware of your
computer. And there's two variants to the shell.
There are graphical user interface shells, so for example,
when you open up finder
you're using a graphical user interface to access and find your files.
But there's also a command line interface
which is what we're going to be talking about and what we're going to be
learning about in this module.
So this is an alternative way of interfacing with the kernel.
Now it can seem intimidating at first,
but I want to show you that it's actually super simple.
And once you understand it, you're going to see that it's actually really,
really useful tool at your disposal.
A lot of the things that you can do with command line can be done using the
graphical user interface. So why use
the command line at all? At the end of the day,
what it all boils down to is about greater control.
And in your journey of becoming a better developer,
you will need to understand and exert more power over your tools,
that is the computer hardware. But on a more day to day basis,
you will find that if you're a regular command-line user
that it's actually easier and faster to do a lot of really common things.
If you're a Mac user, you've actually got a dedicated terminal application.
So if you go to spotlight and hit the search button and look for the terminal
and hit enter, you can see that this is the actual terminal application.
Now I've styled it up to look a bit more like a hacker because I like scaring
people in Starbucks, but that's not the only you can change.
You can change the color of your terminal,
you can also change the type of shell you're using. Currently,
if you are running macOS Catalina or above, so Big Sur
or any of the future operating systems, you're going to be using zsh
which has now the default Mac shell.
1: All right,
0: Now, if you're on Windows however, the default shell is the command prompt.
So if you type in to search for the command prompt, you can see
this is the app you need to launch and this is your command prompt.
For the most part, you can type in the same commands on Windows and on Mac
and it will work on both.
I want to show you some common things we can do in the terminal. Here
I've got my terminal and the address of the terminal is determined by the part
that's just before the prompt. So at the moment, it's a ~ on my Mac.
There's a couple of things I want to show you that you can do with the terminal.
The first thing that's really useful to know is where am I in my file path?
And to do this, we have to write pwd, print working directory.
And this tells me where I am. Once you open up your finder
as you can see up here, go to view and then go to show path bar.
And this is showing us the path from our hard drive/ users/angela,
which is exactly the same location as what I've got now in my terminal.
So the print working directory is really useful for you to locate where you are.
Similarly, on Windows, you can see when I do print working directory,
it tells me that I'm in the C:\users\username.
So C drive, users, username... right here.
The next command that is really useful is ls,
which stands for list.
And what this does is it will list all of the files and folders at your current
working directory.
So it's going to list everything that's in this current location where I am at
basically this. So if I go ahead and hit enter,
you can see all of the same folders showing up as it does
in finder. Pwd and ls are really useful for finding things out, for displaying
things.
But we can also use the terminal to move around in our computer.
And we do that using the CD command, which is changed directory.
So if at the moment, my working directory is users/Angela
and I want to get into my desktop folder
which is clearly one of the folders that I can access, then I can simply say
change directory to desktop. Now,
one of the things you can do to prevent yourself from getting repetitive strain injury and
typing a lot is you can type the start of the folder, so for example desktop-
De and then hit tab,
and it will narrow down on the possibility that you can go to so there's
development or there's desktop.
So if I add one extra S and I hit tab,
it's now going to automatically insert the folder that I'm looking for,
which is desktop. So now if I hit enter,
you can see that this part before my flashing prompt has changed and it's
changed to the desktop. So now, if I do pwd,
you can see I'm now in users/angela/Desktop,
which is the equivalent of double-clicking on this basically.
1: Okay.
0: On my desktop what if I wanted to create a new folder? Well,
we can do that using the make dir or make directory command.
So we're saying make directory and then we give it a name. So let's say
I want to call this Test then I'll just say mk
dir Test. And notice the location while you are at.
So I'm going to be making this test directory on my desktop.
And as soon as they hit enter, there you see it.
It's created that folder from scratch.
So now here's a question. If you wanted to navigate into Test so that when you
print the working directory,
it's going to output users/yourusername/desktop/test.
What would you do? What would be your command? Pause the video and
have a think about that. Alright,
of course, we have to change directory coz at the moment we're in the desktop
which is this, and if we want to go inside this test folder,
then we have to cd into it. So cd into test,
and now we're inside the test folder. And if I hit pwed, you can see
this is where I'm at. Now,
once I've cded into this new directory,
I'm going to create a new file in this directory. To create a new file
the command is a little bit different from creating a new directory.
The key word is touch. And after the key word, there's a space
and then we can provide the name of our file.
So let's say that we want to create a new Python file
which we'll call main.py. Now,
what it's going to do is create this file code in main
which is a Python file and it's going to do that inside the Test folder.
So let's hit enter and there you see it.
There's our Python file created without any clicking or dragging completely done
with our command line. So we've seen how we can create folders,
how we can create files. And by the way,
if you want to create a different type of file that you just change the
extension. So it could be .text,
or it could be .docx or whatever file format you want to create. Now,
what if we wanted delete our file? Well,
all we have to do is make sure we're in the correct directory to be able to
access that file. So when we hit ls,
we should be able to see the file we want to delete.
And then all we have to do is type rm which stands for remove
and then we could put our main.py. So there it is, it's gone.
1: Okay.
0: Now, what if I want to delete the Test folder? Firstly,
I have to navigate one step up. Instead of being inside the Test folder,
I can't delete the Test folder while I'm inside the Test folder.
I have to go one step up, which is the desktop folder.
Now of course I could say cd into the full path
which is /Users/angela/Desktop.
Now this would work, but this is really wordy.
A really simple shortcut for navigating
just one step up is writing cd and then two dots.
This is going to take us to the parent folder, which is now Desktop.
1: Right.
0: From here I'm going to delete this folder. Now to delete a folder
we can't just put rm. We also have to put a flag.
So a - and then rf. So this is going to recursively, forcibly, remove this
folder that we're going to add here, which is the Test folder.
Now the thing you have to be really careful about rm -rf is it does things
recursively. So if there were lots of folders inside this Test folder,
then it's going to delete everything in there.
And if, God forbid, you actually have this and you put it onto your whole computer,
well then that's going to be a real disaster.
Because the thing you have to know about the terminal is that it's a very
powerful. And what did Spider-Man's uncle say? With great power comes great
responsibility.
There's some pretty funny stories about people who accidentally deleted
everything on their computer just by messing with rm -rf.
So be really careful that you are in the correct location to delete the folder
and to double check that it's the right folder.
Because when I hit enter,
you can see it's not going to ask me for a confirmation
and it's also not going to put that folder or the containing files into the bin.
So be really careful when you're using the terminal.
There you have it.
There's an introduction to the command line using zsh on a Mac and using the
command prompt on Windows. Now,
when you open up PyCharm and you go to this emulator terminal,
so it's a sort of application within an application,
you can see that it looks pretty much the same.
Basically, all that PyCharm's done is just bundled the command
prompt into the PyCharm on windows and the zsh terminal into the PyCharm
on Mac. So you can do everything that you can do in the terminal
inside PyCharm as well
which makes it really easy to pip install modules or to run our Flask server.
Now,
if you want to know about other things that you could do in the Windows command
prompt or the Mac terminal, you can simply just search for command prompt
cheatsheet or terminal cheatsheet.
And what it will take you to are lots of addresses
which contain basically a lot more things that you could do with your command
line. But the ones that we've talked about today
are some of the most common ones and 99% of the time,
those are going to be the commands that you're going to be sticking to.
So write them down if you need to review them regularly.
And if you just use them on a regular basis,
you won't have any trouble remembering them.