Skip to content

Terminal Basics

What is a Terminal?

The Terminal is a text interface for interacting with the Linux system. Through the terminal, you can input commands to perform various operations, from simple file browsing to complex system management tasks.

┌─────────────────────────────────────────────────────────────┐
│                                                             │
│  Terminal Emulator                                          │
│  ┌───────────────────────────────────────────────────────┐  │
│  │                                                       │  │
│  │  Shell (e.g., Bash)                                 │  │
│  │  ┌─────────────────────────────────────────────────┐  │  │
│  │  │                                                 │  │  │
│  │  │  $ ls -la                    ← Command line    │  │  │
│  │  │  total 24                                       │  │  │
│  │  │  drwxr-xr-x 3 user user 4096 Jan  1 10:00 .    │  │  │
│  │  │  -rw-r--r-- 1 user user  123 Jan  1 10:00 file │  │  │
│  │  │                                                 │  │  │
│  │  └─────────────────────────────────────────────────┘  │  │
│  │                                                       │  │
│  └───────────────────────────────────────────────────────┘  │
│                                                             │
└─────────────────────────────────────────────────────────────┘
  • Terminal Emulator: Terminal program running in a graphical interface (e.g., GNOME Terminal, Konsole)
  • Shell: Command interpreter that receives commands and executes them (e.g., Bash, Zsh)
  • Command Line: Interface for entering commands
  • Console: Physical terminal, usually refers to TTY

Opening Terminal

Graphical Interface

  • GNOME: Press Ctrl + Alt + T or search for "Terminal"
  • KDE: Press Ctrl + Alt + T or search for "Konsole"
  • Xfce: Menu → Accessories → Terminal

Virtual Console (TTY)

Press Ctrl + Alt + F1 to F6 to switch to a virtual console, Ctrl + Alt + F7 or F2 to return to the graphical interface.

Command Prompt

When you open the terminal, you'll see a command prompt:

bash
username@hostname:~$

