Command Center
LAB2

command.jpg

Welcome

You’ve heard that data scientists are high key in demand, so you’ve decided to get into this fascinating field. You know that data scientists need to be good at organizing data, and since you love to travel, you decide to practice your data science by organizing your photo library by city, country, and region. But your data scientist friend tells you there’s more to their craft: they don’t organize data by dragging and dropping file icons—they communicate with a computer by typing commands on this thing called the command line. You are now motivated to glow up and become command line literate.

What We Will Learn

Basic commands Files and Folders Navigating the file system Creating folders Moving folders Creating files Moving files Deleting files Editing files The Python REPL

Activity

If you are using Windows, install Git for Windows. Then launch the program called Git Bash. You will be using Git Bash rather than PowerShell throughout the course.

If you are using macOS, you might already have the program called Terminal on your task bar. Launch it if so; if not, hit ⌘-Space and type in terminal. If you have any other operating system, make sure it has a Unix-like shell (chances are if this is you, you have a niche OS and already know what you are doing 😀).

Vocab learning time: The terminal is a visual program providing a CLI (command line interface) on which commands are issued on the command line. The commands are executed by an internal program called a shell. Don’t worry too much about the distinctions for now: in practice, people mix up these terms all the time.

Now let’s try out some commands. We’ll do these together and explain everything during class.

Here are some commands that don’t work on Windows (even with Git Bash). If you have macOS, try them:

Exercise: What the heck is going on with September 1752?
How many commands are there?

Hundreds at least. The number is actually unlimited because you can install new software that comes with additional commands, and you can write your own commands!

There are nice online resources listing built-in commands for the Mac and built in commands for Linux-like systems. If you are running Git Bash, enter the command: ls -1F /bin | grep '\*$' | grep -v '\.dll\*$' | sed 's/\*$\|\.exe\*$//g' | column to see a list of the basic commands it provides.

File System Commands

Now, we’ll do these super important commands that manage your computer’s file system (and we’ll discuss what’s happening during class):

Now after these commands, assuming your name is Jessica, the current folder, a.k.a. current directory is something like /Users/jessica/cmsi1010/lab02 on a Mac, /home/jessica/cmsi1010/lab02 on Linux, or /c/Users/jessica/cmsi1010/lab02 on Git Bash under Windows. Because different systems put user folders in different places, there’s a cool convention: the folder named ~ refers to your home folder! So we can say, whatever system we’re on, you can always refer to this folder as ~/cmsi1010/lab02.

Is it “folder” or ”directory”?

No one cares. Pick which one you like better. “Directory” is the OG term, which is why cd got its name: it means “change directory.” Ditto for mkdir.

Now let’s practice: We’re going to fetch a zip file of completely messed up folders and files, unzip it, and use find . to see not only the files in your current folder but those in all of the subfolders.

Ah, so similar to how ~ is your home folder, . is your current folder.

Here is what your folder structure for this class should look like at this point:

cmsi1010
├── lab01
└── lab02
    ├── places.zip
    └── world
        ├── africa
        │   ├── ghana
        │   └── italy
        ├── asia
        │   ├── china
        │   ├── iceland
        │   │   └── matsya-narayana.jpg
        │   └── spain
        │       └── spanish-steps.jpg
        ├── europe
        │   ├── egypt
        │   ├── india
        │   │   └── bahia-palace.jpg
        │   └── palau
        └── oceania
            ├── morocco
            └── zambia
                └── victoria-falls.jpg

WAIT THIS IS REALLY MESSED UP! We’re going to get these in order using only the command line. Practice matters! Here are the first few commands:

