Command line: reference

Terminal and Bash: files, search, pipelines and first scripts.

Navigation

Where am I and what is here

~ is the user's home folder, . is the current one, .. is the parent.

pwd              # current folder
ls               # list of files
ls -la           # with details and hidden files
cd projects      # enter a folder
cd ..            # one level up
cd ~             # to the home folder

Files and folders

Creating, copying, deleting

Deleting with rm is irreversible: there is no recycle bin in the terminal. Double-check the path.

mkdir app                 # create a folder
mkdir -p a/b/c            # together with nested ones
touch notes.txt           # create an empty file
cp a.txt b.txt            # copy
mv b.txt docs/            # move or rename
rm notes.txt              # delete a file
rm -r old/                # delete a folder

Viewing contents

cat, head, tail, less

tail -f is handy for watching logs in real time.

cat file.txt            # the whole file
head -n 5 file.txt      # first 5 lines
tail -n 5 file.txt      # last 5 lines
tail -f app.log         # follow new lines
less file.txt           # scroll (quit: q)
wc -l file.txt          # number of lines

Search

grep and find

grep searches for text inside files, and find searches for the files and folders themselves.

grep "error" app.log          # lines with the text
grep -i -n "error" app.log    # case-insensitive, with numbers
grep -r "TODO" src/           # in all files of the folder
find . -name "*.txt"          # search files by name
find . -type d -name "node_modules"

Streams and pipelines

|, >, >>

The pipeline | passes the output of one command to the input of the next.

ls | wc -l                    # number of files
cat log.txt | grep error | sort | uniq -c
echo "hello" > out.txt       # write (overwrites)
echo "another line" >> out.txt    # append to the end
command 2> errors.txt         # errors to a file

Variables

Working with variables

There are no spaces around =. The command in $(...) is executed and its output is substituted.

name="Alex"
echo "Hello, $name"
echo "Files: $(ls | wc -l)"
export APP_ENV=production    # available to child programs
echo $HOME $PATH

Scripts

The first script

Run it: chmod +x hello.sh, then ./hello.sh Alex. The first argument is available as $1.

#!/bin/bash
# file hello.sh
name="$1"
if [ -z "$name" ]; then
  echo "Provide a name"
  exit 1
fi
echo "Hello, $name!"

Conditions and loops

Spaces are required inside [ ]. Flags: -f file, -d folder, -z empty string, -lt less than.

if [ -f "config.txt" ]; then
  echo "it is a file"
elif [ -d "config" ]; then
  echo "it is a folder"
else
  echo "nothing there"
fi

for f in *.txt; do
  echo "File: $f"
done

n=0
while [ $n -lt 3 ]; do
  n=$((n + 1))
done

Functions

A function's arguments are also available as $1, $2 and so on.

greet() {
  echo "Hello, $1"
}
greet "Alex"

Permissions

chmod and owner

The digits in chmod: 4 read, 2 write, 1 execute (they are added up).

ls -l                  # -rwxr-xr--  owner group others
chmod +x script.sh     # allow execution
chmod 644 file.txt     # rw-r--r--
sudo command           # run as administrator

Useful things

Help and history

The Tab key completes file and command names.

man ls              # manual (quit: q)
ls --help
history             # previously entered commands
clear               # clear the screen
Ctrl+C              # stop the command
Ctrl+R              # search the history

Command substitution

The backtick form does the same as $(...), but $(...) is more modern.

today=`date +%F`
echo "Today: $today"
curl -s https://example.com | head -n 3

Learn Command line with practice

Want to learn step by step? Your account has 110 lessons organised by chapter (80 core and 30 advanced, you can switch the level at any time) and tasks with code checking.