Each part means:

  • username: Current username
  • @: Separator
  • hostname: Computer name
  • :: Separator
  • ~: Current directory (~ represents home directory)
  • $: Regular user (# represents root user)

Prompt Examples

bash
maxwell@ubuntu:~$           # Regular user in home directory
maxwell@ubuntu:/etc$        # Regular user in /etc directory
root@ubuntu:/var/log#       # Root user in /var/log directory

Basic Command Format

bash
command [options] [arguments]

Examples

bash
ls                    # Command: List files
ls -l                 # Command + option: Detailed listing
ls -la /home          # Command + options + argument: List all files in /home

Option Formats

bash
# Short options (single letters, starting with -)
ls -l
ls -a
ls -la              # Multiple short options can be combined

# Long options (words, starting with --)
ls --all
ls --human-readable

# Options with values
head -n 10 file.txt
head --lines=10 file.txt

First Set of Commands

pwd - Print Working Directory

bash
$ pwd
/home/maxwell

ls - List Files

bash
# Basic use
$ ls
Desktop  Documents  Downloads  Pictures

# Detailed information
$ ls -l
drwxr-xr-x 2 maxwell maxwell 4096 Jan  1 10:00 Desktop
drwxr-xr-x 2 maxwell maxwell 4096 Jan  1 10:00 Documents

# Show hidden files
$ ls -a
.  ..  .bashrc  .profile  Desktop  Documents

# Human-readable file sizes
$ ls -lh
-rw-r--r-- 1 maxwell maxwell 1.5K Jan  1 10:00 file.txt

# Common combinations
$ ls -la
$ ls -lah

cd - Change Directory

bash
# Enter directory
$ cd Documents
$ pwd
/home/maxwell/Documents

# Go up one directory
$ cd ..

# Go to home directory
$ cd
$ cd ~

# Go to last directory
$ cd -

# Use absolute path
$ cd /var/log

# Use relative path
$ cd ../etc

Path Description

bash
.           # Current directory
..          # Parent directory
~           # Home directory
/           # Root directory

# Absolute path: starts from root directory
/home/maxwell/Documents

# Relative path: starts from current directory
Documents
./Documents
../maxwell/Documents

clear - Clear Screen

bash
$ clear
# Or press Ctrl + L

echo - Output Text

bash
$ echo "Hello, World!"
Hello, World!

$ echo $HOME
/home/maxwell

$ echo $PATH
/usr/local/bin:/usr/bin:/bin

whoami - Show Current User

bash
$ whoami
maxwell

date - Show Date and Time

bash
$ date
Sat Jan  1 10:00:00 CST 2025

$ date +"%Y-%m-%d"
2025-01-01

$ date +"%H:%M:%S"
10:00:00

cal - Display Calendar

bash
$ cal
    January 2025
Su Mo Tu We Th Fr Sa
          1  2  3  4
  5  6  7  8  9 10 11
 12 13 14 15 16 17 18
 19 20 21 22 23 24 25
 26 27 28 29 30 31

Getting Help

man - Manual Pages

bash
$ man ls

LS(1)                   User Commands                   LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List information about the FILEs...

man Manual Navigation

  • Space/Page Down: Next page
  • b/Page Up: Previous page
  • /pattern: Search
  • n: Next search result
  • q: Quit

Manual Sections

SectionContent
1User commands
2System calls
3Library functions
4Special files
5File formats
6Games
7Miscellaneous
8System administration commands
bash
# View specific section
$ man 5 passwd    # View passwd file format
$ man 1 passwd    # View passwd command

--help Option

bash
$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs...

info - Detailed Documentation

bash
$ info ls

whatis - Short Description

bash
$ whatis ls
ls (1)               - list directory contents

apropos - Search for Commands

bash
$ apropos copy
cp (1)               - copy files and directories
scp (1)              - secure copy (remote file copy program)

Command Line Shortcuts

Cursor Movement

ShortcutFunction
Ctrl + AMove to beginning of line
Ctrl + EMove to end of line
Ctrl + BMove back one character
Ctrl + FMove forward one character
Alt + BMove back one word
Alt + FMove forward one word

Editing

ShortcutFunction
Ctrl + UDelete to beginning of line
Ctrl + KDelete to end of line
Ctrl + WDelete previous word
Alt + DDelete next word
Ctrl + YPaste deleted content
Ctrl + _Undo

Command History

ShortcutFunction
/ Ctrl + PPrevious command
/ Ctrl + NNext command
Ctrl + RSearch command history
!!Execute previous command
!nExecute nth history command
!stringExecute most recent command starting with string

Control

ShortcutFunction
Ctrl + CInterrupt current command
Ctrl + ZPause current command
Ctrl + DExit terminal (or input EOF)
Ctrl + LClear screen
TabAuto-complete
Tab TabShow all completion options

Tab Completion

Tab completion is an important feature for improving efficiency:

bash
# Complete command
$ sys<Tab>
$ systemctl

# Complete filename
$ cd Doc<Tab>
$ cd Documents/

# Complete path
$ ls /u<Tab>sr/<Tab>lo<Tab>cal/
$ ls /usr/local/

# Show multiple options
$ cd D<Tab><Tab>
Desktop/    Documents/  Downloads/

Command History

history Command

bash
# Show history
$ history
    1  ls
    2  cd Documents
    3  pwd
    ...

# Show last 10 commands
$ history 10

# Search history
$ history | grep ls

# Clear history
$ history -c

Using History Commands

bash
# Execute previous command
$ !!

# Execute 42nd command
$ !42

# Execute most recent command starting with ls
$ !ls

# Use arguments from previous command
$ ls Documents
$ cd !$           # cd Documents

Press Ctrl + R, then type a keyword:

bash
(reverse-i-search)`ls': ls -la /home

Press Ctrl + R to continue searching, press Enter to execute, press Esc or Ctrl + C to cancel.

Multiple Commands

Sequential Execution

bash
# Separate with semicolons
$ mkdir test; cd test; pwd
/home/maxwell/test

Conditional Execution

bash
# && - Execute next only if previous succeeded
$ mkdir test && cd test
# If mkdir succeeds, then execute cd

# || - Execute next only if previous failed
$ cd test || mkdir test
# If cd fails, then execute mkdir

Combination

bash
$ mkdir test && cd test || echo "Failed"

Aliases

Creating Aliases

bash
# Temporary alias
$ alias ll='ls -la'
$ ll

# View all aliases
$ alias

# Delete alias
$ unalias ll

Permanent Aliases

Edit ~/.bashrc:

bash
# Add aliases
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias update='sudo apt update && sudo apt upgrade'

Make changes effective:

bash
$ source ~/.bashrc
# Or
$ . ~/.bashrc

Wildcards

Basic Wildcards

WildcardMeaningExample
*Match any character (0 or more)*.txt
?Match single characterfile?.txt
[abc]Match any character in bracketsfile[123].txt
[a-z]Match character rangefile[a-z].txt
[!abc]Not match characters in bracketsfile[!0-9].txt

Examples

bash
# All .txt files
$ ls *.txt
file1.txt  file2.txt  notes.txt

# Single character match
$ ls file?.txt
file1.txt  file2.txt

# Character range
$ ls file[1-3].txt
file1.txt  file2.txt  file3.txt

# Exclusion
$ ls file[!1].txt
file2.txt  file3.txt

Brace Expansion

bash
# Create multiple files
$ touch file{1,2,3}.txt
# Equivalent to touch file1.txt file2.txt file3.txt

# Range expansion
$ echo {1..5}
1 2 3 4 5

$ echo {a..e}
a b c d e

# Combination
$ mkdir project/{src,bin,doc}
# Creates project/src, project/bin, project/doc

Summary

This chapter introduced basic terminal usage:

  • Terminal and Shell concepts
  • Basic commands: pwd, ls, cd, clear, echo
  • Getting help: man, --help, info
  • Shortcuts: Improve operation efficiency
  • Command history: Reuse previous commands
  • Wildcards: Batch file operations

After mastering these basics, you can start working efficiently in the terminal. Next, we'll learn about the Linux filesystem structure.


Previous chapter: Desktop Environment

Next chapter: File System Structure

Content is for learning and research only.