Lesson 3: Shell Navigation

Homepage Content Slides Video

Warning

This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.

Overview

The Shell

A shell is a text-based user-interface for a computer.
sea shell

Shell Examples

sh
Required by all POSIX Operating Systems.
bash
Default on most GNU/Linux-based Operating Systems.
csh
Default shell on most BSD (Unix) based Operating Systems
fish
Useful but not sh compliant shell.
zsh
The hip new shell on the block.

Basic Shell Commands

$ pwd    # Prints the current working directory (where you are)
$ ls     # Prints the contents of the current working directory
$ cd <path/to/other/directory>   # Navigates to a new directory.
$ echo "some thing $AND_VARS"    # Prints a string to the screen.
$ cat  foo.txt bax.txt # Prints the contents of a file(s) to the screen.
$ grep foo file.txt    # Searches `file.txt` for the string `foo`
$ less  file.txt       # Prints a file to the screen so you can arrow up/down.
$ env    # Prints environment variables to the screen.
$ whoami # Prints out current user
$ help   # When in doubt, always type help.

Shell Scripts

about_me.sh

#!/bin/sh
if [ $(whoami) == "root" ]; then
  echo "You're root!"
else
  echo "Your username is $(whoami)"
  echo "Your home-directory is $HOME"
  echo "Your current directory is $PWD"
  echo "Your computer's host-name is $HOSTNAME"
fi

Invoke with:

$ chmod +x about_me.sh  # Tell Linux that this can be run as a program.
$ ./about_me.sh         # Invoke the script.

File Paths

.
The current directory.
..
The parent directory.
~
Alias for your home directory.
/

Separates directories: one_dir/another_dir/last_dir

Alone, or at the start of a path, it is the root directory.

File Paths

$ tree -F
.
|-- bar/
|   |-- one
|   `-- two
|-- baz/
`-- foo/
    `-- a/
        `-- b/

5 directories, 2 files

Special Characters

Wildcard (*)

Used as a stand-in for any character(s).

Example: cat *.log cats all files in the current working directory ending in .log.

End of line ($)
Used to specify the end of a regex. We'll cover what regex is later.
Curl braces ({ })

Used to specify a set.

Example: ls {foo,bar,baz}ley-thing expands to ls fooley-thing barley-thing bazley-thing

Escape special characters (treat them as normal characters) with the escape character (\).

Type Less, Tab More

Pressing the tab key auto-completes a command, file-path, or argument in your shell.

Pressing tab multiple times completes the command to the best of the shells ability and then lists the possible completions (if there are any).

$ ls b    # <tab>
$ ls ba   # <tab>
bar_thing/ baz_thing/
$ ls bar  # <tab>
$ ls bar_thing

TODO

Further Reading

BASH Programming - Introduction HOW-TO
A free online resoruce of learning bash programming. Covers some concepts we'll get to later in DOBC, but a good resoruce to have on hand.
Running rm -rf / on Linux
This video demonstrates what happens when you 'delete your hard-drive' on Linux. A fun watch!