So we got pretty far along ourselves, but now your job is to figure out the commands to do each of the following. As you do these things, keep a record of the commands you entered. (Remember, you are currently “in” Europe. You can stay there or move back up to the world folder. Your choice!

Congratulations! Your lab02/world folder should now look great.

Let’s practice adding new folders. From the world folder:

OH! Just realized something: that zip file is still there, isn’t it. Let’s get rid of it:

You are getting good manipulating the structure of the file system and for creating folders. How about creating a file? We weren’t doing files in the previous lab, so let’s create a file in the lab01 folder for a triangle program. First we use cd to get into our class folder, then touch to create the file:

How do you know the file got created? Try these commands, which we will explain in class:

The point is, the new file is empty. It has a size of zero bytes. How do we get content into it? Fun fact: übergeeks can use the echo command, but even for them, for a file with multiple lines, a text editor is better.

Text Editors

The most popular these days for programmers is Visual Studio Code, or VSCode for short. Install it. Then launch it.

Within VSCode, open a “workspace” at the cmsi1010 folder. Note how easy it is to expand and collapse the folders in your workspace: VSCode gives you a really nice view of your files. Everything you do in this course can be done within this workspace.

You can also run VSCode from the command line

Optionally, do some research to find out how to run VSCode from the command line. If you are able to set things up to do so, go to your cmsi1010 folder (remember, cd gets you there), then run the command:

  code .

That can be quite convenient.

Once open, find your simple_triangles.py file in the VSCode file explorer. Next, edit that file as follows, and don’t forget to save it:

def print_triangle(character, lines):
    for count in range(1, lines + 1):
        print(character * count)

print_triangle(character="@", lines=8)
print_triangle(character="&", lines=13)
print_triangle(character="o", lines=5)
If you haven’t edited files before

It won’t be so bad! A text editor is like a word processor, but used for programs, data files, notes, and the like. When you create and save files, they are created not in an online account, but directly on your computer’s file system.

Oh, and if you are new to “saving” files, you will get used to that eventually. We will explain what that means in class, too.

The python Command

Once you have saved that file, go back into the Terminal (or Git Bash) and go to the lab01 folder (something like cd lab01), where we will try to run this program. To do so, we’ll need a Python runner installed on our machine. It might already be there. To see if it is, type:

If you see something like command not found: python3 then try:

You should get a response like Python 3.13.2. If you have anything less than Python 3.13, then you need to install Python on your machine. It is recommended you install Python from the official Python downloads page. After downloading, run either python3 --version or python --version (whichever one works). If you can’t get either to work, get help during class.

And now, we can run a program:

or,

It would be nice if the world would standardize on the name of the Python command, but alas.

One last item. The python (or python3) command will run a Python program if you give it the name of a file, but if you just enter python or python3 with no argument, you can enter a little command line environment in which you can execute bits of Python. This environment is called the Python shell, Python interpreter, or Python REPL. Its prompt is >>>. Try it out now. Inside the Python environment, here are some suggested commands. (The last one, exit() is a good one to know!)

>>> 10 * 5
50
>>> 3 ** 21 + 5
10460353208
>>> "Hello" + ", " + "World"
'Hello, World'
>>> type(3)
<class 'int'>
>>> type(3.14)
<class 'float'>
>>> type("Hello")
<class 'str'>
>>> type([1, 2, 3])
<class 'list'>
>>> exit()
Exiting the Python Interpreter

One of these ways should work: entering quit, entering quit(), entering exit, entering exit(), the keychord Control-D. If desperate, the keychord Control-C might also work, as does closing the Terminal Window in which the interpreter is running.

The Terminal Inside VSCode

Tired of moving back and forth between the Terminal and VSCode applications? You can open an integrated terminal right inside VSCode! Just go to the menu and select Terminal > New Terminal. There are ways to use multiple terminals in the terminal window. Ask a friend or a friendly chatbot how.

If you are a Windows user, you will need to make sure that you set the terminal program to Git Bash rather than PowerShell or Command Prompt. The course staff can help you if require assistance.

It’s good to be able to use either the Terminal program or the integrated terminal in VSCode. It’s also fine to have your own preference, too.

Challenges

Now it’s your turn. Here are some ideas for you to extend the activities above:

Further Study

Keep up the momentum with these recommended reads and activities:

Summary

We’ve covered:

  • How to launch the terminal
  • Alternate names for the terminal: shell, command line, CLI
  • The commands date, df, echo, whoami, hostname, ps, clear
  • File system commands: pwd, ls, cd, mkdir, curl, unzip, find, rm, touch, cat
  • The special names: / (root folder), ~ (home folder), . (current folder), .. (parent folder)
  • VS Code
  • The Python REPL

Recall Practice

Here are some questions useful for your spaced repetition learning. Many of the answers are not found on this page. Some will have popped up in lecture. Others will require you to do your own research.

  1. What does one do with the terminal program?
    Issue commands
  2. The term GUI stands for “graphical user interface.” What kind of interface is provided by the terminal and what is its three-letter abbreviation?
    A command line interface, or CLI
  3. What do we call a program that interprets commands entered in the terminal?
    A shell
  4. What command can be used to download a file from somewhere on the web?
    curl
  5. What command can be used to unzip a file?
    unzip
  6. How can you see what is in a zip file without unzipping it?
    unzip -t
  7. What is a file system?
    The organized collection of files and folders on a computer system, together with all of the mechanisms for managing the files and folders
  8. What is the name of the root folder?
    The root folder is called /
  9. What is the name of the home folder?
    The home folder is called ~
  10. What is the name of the current folder?
    The current folder is called .
  11. What is the name of the parent folder?
    The parent folder is called ..
  12. What command tells you the current folder?
    pwd
  13. How do you make your cmsi1010 folder the current folder?
    cd ~/cmsi1010
  14. How do you “move up” (that is, make your current folder’s parent folder the current one)?
    cd ..
  15. What command lists the files in the current folder?
    ls
  16. How can you lists all the files in the current folder and all subfolders?
    find .
  17. What command creates a new folder?
    mkdir
  18. What command creates a new file?
    touch
  19. What command removes a file?
    rm
  20. What command moves a file?
    mv
  21. How do you move the file named dog that is currently in the folder named animals into the folder named canines?
    mv animals/dog canines
  22. How do you rename the file named best to bestie?
    mv best bestie
  23. What command shows the contents of a file?
    cat
  24. How do you get extra information about the files in a folder?
    ls -l
  25. What kind of a program is VSCode?
    A text editor
  26. What program runs Python programs?
    python3 or python, it depends
  27. What are some names for the Python REPL?
    The Python shell, the Python interpreter, the Python REPL
  28. What is the prompt for the Python REPL?
    >>>
  29. How do you exit the Python REPL?
    By entering exit(), quit(), or by using the keychord Control-D