Opening the Terminal
The terminal (also called a shell, console, or command line) is a text-based interface where you type commands for the operating system to execute. On most Linux desktops, you can open a terminal with the keyboard shortcut Ctrl + Alt + T, or by searching for "Terminal" in your application menu.
When you open the terminal, you will see a prompt that typically looks like this:
username@hostname:~$
This tells you your username, the computer's hostname, the current directory (~ means your home directory), and the privilege level ($ for normal user, # for root).
Navigation Commands
Navigating the file system is the most fundamental skill. These commands let you move around and see what is where:
| Command | Description | Example |
|---|---|---|
pwd | Print working directory — shows your current location | pwd → /home/user |
ls | List files and directories in the current location | ls -la |
cd | Change directory — move to a different location | cd /var/log |
cd .. | Move up one directory level | cd .. |
cd ~ | Go to your home directory from anywhere | cd ~ |
cd - | Go back to the previous directory | cd - |
Understanding ls Options
The ls command becomes much more powerful with flags:
# Long format with details (permissions, owner, size, date)
ls -l
# Show hidden files (those starting with a dot)
ls -a
# Combine both: long format + hidden files
ls -la
# Human-readable file sizes (KB, MB, GB)
ls -lh
# Sort by modification time (newest first)
ls -lt
# Recursive listing (include subdirectories)
ls -R
File Management
These commands let you create, copy, move, rename, and delete files and directories:
| Command | Description | Example |
|---|---|---|
touch | Create an empty file or update timestamp | touch newfile.txt |
mkdir | Create a new directory | mkdir projects |
mkdir -p | Create nested directories | mkdir -p a/b/c |
cp | Copy files or directories | cp file.txt backup.txt |
cp -r | Copy directories recursively | cp -r dir1 dir2 |
mv | Move or rename files/directories | mv old.txt new.txt |
rm | Remove (delete) files | rm unwanted.txt |
rm -r | Remove directories recursively | rm -r old_folder |
rmdir | Remove empty directories | rmdir empty_dir |
Be Careful with rm
The rm command permanently deletes files — there is no recycle bin in the terminal. Always double-check before using rm -rf. Consider using rm -i (interactive mode) which asks for confirmation before each deletion, especially when learning.
Viewing and Editing Files
Reading file contents is something you will do constantly on Linux:
# Display entire file contents
cat filename.txt
# View file with pagination (scroll with Space, quit with q)
less filename.txt
# Display first 10 lines (or specify a number)
head filename.txt
head -20 filename.txt
# Display last 10 lines
tail filename.txt
# Follow a file in real-time (great for logs)
tail -f /var/log/syslog
# Count lines, words, and characters
wc filename.txt
wc -l filename.txt # lines only
Text Editors in the Terminal
Linux offers several terminal-based text editors:
- nano — Beginner-friendly editor with on-screen shortcut hints. Open with
nano filename.txt. Save withCtrl+O, exit withCtrl+X. - vim — Powerful modal editor with a steep learning curve. Highly efficient once mastered. Open with
vim filename.txt. Pressito enter insert mode,Escthen:wqto save and quit. - vi — The classic Unix editor, available on virtually every system. Similar to vim but with fewer features.
Searching and Finding
Quickly locating files and content is essential for efficient system management:
# Find files by name
find /home -name "*.txt"
# Find files modified in the last 7 days
find /var/log -mtime -7
# Find files larger than 100MB
find / -size +100M -type f
# Search for text inside files
grep "error" /var/log/syslog
# Recursive search through directories
grep -r "function" /home/user/project/
# Case-insensitive search
grep -i "warning" logfile.txt
# Show line numbers with matches
grep -n "TODO" *.py
Pipes and Redirection
Pipes and redirection are fundamental concepts that let you chain commands and control input/output:
Pipes ( | )
Pipes send the output of one command as input to another:
# Count number of files in a directory
ls -1 | wc -l
# Find a specific process
ps aux | grep nginx
# Sort and display unique lines
cat names.txt | sort | uniq
# View long output with pagination
dmesg | less
Redirection
# Write output to a file (overwrites)
echo "Hello World" > output.txt
# Append output to a file
echo "Another line" >> output.txt
# Redirect errors to a file
command 2> errors.log
# Redirect both output and errors
command > output.txt 2>&1
# Discard output completely
command > /dev/null 2>&1
System Information Commands
# Display system information
uname -a
# Show disk usage
df -h
# Show directory size
du -sh /var/log
# Display memory usage
free -h
# Show running processes
ps aux
top
htop # interactive (install separately)
# Display network information
ip addr show
ip route show
# Show current date and time
date
# Show system uptime
uptime
# Show who is logged in
who
w
Helpful Keyboard Shortcuts
| Shortcut | Action |
|---|---|
Tab | Auto-complete commands, filenames, and paths |
Ctrl + C | Cancel/interrupt the current running command |
Ctrl + Z | Suspend the current process (resume with fg) |
Ctrl + D | Log out of the current shell / end input |
Ctrl + L | Clear the terminal screen (same as clear) |
Ctrl + R | Search through command history |
Up/Down Arrow | Navigate through previous commands |
Ctrl + A | Move cursor to the beginning of the line |
Ctrl + E | Move cursor to the end of the line |
!! | Repeat the last command (useful with sudo !!) |
Getting Help
Linux has excellent built-in documentation:
# Manual pages - comprehensive documentation
man ls
man grep
# Quick help - most commands support --help
ls --help
grep --help
# One-line command description
whatis ls
# Search for commands by description
apropos "copy files"
# Info pages (alternative to man)
info coreutils
Practice Makes Perfect
The best way to learn the command line is by using it daily. Start by navigating your file system, creating and organizing files, and gradually incorporate more advanced commands into your workflow. Next, explore the Linux File System to understand the directory structure you are navigating.