Latticework

Command Palette

Search for a command to run...

Bash / Shell

Shell Basics

14 min

Explanation

Every shell session has a current working directory — the folder commands run relative to — and a small set of commands you'll type more than any others: pwd (print it), cd (change it), ls (list what's in it), mkdir (make a directory), touch (create an empty file), cat (print a file's contents), echo (print text — and, combined with >, write it to a file), and rm (delete). Paths can be relative to where you are (notes/todo.txt) or absolute from the root (/home/user/notes/todo.txt).

mkdir notes
echo buy milk > notes/todo.txt
cat notes/todo.txt
# buy milk
Try it

ls without a path lists the current directory; ls logs lists inside a specific one. Try changing the echo text and running it again — the file's contents update to match exactly what you wrote.

Loading editor…
Exercise

Starting in your home directory, create a directory called `notes`, then inside it create a file `todo.txt` whose only line is `buy milk`.

Quiz

What does `echo buy milk > notes/todo.txt` do if `notes/todo.txt` already exists and contains other text?

Checkpoint

You can navigate directories and create, write, and read files using the core shell commands.