Skip to content

Instantly share code, notes, and snippets.

@johnsogg
Last active January 11, 2024 02:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save johnsogg/bfa37707275b67e4e8ac6a87009cc491 to your computer and use it in GitHub Desktop.
Save johnsogg/bfa37707275b67e4e8ac6a87009cc491 to your computer and use it in GitHub Desktop.
Command Line Interface crash course

Command Line Crash Course

I'm writing this for my 2270 Data Structures students, though I bet others will find it helpful too.

You might be using a command line for the first time, and if that's true, you're certainly using Git on the command line for the first time. It can be daunting, but don't sweat it.

A command line interface (CLI) is based on a textual back-and-forth. You type a command, and if the CLI can understand it, it will run some code and maybe give you a response. In this sense, working with a CLI is like working with a programming language. You have to use the right syntax.

The syntax and behavior depends on which particular CLI you're using. A CLI is typically powered by a program called a Shell, and for our purposes we can use the terms CLI and Shell interchangeably. macOS now ships with zsh as the default; many Linux and older Macs tend to use either the Bourne shell (sh) or the Bourne Again shell (bash). Windows also has its default command line (which I know nothing about) and PowerShell on Windows is the one thing I am aware of.

Many commands interpret your command in context of the directory you're in, so knowing 'where' you are is usually very important. There are special locations - your current working directory, the parent directory, your home directory, the "root" directory, and so on.

Also there are many symbols (using punctuation characters) that are meaningful to the CLI that you need to understand.

I'll show you a bunch of commands below, annotated so they hopefully read easily.

How To Read Examples

The cheat sheet below involves two important symbols that you need to understand. First, the dollar sign $ --

$ foo
zsh: command not found: foo

It is traditional for a CLI to show a "prompt". Whenever you see something like the above, the $ character is a stand-in for the prompt. It is what the CLI prints out to let you know that it is waiting for your input. In this example, the CLI put the $ there, and the user typed in foo and then pressed Enter. The CLI printed the message zsh: command not found: foo to let you know the outcome. In documentation, whenever you see a $ it means the text after the prompt is what you should type, but you should not type the $ itself!

Second, the # symbol (hash sign, octothorpe, pound sign, sharp) --

$ foo # everything to the right of the hash sign is a comment and is ignored

An Incomplete Cheat Sheet Of CLI Commands

# Where am I?
$ pwd # Prints the "present working directory", sometimes called "current working directory".
$ cd # "change directory" with no arguments will take you to your home directory
$ cd ~ # Same as above - go to the special "~" directory, which is a synonym for your home diretory.
$ cd foo # Change directory to one that is named "foo" and is expected to be contained in your current directory.
$ cd .. # Change directory to the parent - the two dots, ".." always means "parent directory".
$ cd . # Change directory to the directory you're in. A single dot, "." always means "same directory".
$ cd / # Change directory to the system "root" directory, which contains everything.
$ cd ~/foo # Change directory to a folder named "foo" that is contained inside your home directory.

# Showing files
$ ls # basic command to show files in the current directory. Pronounced "list".
$ ls -l # Show more stuff like file sizes, permissions, edit dates. Pronounced "list long".
$ ls -a # Show "all" files, including dot files which are treated as hidden. Pronounced "list all".
$ ls -la # Combines "long" and "all" to show all files in long format. Pronouncing these things out loud gets awkward fast.
$ ls -R # List directory contents in this directory and recursively all child directories.

# Invoking Commands
$ mkdir foo # Make a folder named 'foo'. It expects to find the 'mkdir' command in your PATH (see Environmet Variables below).
$ echo $PATH # prints your PATH environment variable to console.
$ monkey # Invoke a command 'monkey' that it expects to find in your path
$ monkey -t # Invoke 'monkey' with a flag. Commands all interpret flags differently. Hope monkey knows what to do with t.
$ ./monkey # Invoke 'monkey' that is explicitly expected to be in the current directory. If not, it won't work.
$ ../monkey # Invoke 'monkey' that is expected to be in the parent directory.
$ build/monkey # Invoke 'monkey' that is expected to be in a directory called 'build', which is in the current directory.
$ ./build/monkey # This has the same meaning as the previous line.
$ which monkey # Prints out the location of the monkey command, if it can be found.

# Redirecting input and output
$ ls -l > ~/file-listing.txt # List files, but instead of printing to console, write to the file ~/file-listing.txt instead.
$ ls -la | grep zsh # List files, and 'pipe' the results into the 'grep' command. 'grep' will print results.
$ ls -l `which monkey` # Give a long listing of the result of "which monkey". Note use of the backticks!
$ make && ./run_app_1 # Run the 'make' command, and if it succeeds, execute 'run_app_1' in your current directory.

Environment Variables

Your shell maintains state in a few ways, and one of the most important is by environment variables. When your shell starts, it loads a little script that can establish environment variables. One extremely important environment variable is PATH, and it lists out the directories that it will look in to find commands that you type in. For example, when you type make, it is actually invoking a command that is in a different directory. On my computer, this is /usr/bin/make.

To read or use an environment variable, put a dollar sign in front of it. Try it with and without the dollar sign to see what I mean:

$ echo PATH # boring, it literally prints PATH
$ echo $PATH # prints something like /usr/local/bin/usr/bin:/bin:/usr/sbin:/sbin

Impress Your Friends With Keyboard Shortcuts

Yes, it is maybe shocking that the CLI doesn't really understand your mouse clicks. But there's a lot you can do with your keyboard!

  • Tab - autocomplete a command. Type ba then Tab a few times to see what happens.
  • up arrow - press the up (and down) keys to navigate backwards (and forwards) in your command history
  • ^R - search backwards in your command history - just type and hit Enter when you found what you want
  • ^A - go to the beginning of a line
  • ^E - go to the end of a line
  • ^F - go forward one character
  • ^B - go backward one character
  • ^L - clear your terminal (also, typing 'clear' probably works)
  • ^U - erase what you've typed in and reset the current input to empty
@RuthNanny
Copy link

Excellent source. My Vector10 source files disappear from Jupyter. I will need this to try to push it from GitHub. Cheers, NannyGranny Ruth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment