Skip to content

Terminal and shell

~14 min

In short

The terminal is a window that runs a shell, a program that reads your commands and runs them. Most of your work in later courses, from Git to Python to servers, goes through it. This page teaches the twelve commands you will use most, how to chain them, and how to ask a command for help.

What you need

  • macOS or Linux: open the Terminal app (macOS) or any terminal emulator (Linux). The examples here were run in zsh, the default shell on the Mac used for checking. Linux systems often use bash. The commands on this page work in both.
  • Windows: install WSL, the Windows Subsystem for Linux, and use its Ubuntu terminal [1]. From an administrator PowerShell, run wsl --install and restart the machine. The commands on this page then work unchanged. Windows PowerShell is covered at the end.

Every command on this page was run in a scratch folder under /tmp, so nothing in your home folder changed. Your owner name, file sizes, and dates will differ from the examples. The text after $ is what you type. The lines below it are the output.

Where am I? pwd

pwd prints the working directory, the folder your commands act on by default. cd changes it [2]. With no argument, cd goes to your home folder.

$ pwd
/tmp/cs-lab
$ cd notes
$ pwd
/tmp/cs-lab/notes

List files: ls

ls lists the contents of the current folder. ls -l gives a long listing with permissions, owner, size, and date. ls -a includes names that start with a dot, such as .git and .env. ls -lh prints sizes as 1K or 2M [3].

$ ls -l
total 8
-rw-r--r--  1 ankush  wheel  36 Oct 18 00:27 hello.txt

The first column is the permission string. The Linux basics page explains how to read it.

Make, copy, move, and delete

$ mkdir notes            # make a folder
$ mkdir -p a/b/c         # make nested folders in one step [4]
$ cp hello.txt copy.txt  # copy a file
$ mv copy.txt renamed.txt  # rename (or move) a file [5]
$ rm renamed.txt         # delete a file

Two warnings. rm deletes the file right away, so keep a backup or put anything you cannot lose under Git. rm -r removes a folder and everything inside it, so use it only when you are sure. mv -i asks before it overwrites a file [5].

Read a file: cat

cat prints a file to the screen. cat -n numbers each line [6]. For a long file, use less and press q to quit, or head and tail to see the first or last lines.

$ cat hello.txt
hello from the terminal
second line

Search: grep

grep prints the lines that match a pattern [7]. The options you will use most:

  • -n shows line numbers.
  • -i ignores case.
  • -r searches every file under a folder.
  • -c prints a count of matching lines.
$ grep -n "second" hello.txt
2:second line
$ grep -r "zzz" notes ; echo "exit=$?"
exit=1

grep exits with status 1 when nothing matches [7]. The echo "exit=$?" part prints that status, so you can see it. A status of 0 means success.

Pipes and redirects

Three symbols connect commands to each other and to files.

Symbol Meaning Example
> Send output to a file, replacing the file's contents [8] echo hi > a.txt
>> Send output to a file, adding to the end [8] echo again >> a.txt
\| Send the output of one command to the input of the next [9] ls \| wc -l
$ echo "second line" >> hello.txt
$ ls | wc -l
       2
$ printf "apple\nbanana\napple\n" | grep -c apple
2

wc -l counts lines [10], so ls | wc -l counts the entries in the folder. The printf command writes three lines. grep -c counts the lines that contain "apple".

Errors go to a separate stream, called standard error. 2> redirects that stream to a file [8].

Get help: man

man opens the manual page for a command [11]. Press q to leave. The manual has numbered sections: 1 is user commands, 2 is system calls, 3 is library functions, and 5 is file formats [11]. Use man -f to search the one-line descriptions.

$ man -f grep
grep(1), egrep(1), fgrep(1), rgrep(1), ... - file pattern searcher

Options can differ between macOS and Linux. When an option fails, read the page on the system you are using.

A short exercise

Run these in a scratch folder, not in your home folder:

$ mkdir -p /tmp/practice && cd /tmp/practice
$ printf "red\ngreen\nblue\n" > colors.txt
$ grep -n "e" colors.txt
1:red
2:green
3:blue
$ grep -c "e" colors.txt
3
$ cat -n colors.txt | tail -1
     3  blue
$ rm colors.txt && ls
(empty)

All three words contain an e, so grep prints three lines and the count is 3. The cat -n line shows that the last line is number 3.

Windows PowerShell

Windows PowerShell has its own commands. Select-String is the equivalent of grep and uses regular expressions [12]. The Unix commands above are the ones this course uses, so prefer WSL for coursework. Use PowerShell for Windows tasks.

Try this

  1. Make a folder under /tmp, create three files with printf and >, and combine them with cat.
  2. Use grep -i to find a word regardless of case.
  3. Pipe ls -l into grep "\.txt" to show only the text files.
  4. Read Reading documentation to practise man on three commands.

The MIT course The Missing Semester of Your CS Education covers the shell in depth and is free to watch [13].

Sources

  • [1] Microsoft Learn, "Install WSL". https://learn.microsoft.com/en-us/windows/wsl/install
  • [2] GNU Bash manual, "Bourne Shell Builtins" (cd, pwd). https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html
  • [3] ls(1), Linux man-pages. https://man7.org/linux/man-pages/man1/ls.1.html
  • [4] mkdir(1), Linux man-pages. https://man7.org/linux/man-pages/man1/mkdir.1.html
  • [5] mv(1), Linux man-pages. https://man7.org/linux/man-pages/man1/mv.1.html
  • [6] cat(1), Linux man-pages. https://man7.org/linux/man-pages/man1/cat.1.html
  • [7] grep(1), Linux man-pages. https://man7.org/linux/man-pages/man1/grep.1.html
  • [8] GNU Bash manual, "Redirections". https://www.gnu.org/software/bash/manual/html_node/Redirections.html
  • [9] GNU Bash manual, "Pipelines". https://www.gnu.org/software/bash/manual/html_node/Pipelines.html
  • [10] wc(1), Linux man-pages. https://man7.org/linux/man-pages/man1/wc.1.html
  • [11] man(1), Linux man-pages. https://man7.org/linux/man-pages/man1/man.1.html
  • [12] Microsoft Learn, Select-String. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string
  • [13] The Missing Semester of Your CS Education (MIT). https://missing.csail.mit.edu/