Foundations reference

Command Line Reference

A reference for navigating, searching and automating a Unix environment.

Files and directories

Creating and removing

mkdir and touch create, rm and rmdir remove. There is no recycle bin.

mkdir notes
mkdir -p a/b/c        # create missing parents
touch index.html
rm old.txt
rm -r old-folder      # directories need -r
rmdir empty-folder
  • Run ls on the path before any rm -r. If ls shows what you meant, rm will remove what you meant.

Copying and moving

cp duplicates, mv relocates. Renaming is moving to a new name in the same place.

cp file.txt copy.txt
cp -r src backup      # directories need -r
mv old.txt new.txt    # rename
mv file.txt archive/  # move

Reading and searching

Reading files

Four commands, differing mainly in how much they show you.

cat file.txt          # the whole thing
less file.txt         # scrollable, press q to leave
head -n 20 file.txt   # the first lines
tail -n 20 file.txt   # the last lines
tail -f app.log       # follow a live log
wc -l file.txt        # count lines

Searching

find matches on filenames. grep matches on file contents.

find . -name "*.js"       # quote the pattern
find . -type d
grep -rn "TODO" .         # recursive, with line numbers
grep -i "error" app.log   # ignore case
grep -v INFO app.log      # everything that does NOT match
grep -c ERROR app.log     # a count
  • Quote the find pattern, or the shell expands it before find ever sees it.

Redirection and pipes

Redirection

One arrow replaces the file, two appends to it.

echo hello > file.txt      # replaces
echo again >> file.txt     # appends
command 2> errors.txt      # only errors
command > out.txt 2>&1     # everything
command 2> /dev/null       # discard errors
  • A single arrow empties the file the moment the command starts, before it produces anything.

Pipes

Connect the output of one command to the input of the next. Nothing touches the disk.

cat app.log | grep ERROR | wc -l
ps aux | grep node
sort file.txt | uniq -c | sort -rn
  • sort before uniq, because uniq only collapses adjacent duplicates.

Permissions, environment and processes

Permissions

Ten characters: the kind, then owner, group and everyone else, each as read, write, execute.

ls -l
chmod +x script.sh    # make it runnable
chmod 755 script.sh   # owner all, others read and execute
chmod 644 file.txt    # owner writes, others read
  • A new file never has the execute bit, which is why ./script.sh says permission denied.

Environment and PATH

PATH is the ordered list of directories the shell searches for a command.

echo $HOME
echo $PATH
export PROJECT=archive
which node
  • command not found means it is not in any PATH directory.
  • The current directory is deliberately not on PATH, which is why local scripts need ./

Processes

Every running program has a numeric id.

ps aux
ps aux | grep node
kill 1234        # ask politely
kill -9 1234     # force, as a last resort
jobs
command &        # run in the background

Shell scripts

A file of commands you would otherwise have typed.

#!/bin/bash

if [ -z "$1" ]; then
  echo "Usage: scaffold.sh <name>"
  exit 1
fi

mkdir -p "$1"/src "$1"/docs
echo "Created $1"
  • Write it, chmod +x it, run it with ./name.sh. The middle step is the one people forget.

Want to learn how to build with this?

A reference tells you what exists. The course teaches you when to reach for it, and has you build something real while you learn.