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:
Each part means:
username: Current username
@: Separator
hostname: Computer name
:: Separator
~: Current directory (~ represents home directory)
$: Regular user (# represents root user)
Prompt Examples
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
command [options] [arguments]
Examples
ls # Command: List files
ls -l # Command + option: Detailed listing
ls -la /home # Command + options + argument: List all files in /home
# 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
ls - List Files
# 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
# 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
. # 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
$ clear
# Or press Ctrl + L
echo - Output Text
$ echo "Hello, World!"
Hello, World!
$ echo $HOME
/home/maxwell
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
whoami - Show Current User
date - Show Date and Time
$ 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
$ 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
$ 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
# View specific section
$ man 5 passwd # View passwd file format
$ man 1 passwd # View passwd command
--help Option
$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs...
info - Detailed Documentation
whatis - Short Description
$ whatis ls
ls (1) - list directory contents
apropos - Search for Commands
$ apropos copy
cp (1) - copy files and directories
scp (1) - secure copy (remote file copy program)
Command Line Shortcuts
Cursor Movement
Editing
Command History
Control
Tab Completion
Tab completion is an important feature for improving efficiency:
# 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
# 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
# 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
Reverse Search
Press Ctrl + R, then type a keyword:
(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
# Separate with semicolons
$ mkdir test; cd test; pwd
/home/maxwell/test
Conditional Execution
# && - 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
$ mkdir test && cd test || echo "Failed"
Aliases
Creating Aliases
# Temporary alias
$ alias ll='ls -la'
$ ll
# View all aliases
$ alias
# Delete alias
$ unalias ll
Permanent Aliases
Edit ~/.bashrc:
# 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:
$ source ~/.bashrc
# Or
$ . ~/.bashrc
Wildcards
Basic Wildcards
Examples
# 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
# 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