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.
Related Concepts
┌─────────────────────────────────────────────────────────────┐
│ │
│ 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 + Tor search for "Terminal" - KDE: Press
Ctrl + Alt + Tor 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:
username@hostname:~$Each part means:
username: Current username@: Separatorhostname: 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 directoryBasic Command Format
command [options] [arguments]Examples
ls # Command: List files
ls -l # Command + option: Detailed listing
ls -la /home # Command + options + argument: List all files in /homeOption Formats
# 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.txtFirst Set of Commands
pwd - Print Working Directory
$ pwd
/home/maxwellls - 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 -lahcd - 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 ../etcPath 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/Documentsclear - Clear Screen
$ clear
# Or press Ctrl + Lecho - Output Text
$ echo "Hello, World!"
Hello, World!
$ echo $HOME
/home/maxwell
$ echo $PATH
/usr/local/bin:/usr/bin:/binwhoami - Show Current User
$ whoami
maxwelldate - 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:00cal - 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 31Getting 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 pageb/Page Up: Previous page/pattern: Searchn: Next search resultq: Quit
Manual Sections
| Section | Content |
|---|---|
| 1 | User commands |
| 2 | System calls |
| 3 | Library functions |
| 4 | Special files |
| 5 | File formats |
| 6 | Games |
| 7 | Miscellaneous |
| 8 | System administration commands |
# 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
$ info lswhatis - Short Description
$ whatis ls
ls (1) - list directory contentsapropos - Search for Commands
$ apropos copy
cp (1) - copy files and directories
scp (1) - secure copy (remote file copy program)Command Line Shortcuts
Cursor Movement
| Shortcut | Function |
|---|---|
Ctrl + A | Move to beginning of line |
Ctrl + E | Move to end of line |
Ctrl + B | Move back one character |
Ctrl + F | Move forward one character |
Alt + B | Move back one word |
Alt + F | Move forward one word |
Editing
| Shortcut | Function |
|---|---|
Ctrl + U | Delete to beginning of line |
Ctrl + K | Delete to end of line |
Ctrl + W | Delete previous word |
Alt + D | Delete next word |
Ctrl + Y | Paste deleted content |
Ctrl + _ | Undo |
Command History
| Shortcut | Function |
|---|---|
↑ / Ctrl + P | Previous command |
↓ / Ctrl + N | Next command |
Ctrl + R | Search command history |
!! | Execute previous command |
!n | Execute nth history command |
!string | Execute most recent command starting with string |
Control
| Shortcut | Function |
|---|---|
Ctrl + C | Interrupt current command |
Ctrl + Z | Pause current command |
Ctrl + D | Exit terminal (or input EOF) |
Ctrl + L | Clear screen |
Tab | Auto-complete |
Tab Tab | Show all completion options |
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 -cUsing 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 DocumentsReverse Search
Press Ctrl + R, then type a keyword:
(reverse-i-search)`ls': ls -la /homePress 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/testConditional 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 mkdirCombination
$ mkdir test && cd test || echo "Failed"Aliases
Creating Aliases
# Temporary alias
$ alias ll='ls -la'
$ ll
# View all aliases
$ alias
# Delete alias
$ unalias llPermanent 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
$ . ~/.bashrcWildcards
Basic Wildcards
| Wildcard | Meaning | Example |
|---|---|---|
* | Match any character (0 or more) | *.txt |
? | Match single character | file?.txt |
[abc] | Match any character in brackets | file[123].txt |
[a-z] | Match character range | file[a-z].txt |
[!abc] | Not match characters in brackets | file[!0-9].txt |
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.txtBrace 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/docSummary
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