The Command Line

The command line is a more intimate way of interacting with your computer (than say, Finder on Mac or Explorer on Windows). We’ve already used it to install Python 3 during orientation (if you haven’t done that, go do that now). You might also hear folks referring to the command line as the shell, Terminal (on Mac) or Command Prompt (on Windows).

I’ve compiled a ton of common shell commands! I won’t repeat them all here now, but remember pwd, ls, cd [foldername], cd .. are really helpful when you are moving around your computer using the command line. A few other things to keep in mind:

  • You can add flags to commands to instruct shell to do specific things or structure its output in a specific way. A few helpful flags to add to the ls command:
    • -l: displays more information about your files and folders (you’d enter it as ls -l in your shell)
    • -h: makes the displayed filesize numbers human readable!
    • -a: all, shows you hidden files, usually important stuff your computer doesn’t want you to deal with.
    • You can use a bunch of flags together ls -lh or ls -lha
  • A few helpful cd commands:
    • cd .. moves you “up” one directory
    • When you see ~ in terminal, it means you’re in your home directory.
    • cd ~: takes you directly to your home directory
    • cd ~/Downloads: takes you directly to your Downloads folder
    • cd [drag the folder from Finder into Terminal]: it’ll autocomplete the path to that folder! This only works on Macs.
  • If your command line is running something it shouldn’t be, use Ctrl+C to escape/exit.
  • clear will do exactly as it sounds; your Terminal/Babun shell will be cleared of all commands and output.
  • File and folder names have to be typed exactly as is! cd desktop and cd Desktop are different (you probably want to use the latter).

Exploring Folders & Files

In class, we downloaded a file from Slack to test out our new commands! Extract the zip file, and then:

  1. pwd to see where you are
  2. ls see what’s there!
  3. cd ~/Downloads to change directory into your Downloads folder
  4. cd class-01
  5. pwd to check you’re in the right place!
  6. cat [filename] to view a file’s contents!
  7. Typing the file name is super annoying though - write cat [first letter(s) of your filename] and then hit TAB to autocomplete the filename.

TIP: If pressing TAB does not autocomplete your folder or filename, then you may need to enter a few more letters to disambiguate which folder or file you mean.

Also, make sure that autocomplete gave you the ENTIRE folder or file name. Sometimes, you will have to enter the next letter hit TAB again, especially if there are spaces in your filename.

DON’T PUT SPACES OR APOSTROPHES IN YOUR FILE NAMES.

Besides using cat to see the entire file, you can also take a peak at the beginning or end of a file.

  • head -n 20 [filename]: see the first 20 lines; change the number to see more or less lines.
  • tail: use tail to see the end of the file; structure the command the way that the head command is structured directly above.

Treasure Hunt!

So we found an awesome treasure in class! The MOST IMPORTANT takeaway from the exercise was that you should use TAB to autocomplete folder and file names as much as possible. See above tips on using autocomplete/TAB.

  1. Navigate to where your downloaded (and hopefully extracted) zipfile is located.
  2. cat instructions.txt to see the treasure hunt instructions.
  3. You’ll notice that the folder and file names are cuh-razy. Use TAB/autocomplete to get your computer to do all the hard work for you.
    • \ indicates that the space character is actually part of the name you’re looking for! The \ is an escape character – because spaces can mean other things on Terminal, using the \ before a space lets Terminal know that you really mean it! You’ll see this structure appear with other “special characters” as well (not just spaces).

TIP: Use ↑ and ↓ on your keyboard to move through the other commands you’ve typed on the command line.

Other fun commands

  • grep: allows you to search for a specific set of characters inside of a file
    • grep [search term] [filename]
    • grep -i [search term] [filename]: case insensitive search
    • grep "[search term]" [filename]
    • grep -r "[search term]" [directory or folder name]: this searches all the files in the specified directory for your search term
    • Note that the search terms are case-sensitive
  • man [command]: see the manual entry for that command. Ex: man wc
    • use q to escape from the manual, because it’s horrible
  • wc -l [filename]: get the line count for the given file

Piping a way of connecting different commands together on the terminal.

  • grep bugs [filename] | wc -l: displays the line count of the output of grep bugs [filename] (rather than the whole file)
  • More on piping.

How to write and run Python scripts

  1. Open up your text editor and create a new file: File > Save “intro.py”
  2. On the command line, cd into the folder where you saved intro.py.
  3. Let’s edit intro.py in your text editor so that our script actually does things!

Remember: We use # to write comments within our code.

Using OS X commands on Windows

Command Prompt has a different set of similar commands - see this reference. We can install some software CoreUtils, though, which gives us the “normal” OS X/Linux commands.

First, we’ll need to install Chocolatey. It’s a “package manager” which means it lets us install software on the command line without going through big installer programs. The OS X version is called Homebrew.

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

Then we’ll use Chocolatey to install CoreUtils

choco install gnuwin32-coreutils.install

…and life is now perfect! ls and pwd forever!