Skip to content

CLI Basics

The command line interface (CLI) provides the following advantages over a GUI:

  • No GUI overhead - runs well on headless servers with minimal resources
  • Virtually every task can be accomplished from the terminal
  • Scripts automate often-used (or easy-to-forget) procedures
  • Sign into remote machines securely over any network
  • Graphical applications can be launched directly from the CLI
  • CLI interface is consistent across all Linux distributions - GUI tools vary

A terminal emulator simulates a standalone terminal within a window on a graphical desktop. Common choices:

EmulatorNotes
gnome-terminalDefault on GNOME
konsoleDefault on KDE
alacrittyGPU-accelerated, fast
kittyFeature-rich, GPU-accelerated
terminatorSplit-pane support
xtermLightweight, minimal
ghosttyNew, very fast

Virtual Terminals

Virtual Terminals (VTs) are console sessions that use the entire display and keyboard outside of a graphical environment. There can be multiple active virtual terminals, but only one is visible at a time - unlike graphical terminal windows, which can all be open simultaneously.

  • One VT is reserved for the graphical environment (VT7 on Ubuntu, VT1 on RHEL/openSUSE)
  • Switch between VTs: CTRL-ALT-F1 through CTRL-ALT-F6 (or ALT-Fn if already in a VT)
  • VTs are invaluable when the graphical desktop freezes - switch to a text VT to investigate and recover
Terminal window
sudo systemctl stop gdm # stop GNOME (or lightdm on Ubuntu <18.04)
sudo telinit 3 # drop to multi-user text mode
sudo systemctl start gdm # restart GUI
sudo telinit 5 # return to graphical mode

Most input lines have 3 basic elements:

command [options] [arguments]
  • Command - the program to execute (e.g., ls, grep, find)
  • Options - modify behavior; start with - (short) or -- (long): -l, --all
  • Arguments - what the command operates on: a filename, a path, a search pattern

Many commands have no options, no arguments, or neither. The order matters.


sudo (SuperUser DO) lets you run commands with the security privileges of another user - usually root. It’s the preferred way to administer a Linux system without logging in as root.

Terminal window
# 1. Switch to root using su (since sudo isn't set up yet)
su
Password:
#
# 2. Create a sudoers drop-in file for your user
echo "student ALL=(ALL) ALL" > /etc/sudoers.d/student
# 3. Fix permissions (required by some distributions)
chmod 440 /etc/sudoers.d/student
Terminal window
sudo dnf update # run as root
sudo -i # open a root shell
sudo -u postgres psql # run as a different user (postgres)
sudo !! # re-run last command with sudo

sudo prompts for your own password (not root’s). After entry, it’s cached for a configurable window (default: 5–15 minutes depending on distro).


Basic CLI commands

When logging into a text terminal, you’ll see a login: prompt. Your password is never displayed (not even asterisks) to prevent shoulder-surfing.

Always shut down properly - abrupt power cuts can corrupt filesystems.

Terminal window
sudo shutdown -h now # halt immediately
sudo shutdown -h 10:00 "Scheduled maintenance" # notify and halt at 10:00
sudo shutdown -r now # reboot immediately
sudo reboot # equivalent to shutdown -r now
sudo poweroff # equivalent to shutdown -h now
sudo halt # similar to poweroff

man is the most-used Linux documentation tool. It displays formatted reference pages that ship with most programs.

Terminal window
man ls # manual for ls
man 8 useradd # section 8 (admin commands) for useradd
man -f ls # which sections have a 'ls' page (= whatis ls)
man -k sysctl # find all pages mentioning sysctl (= apropos sysctl)
man -w ls # show the file path of the man page

Man Page Sections

Man pages are divided into sections, each containing a different type of information:

SectionContents
1Executable programs / shell commands
2System calls (kernel functions)
3Library calls
4Special files (/dev)
5File formats and conventions (/etc/passwd)
6Games
7Miscellaneous (overview, conventions)
8System administration commands (usually root)
9Kernel routines
Terminal window
man -f intro
# intro (1) - introduction to user commands
# intro (2) - introduction to system calls
# ...
man 1 intro # user commands intro
man 8 intro # admin commands intro
Terminal window
whatis ls # same as man -f ls - one-line description
apropos sysctl # same as man -k - search all man pages for keyword

The GNU Info system is the GNU project’s preferred alternative to man. It uses hyperlinked sections (nodes) - like documentation structured for browsing rather than reference pages.

Terminal window
info coreutils # browse the coreutils info document
info ls # info page for ls (often more detailed than man)

Inside info:

KeyAction
nNext node
pPrevious node
uUp one level
EnterFollow link
qQuit

Nodes (sections) may contain menus and linked subtopics. Items outside a menu are marked with :: at the end of their name.


The PS1 variable controls the shell prompt string.

Terminal window
echo $PS1 # see current prompt
PS1="\u@\h \$ " # user@hostname $ (use backslash escapes)

Common PS1 escape sequences:

SequenceOutput
\uCurrent username
\hHostname (short)
\HFull hostname
\wCurrent working directory
\WBasename of current directory
\$# if root, $ otherwise
\tCurrent time (HH:MM:SS)

To make permanent, add to ~/.bashrc:

Terminal window
export PS1='\u@\h:\w\$ '

Standard File Streams (stdin / stdout / stderr)

Section titled “Standard File Streams (stdin / stdout / stderr)”

Every process starts with three open file streams:

Standard file streams

NameSymbolFile DescriptorDefault
Standard inputstdin0Keyboard
Standard outputstdout1Terminal
Standard errorstderr2Terminal

The file descriptor number is how the shell references these streams when redirecting. Any additional files opened by the process start at fd 3.

Terminal window
command < input-file # redirect stdin from file
command > output-file # redirect stdout to file (overwrite)
command >> output-file # redirect stdout to file (append)
command 2> error-file # redirect stderr to file
command > allout 2>&1 # redirect both stdout and stderr to file
command >& allout # bash shorthand for the above
command > /dev/null 2>&1 # suppress all output (stdout and stderr)

The UNIX/Linux philosophy is to have many simple, short programs that cooperate to produce complex results - rather than one complex program with many modes. The | (pipe) operator is the glue:

Terminal window
command1 | command2 | command3

This is a pipeline. Key properties:

  • command2 and command3 start operating on data before command1 finishes - no waiting, no temporary files
  • On multi-core systems, pipeline stages run concurrently - better CPU utilization
  • No disk I/O between stages - disk is almost always the slowest bottleneck
Terminal window
# Practical examples
ps aux | grep nginx # find nginx processes
cat /var/log/syslog | grep ERROR | tail -20 # last 20 errors
ls -lSh /usr | head -10 # 10 largest files in /usr
du -sh /var/* | sort -rh | head -5 # top 5 largest dirs in /var