DATASCI 350 - Data Science Computing

Lecture 03: Command Line Interface

Danilo Freire

Department of Data and Decision Sciences
Emory University

Recap and lecture overview 📚

Brief recap of last class

Early computing and data representation

  • Computers went from people to mechanical calculators to silicon chips
  • The Von Neumann architecture keeps instructions and data in one memory
  • Everything is stored in binary, base 2, using only 0s and 1s
  • One digit is a bit. Eight bits are a byte
  • Hexadecimal is base 16, and one hex digit covers exactly four bits
  • Abstraction is what lets numbers stand for images and text

Brief recap of last class

Representing images, colours and text

  • An image is a grid of pixels
  • RGB gives each pixel three numbers from 0 to 255: red, green, and blue
  • That is 8 bits per channel, so 16.7 million colours
  • Text is split into characters, and each character gets a number
  • ASCII is a lookup table for 128 characters
  • Unicode extends it to accents, other alphabets, and emoji

Brief recap of last class

Programming languages

  • Konrad Zuse built the first programmable computers in the 1940s
  • Assembly gives machine code readable names, one line per instruction
  • Low-level languages are fast, but hard to read and write
  • High-level languages like Python hide the hardware and run anywhere
  • Compiled languages translate first. Interpreted ones translate as they run

Today’s lecture

Command line: the old-school way of interacting with computers

  • The command line is a text interface to your computer
  • We will navigate the file system, and create, move, and delete files
  • We will write shell scripts to automate work you would rather not repeat
  • It is still everywhere: remote servers, cloud computing, and automation
  • Let’s get started! 🤓

Tweet of the day

Questions? 🤓

What is the command line? 💻

A computer in a nutshell

Operating system

Source: Dave Kerr
  • The operating system sits between your programs and the hardware, and manages access to it
  • It has two halves: the kernel and user space
  • The kernel talks to the hardware, runs the drivers, and hands out memory and CPU time
  • Code running in the kernel can break everything, which is why users are kept out of it
  • User space is where your programs run, each in its own sandbox, asking the kernel for whatever it needs
  • Curiosity: the Linux kernel source is on GitHub

A computer in a nutshell

Kernels and shells

Source: Dave Kerr

  • A shell is a user space program that lets you reach the system’s resources
  • Use it interactively by typing at a terminal, or run a script, a file full of commands
  • Most people use a graphical interface instead. Same idea, prettier surface
  • Why “kernel” and “shell”? A nut has a soft kernel protected by a hard shell. Useful metaphor, innit?

Interacting with the shell

Terminals

Source: Dave Kerr
  • You never talk to the shell directly. You talk to a terminal
  • A terminal reads your keystrokes, passes them to a program, and shows the results
  • A shell cannot do that on its own. It needs a terminal as its interface
  • Why “terminal”? Before screens, a terminal was hardware: a keyboard wired to a mainframe, often many terminals to one machine
  • Working on a data centre or cloud machine today is much the same, with a nicer screen 😉

Interacting with the shell

Command line

Source: Dave Kerr
  • A terminal is simply a text-based interface. The first thing it does is start a shell
  • The shell takes input two ways: interactively from the command line, or from a script
  • It sends output back to the terminal, or to a file
  • The command line is the line you type on. You can customise it with colours and shortcuts

Shell variants

Bash, Zsh, and others

  • There are many shells, and they differ in what they can do
  • bash is the default on most Unix-like systems. The name stands for Bourne-Again Shell
  • zsh is the default on macOS
  • Windows has cmd.exe and PowerShell. There is also C Shell, and many more
  • Your terminal starts your preferred shell automatically, and you can change which one

Why bother with the shell? 🤷🏻‍♂️

Why bother with the shell?

Why should you use this…

… instead of this?

Why bother with the shell?

The programmer’s best friend

  1. Speed. Typing beats hunting through menus with a mouse

  2. Power. Some things simply cannot be done from a graphical interface

  3. Reproducibility. A script can be run again. Clicking cannot

  4. Portability. The same commands work on your laptop, a Raspberry Pi, or a supercomputer

  5. Automation. Shells are programmable, so repetitive work becomes a script

  6. Employability. Servers, containers, and cloud tools all run on Linux, which is why the shell is having a renaissance

The Unix philosophy

The Unix philosophy

  • Our tools come from Unix, built at Bell Labs in the 1970s
  • They still follow the Unix philosophy:

