BYU logo Computer Science

Introducing the Terminal Transcript

Start visual description. The instructor’s screen is shared where he shows how to prepare and write code for a particular program. He demonstrates the steps as he describes each aspect. The instructor can be seen in the top right-hand corner in a small box. End visual description.

[00:00:00] Instructor: Hey! Let me introduce you to the terminal. So the terminal is a program that we use to interact with the computer, with the files and folders that are on the computer as well as the programs that are installed on the computer with just the keyboard, just through text as opposed to using a mouse moving around and clicking on things.

[00:00:23] There’s many ways to open a terminal, but I recommend that you use the terminal that opens inside of PyCharm. So down at the bottom, there’s a bunch of tabs here. One of them is terminal. They might be in a different order for you. You can move them around by clicking and dragging that little button.

[00:00:42] I put mine at the end because I use it a fair amount. You can click the terminal and it’ll come up and give you a little prompt here and your terminal might look different than mine. It probably does. That’s OK. The important part is the commands that I’m going to show you and what those commands do and those will be the same.

[00:01:07] There will be some information in front here. We call this the prompt, terminal prompt. Just like when you write a Python program and you use input and you put a string in there, we call that the prompt. This is just prompting you.

[00:01:20] It may include a username in there. It may tell you something about the virtual environment that you’re running like you have down in the bottom right of PyCharm. It may tell you something about the folder that you’re in. Right now my terminal, when I open it, is showing me that I was in this practice folder here.

[00:01:43] When you open your terminal the very first time, it will show you most likely this folder at the very, very top. I’m going to actually step out just a little bit to that folder. We clear the screen. All right.

[00:02:02] So this is what you’ll see when you’re starting at the very beginning is this folder here, probably something like CS 110, if that’s what you called your project. In this case, I’m showing you my in-class project. And there’s a few commands now that you can use to understand what information is available to you and then to use that information.

[00:02:22] So first I’ll show you the pwd command. The sensor print working directory, and it will show you where on your file system this folder is. So this says starting at the very beginning under my user folder in gbean, in the folder teach, in the folder CS 110, in the folder in class. That’s where I am right now.

[00:02:46] I can type LS to see what’s inside this folder and I can compare what I see here with what I’m seeing up here. My in-class folder has two folders, fall 2022 and winter 2023, which is what I see here.

[00:03:01] Now in the file browser, I might click on this to open it up and see what else is inside. The terminal equivalent to that is CD or change directory. And so I can change directory into the winter 2023 folder. Now, rather than typing that out, let me do this in slow motion. I said win and then I hit the tab key on my computer and it finished the typing for me.

[00:03:32] I like typos. I produce them all the time. And so tab completion, the ability to use the tab key to finish or complete something, is really nice because there’s less typing on my part, less likely to get wrong. So I encourage you to practice tab completion as well.

[00:03:49] Type one or two of the letters that you need at the beginning and then hit tab and let it figure out which one you’re talking about. Saying wi, between these two options, which one is the one I’m hoping it will be? Clearly, winter not fall. And so tab will complete that.

[00:04:09] So CD and the name of a directory will change to that new directory. So now it says I’m in the winter folder. If I do pwd, you’ll see, I’m in that same place, but with the winter folder added, right?

[00:04:25] Well, what’s in here? And I can type ls and I can see unit one, unit two, unit three, unit four, unit one unit two, unit three, unit four. So I can see the unit four and then ls and look at what’s in here. And I have two lectures in here and I could CD into lecture. Now, if I hit tab here, which one is it going to give me? There’s two options.

[00:04:47] Now on my computer, if I hit tab, it will fill in as much as it can, and then it will stop because it needs me to decide. Is it a zero or is it a three? If I type three and hit Tab again, it’s able to finish it. On other computers, perhaps yours, you’ll find that when you hit tab, it will fill in with the first option it can find. If you hit tab again, it will go to the next option that it could find. So you get to play with your own tab completion and learn how that works.

[00:05:19] But the idea is that as much as possible use tab completion so you can avoid having to type things out by hand, right? So I can CD into this program arguments folder and I can look inside. What do I have? I have an argument demo folder, indexing folder, practice folder, and a Python script.

[00:05:38] Well, the one other command that you’re going to need besides PWD, CD, and LS is the Python command. Python. Python, just like CD, expects you to add one more thing. And so I’m going to give it hello world dot py. And again, I started typing there h e l and I hit tab and it figured out what file I was talking about, and I can run this just like other commands and it will run that Python script.

[00:06:10] Let’s come over here and look at what that looks like. So hello world dot py, just a simple if name equals name, print hello world. Really simple program, but we’re running it down here, right? That’s the… put a little smiley face in there. Let’s run it again. Hello World smiley face, right? That’s the script we’re running right now.

[00:06:34] So we’re used to clicking on the play button and saying hello, you know, run it. But you can also do that through the terminal and just saying Python and then that script. Now to run it, you’ll remember here, we’re in this folder where helloworld dot py is stored. Helloworld dot py is under this program arguments folder.

[00:06:56] If I weren’t there, so I move up a directory, moving up a directory you use dot dot. So now you can see we’re in the unit four directory. I’ve moved up. There’s no hello world dot py file in here. So if I try to run hello world dot py, it’s going to say I can’t find it. This file under the unit four folder, there’s no hello world dot py file, no such file or directory. So it can’t run it. So you need to be next to the file in order to run it.

[00:07:33] One other thing that I just demonstrated here, when I’m in the terminal, if I press the up arrow on my keyboard, it will bring up the last thing that I ran, which is really convenient when you’re running a Python script and you change it and you want to run it again, you just press the up arrow and it’ll bring up the last thing you did and you can run it or change it if you need to turn it again.

[00:07:57] So let’s CD back into lecture program arguments. I’m going to press the up arrow. So did you see the first thing? It said a CD, I’m already there. I don’t need to run that again, but I press up again to get to the Python hello world dot py command. And of course, I could type that out again if I wanted to and run it.

[00:08:16] So that’s how you run a Python script from the terminal. From there now, we want to look at how we get commands, information to the commands that we’re running. So we see that CD takes a directory name and Python takes a script name. We can actually write Python scripts that themselves take additional information. We’ll see that in the argument demo. I’ll make a separate video for that. See you over there.