Do One Thing And Do It Well

  • Each tool is small and does a single job
  • Chain the small tools together and you can build something large
  • Hence the description: minimalist and modular

Unix Version 5, 1974. The home directories ken and dmr belong to Ken Thompson and Dennis Ritchie

Things to use the shell for

  • Find and navigate files faster than any file browser
  • Rename and move files in bulk, hundreds at a time
  • Version control with Git, which is where most Git commands live
  • Install and update software with a single command
  • Connect to remote machines over SSH, including cloud servers
  • Run jobs on supercomputers, which often offer nothing but a shell

Shell basics 🐚 🤓

Shell: First look

Let’s open up our shell!

A convenient way to do this is through VS Code’s built-in Terminal.

Click on the View menu, then Terminal. You can also use the shortcut Ctrl + ` (backtick).

Your system default shell is loaded. To find out what that is, type echo $SHELL in the terminal.

$ echo $SHELL

/bin/zsh

It is Z shell in my case

… what about you? It is your turn to find out! 🤓

Your turn!

Of course, it’s always possible to open up the shell directly if you prefer. It’s your turn!

  • Linux

  • Mac

  • Windows: install WSL first, using our tutorial. It gives you the same shell as everyone else

Feel free to check our class tutorial on how to set up your shell in VS Code

Open your terminal and type the following commands (without the $):

$ echo $SHELL 
$ whoami 
$ pwd 
$ mkdir new-folder
$ rm -rf new-folder
$ cd ..
$ ls

Share your results with a colleague (or the class)!

Shell: First look

You should see something like:

$  username@hostname:~$

This is shell-speak for: “Who am I and where am I?”

  • username denotes a specific user (one of potentially many on this computer).

  • @hostname denotes the name of the computer or server.

  • :~ denotes the directory path (where ~ signifies the user’s home directory).

  • $ (or maybe %) denotes the start of the command prompt.

    • (For a special “superuser” called root, the dollar sign will change to a #).
$ whoami
$ pwd

dafreir
/Users/dafreir/Documents/github/datasci350/lectures/lecture-03

Syntax

Syntax

All bash commands have the same basic syntax:

command option(s) argument(s)

Examples:

$ # list files in the Documents directory 
$ # in long format and human-readable sizes
$
$ ls -lh ~/Documents 


$ # sort the file and display 
$ # only unique lines
$
$ sort -u file.txt 

Commands

  • You don’t always need options or arguments

  • For example:

    • ls ~/Documents/ and ls -lh ~/Documents are both valid commands that will yield (different) output
  • However, you always need a command!

Syntax

All Bash commands have the same basic syntax:

command option(s) argument(s)

Examples:

$ # list files in the Documents directory 
$ # in long format and human-readable sizes
$
$ ls -lh ~/Documents 


$ # sort the file and display 
$ # only unique lines
$
$ sort -u file.txt 

Options (also called flags)

  • Start with a dash, usually one letter
  • Chain several under a single dash
$ ls -l -a -h /var/log  # this works
$ ls -lah /var/log      # so does this
  • Longer, spelled-out names take two dashes
$ ls --human-readable /var/log
  • -l long listing, -a all files including hidden ones, -h readable sizes
  • -u on sort keeps only unique lines
  • Hard to remember what each letter means? You’re totally right!

Syntax

All Bash commands have the same basic syntax:

command option(s) argument(s)

Examples:

$ # list files in the Documents directory 
$ # with human-readable sizes
$ ls -lh ~/Documents/


$ # sort the file and remove duplicates
$ sort -u file.txt

Arguments

  • Tell the command what to operate on

  • What counts as a valid argument depends on the command

  • Can be a file, path, a set of files and folders, a string, and more

  • Sometimes more than just one argument is needed:

$ mv figs/cat.png best-figs/cat02.png

Help! 🆘 😟

Multiple ways to get help

  • man ls opens the manual page for ls. Press q to quit

  • Manual pages come in numbered sections. Run man man to see them

  • cht.sh gives short, practical answers straight in the terminal (you may need to install curl first):

$ curl cht.sh/ls

  • Or you can use an LLM (or Google) to search for help on a command
  • There’s a great chance that someone else has already asked the same question you have 😉

Multiple ways to get help

  • You can also install the tldr tool (GitHub repository) which provides simplified help pages for common commands. Run it like this: tldr command
$ tldr ls

Successfully updated local database

ls

List directory contents.
More information: <https://www.gnu.org/software/coreutils/manual/html_node/ls-invocation.html>.

- List files one per line:
    ls -1

- List all files, including hidden files:
    ls [-a|--all]

- List files with a trailing symbol to indicate file type (directory/, symbolic_link@, executable*, ...):
    ls [-F|--classify]

... (the manual continues; press q to quit)

  • For more info on how to get help, see here

Getting help with man

The man command (“manual pages”) is your friend if you need help

$ man ls

LS(1)               General Commands Manual          LS(1)

NAME
     ls – list directory contents

SYNOPSIS
     ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when]
    [-D format] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls
     displays its name as well as any requested, associated information.  For
     each operand that names a file of type directory, ls displays the names
     of files contained within that directory, as well as any requested,
     associated information.

     If no operands are given, the contents of the current directory are
     displayed.  If more than one operand is given, non-directory operands are

... (the manual continues; press q to quit)

Getting help with man

man shows the page in a pager, a small viewer for long text. The keys you need:

  • j and k, or the arrow keys, move one line
  • d and u move half a page, space and b a whole one
  • g and G jump to the very start and the very end
  • /pattern searches forwards, ?pattern backwards. Then n and N for the next and previous hit
  • q quits
  • h lists every other key

RTFM! 😂

Always check the documentation!

man page explorer challenge

Partner up and choose a command from the list below. Use man to complete these tasks:

$ # Choose one (or all) of the following commands:
$ ls, cd, cp, mv, rm, mkdir, rmdir, touch, cat, find, head, wc
  1. Summarise the command’s purpose in one sentence
  2. Find an interesting option and explain what it does
  3. Create an example using your command with at least two options
  4. Bonus: Combine your command with your partner’s in a single line

You have about 5-10 minutes. Be ready to share your findings!

Reflection: How was using man compared to online searches? How might you use it in future projects?

More navigation commands: A cheat sheet

Getting around

Command What it does
pwd Print the working directory
cd dir Change directory
cd - Back to where you just were
ls List files here
ls -l Long format, with details
ls -a Include hidden files
ls -lh Long format, readable sizes
ls -R Include subdirectories
~ . .. Home, here, one level up

Working with files

Command What it does
mkdir Create a directory
touch Create an empty file
cp Copy
mv Move, or rename
rm Delete a file. No undo!
rmdir Delete an empty directory
cat Show what is in a file
find Search for files

A fuller list is here

Shell navigation exercise

Type each command. Look at the result before you move on

  1. Go to your home directory: cd ~
  2. Create a directory and enter it: mkdir practice && cd practice
  3. Create two empty files: touch file1.txt file2.txt
  4. List the contents: ls
  5. Rename file2.txt to file3.txt: mv file2.txt file3.txt
  6. List again to check, then go home: ls && cd ~
  7. Warning: rm -rf deletes at once, with no Trash and no undo. Read the path, then delete the directory: rm -rf practice
  8. Check that it is gone: ls

Shell navigation exercise to try at home

  1. Go to your home directory: cd ~
  2. Create a directory: mkdir shell-practice
  3. Enter it: cd shell-practice
  4. Create three empty files: touch file1.txt file2.txt file3.txt
  5. List the contents: ls
  6. Create a subdirectory: mkdir subdir
  1. Move a file into it: mv file2.txt subdir/
  2. Copy a file: cp file1.txt file4.txt
  3. List everything, subdirectory included: ls -R
  4. Go up one level: cd ..
  5. Warning: the next command deletes the folder and everything inside it. Check the name, then run rm -r shell-practice
  6. Check that it is gone: ls

Bonus: use man to read about any command you used.

  • Which commands surprised you?
  • Which did you find most useful?

Summary

Today we…

  • The shell runs your commands. The terminal is the window you type them in
  • Every command has the same shape: command [options] arguments
  • Options are short (-l) or spelled out (--human-readable), and they combine: ls -lah
  • pwd says where you are, ls shows what is there, cd moves you
  • ~ is home, . is here, .. is one level up
  • man, tldr, and cht.sh answer your questions. q gets you out of the pager
  • The Unix philosophy: small tools that each do one thing, combined to do big things

Next class

  • Managing files: create, copy, move, and delete them from the terminal
  • Wildcards and find, so you can act on many files at once
  • Working with text: cat, head, grep, and sed
  • Pipes and redirects, chaining small tools into bigger ones. The Unix philosophy in action
  • Loops and shell scripts, to automate what you would otherwise repeat
  • Bring your laptop. We will be typing 🤓

Questions? 😉

Thank you very much and see you soon! 😊 🙏🏼