Basic Linux Commands: Guide to Terminal Essentials Linux Mastery Series
Prerequisites
What Are Basic Linux Commands?
Basic Linux commands are fundamental terminal instructions that enable you to navigate directories, manage files, view content, and interact with your Linux system. The essential commandsβpwd, ls, cd, mkdir, touch, cat, rm, cp, mv, and manβform the foundation that every Linux user must master before advancing to complex operations.
Quick Command Reference (Copy & Paste):
# Check where you are
pwd
# List files in current directory
ls
# List detailed file information
ls -lah
# Change to home directory
cd ~
# Create a new directory
mkdir my_project
# Create an empty file
touch notes.txt
# View file contents
cat notes.txt
# Get help for any command
man ls
These commands provide complete control over your Linux environment. Consequently, mastering them enables you to perform 90% of daily Linux tasks efficiently from the terminal.
Table of Contents
- How Do Basic Linux Commands Work?
- How to Understand the Terminal Prompt?
- What Is Command Syntax in Linux?
- How to Navigate with pwd, ls, and cd?
- How to Create Files and Directories?
- How to View File Contents?
- How to Copy, Move, and Delete Files?
- How to Get Help with man and –help?
- FAQ: Common Beginner Questions
- Troubleshooting: Common Command Problems
How Do Basic Linux Commands Work?
Basic Linux commands interact with the operating system through a shell interpreter (usually bash) that translates your text instructions into system operations. Moreover, each command is actually a small program stored in directories like /bin
, /usr/bin
, or /usr/local/bin
.
The Command Execution Process
When you type a command and press Enter, the shell performs these steps:
- Parse the command – Break down the command into name, options, and arguments
- Locate the executable – Search for the command in PATH directories
- Execute the program – Run the command with specified options
- Return results – Display output and return an exit code (0 for success)
Additionally, Linux commands are case-sensitive, meaning LS
and ls
are different. Therefore, always use lowercase for standard commands.
Command Categories
Category | Purpose | Examples |
---|---|---|
Navigation | Move between directories | cd , pwd , ls |
File Management | Create, copy, move, delete files | touch , cp , mv , rm |
File Viewing | Display file contents | cat , less , head , tail |
Information | Show system details | whoami , hostname , date |
Help | Get command documentation | man , help , --help |
Furthermore, most commands accept options (flags) that modify their behavior, typically starting with a single dash (-
) for short options or double dash (--
) for long options.
Related Guide: Understanding Linux File System Hierarchy
How to Understand the Terminal Prompt?
The terminal prompt displays important information about your session and indicates when the system is ready for input. Therefore, understanding prompt components helps you navigate confidently.
Standard Prompt Format
A typical Linux prompt looks like this:
username@hostname:~$
Breaking Down the Prompt:
Component | Meaning | Example |
---|---|---|
username | Your login name | john |
@ | Separator | @ |
hostname | Computer name | laptop |
: | Separator | : |
~ | Current directory | ~ (home) or /var/log |
$ | User type indicator | $ (regular) or # (root) |
Example Prompts
# Regular user in home directory
john@laptop:~$
# Regular user in /etc directory
john@laptop:/etc$
# Root user (superuser) - notice the #
root@server:/var/log#
# User in nested directory
mary@desktop:~/projects/webapp$
Important Prompt Symbols
# Regular user prompt
$
# Root/superuser prompt (dangerous!)
#
# Home directory shortcut
~
# Current directory
.
# Parent directory
..
Moreover, the prompt can be customized by modifying the PS1
environment variable in your ~/.bashrc
file. However, beginners should keep the default prompt until comfortable with Linux basics.
External Resource: Bash Prompt Customization Guide
What Is Command Syntax in Linux?
Linux command syntax follows a consistent pattern that makes commands predictable once you understand the structure. Specifically, every command consists of a name followed by optional modifiers and targets.
Basic Command Syntax
command [options] [arguments]
Components:
- command: The program to execute (e.g.,
ls
,cd
,cat
) - options: Flags that modify behavior (e.g.,
-l
,-a
,--help
) - arguments: Targets for the command (e.g., filenames, directories)
Real-World Examples
# Simple command with no options or arguments
pwd
# Command with argument
cd /var/log
# Command with option
ls -l
# Command with multiple options
ls -lah
# Command with options and argument
ls -lh /home/user
# Command with long-form option
ls --human-readable
# Command with multiple arguments
cp file1.txt file2.txt /backup/
Options: Short vs Long Format
# Short options (single dash, single letter)
ls -l # Detailed list
ls -a # Show all files
ls -h # Human-readable sizes
# Combine short options
ls -lah # Same as: ls -l -a -h
# Long options (double dash, full word)
ls --all # Same as -a
ls --human-readable # Same as -h
# Options with values
head -n 5 file.txt # Show 5 lines
head --lines=5 file.txt # Same with long option
Common Option Patterns
Pattern | Meaning | Example |
---|---|---|
-a | All (include hidden) | ls -a |
-r | Recursive | rm -r directory |
-f | Force | rm -f file |
-v | Verbose (detailed output) | cp -v file dest |
-h | Human-readable | ls -lh |
-i | Interactive (ask confirmation) | rm -i file |
Furthermore, many commands provide a --help
option that displays usage information and available options.
Related Guide: Linux Terminal Basics: Your First Commands
How to Navigate with pwd, ls, and cd?
The navigation trinityβpwd, ls, and cdβenables you to move through the Linux filesystem and understand your current location. Moreover, these three commands form the foundation of every terminal session.
pwd: Print Working Directory
The pwd
command shows your current location in the filesystem:
# Show current directory
pwd
# Output: /home/john
# pwd after changing directories
cd /var/log
pwd
# Output: /var/log
When to use pwd:
- After navigating to verify your location
- Before running potentially destructive commands
- In scripts to ensure correct directory context
- When lost in deep directory structures
ls: List Directory Contents
The ls
command displays files and directories:
# Basic listing
ls
# Output: Documents Downloads Music Pictures Videos
# Detailed listing with permissions and sizes
ls -l
# Output:
# drwxr-xr-x 2 john john 4096 Oct 15 10:30 Documents
# -rw-r--r-- 1 john john 1234 Oct 15 11:00 readme.txt
# Show all files including hidden
ls -a
# Output: . .. .bashrc .profile Documents Downloads
# Human-readable sizes
ls -lh
# Output shows sizes as 1.2M instead of 1234567
# Combined options
ls -lah
# All files, detailed, human-readable
# Sort by modification time (newest first)
ls -lt
# Sort by size (largest first)
ls -lS
# Reverse sort order
ls -lr
# List specific directory without changing to it
ls /etc
ls ~/Documents
Practical ls Variations:
# Count files in directory
ls | wc -l
# Show only directories
ls -d */
# Show only files (not directories)
ls -p | grep -v /
# Show with full timestamps
ls -l --time-style=full-iso
# Colorized output (usually default)
ls --color=auto
cd: Change Directory
The cd
command moves you between directories:
# Go to home directory (three ways)
cd
cd ~
cd $HOME
# Go to specific absolute path
cd /var/log
cd /etc/nginx
# Go to relative path
cd Documents
cd projects/webapp
# Go up one directory level
cd ..
# Go up two levels
cd ../..
# Go to previous directory (toggle)
cd -
# Go to root directory
cd /
# Go to another user's home (if permitted)
cd ~username
Navigation Workflow Example:
# Start in home
pwd
# Output: /home/john
# List contents
ls
# Output: Documents Downloads Music
# Enter Documents
cd Documents
pwd
# Output: /home/john/Documents
# List what's here
ls
# Output: reports budget.xlsx notes.txt
# Go back to home
cd
pwd
# Output: /home/john
# Jump directly to specific path
cd /var/log/nginx
pwd
# Output: /var/log/nginx
# Return to previous location
cd -
pwd
# Output: /home/john
External Resource: Linux Directory Structure Standard
How to Create Files and Directories?
Creating files and directories is essential for organizing your work. Furthermore, Linux provides simple commands that make file creation instant and reliable.
mkdir: Make Directory
The mkdir
command creates new directories:
# Create single directory
mkdir my_project
# Create multiple directories
mkdir dir1 dir2 dir3
# Create nested directories (parent path must exist)
mkdir project/src # Fails if 'project' doesn't exist
# Create nested directories with parents (-p option)
mkdir -p project/src/components
# Create with specific permissions
mkdir -m 755 public_folder
# Verbose output
mkdir -v new_directory
# Output: mkdir: created directory 'new_directory'
Practical mkdir Examples:
# Create project structure in one command
mkdir -p webapp/{src,tests,docs,config}
# Result:
# webapp/
# βββ src/
# βββ tests/
# βββ docs/
# βββ config/
# Create dated backup directory
mkdir backup-$(date +%Y-%m-%d)
# Creates: backup-2025-10-08
# Create multiple project directories
mkdir -p projects/{web,mobile,desktop}/src
touch: Create Empty Files
The touch
command creates empty files or updates file timestamps:
# Create single file
touch readme.txt
# Create multiple files
touch file1.txt file2.txt file3.txt
# Create files with extension
touch index.html style.css script.js
# Create numbered files
touch report{1..5}.txt
# Creates: report1.txt report2.txt report3.txt report4.txt report5.txt
# Update existing file's timestamp
touch existing-file.txt
Practical touch Examples:
# Create project files quickly
touch {index,about,contact}.html
# Create log file structure
touch logs/app-$(date +%Y-%m-%d).log
# Create hidden config file
touch .env
# Create file with specific timestamp
touch -d "2025-01-01 12:00:00" oldfile.txt
Combining mkdir and touch
# Create directory structure with files
mkdir -p project/{src,tests}
touch project/src/{main,utils,config}.js
touch project/tests/{test1,test2}.js
touch project/README.md
# Result:
# project/
# βββ src/
# β βββ main.js
# β βββ utils.js
# β βββ config.js
# βββ tests/
# β βββ test1.js
# β βββ test2.js
# βββ README.md
Related Guide: File Operations: Copy, Move, Delete Like a Pro
How to View File Contents?
Linux provides multiple commands for viewing file contents, each suited for different scenarios. Moreover, choosing the right viewing command makes working with files more efficient.
cat: Concatenate and Display
The cat
command displays entire file contents:
# Display single file
cat readme.txt
# Display multiple files
cat file1.txt file2.txt
# Display with line numbers
cat -n readme.txt
# Show non-printing characters
cat -A file.txt
# Combine files into new file
cat file1.txt file2.txt > combined.txt
less: Page Through Files
The less
command views large files one screen at a time:
# View file with pagination
less large-log.txt
# Navigation keys in less:
# Space - Next page
# b - Previous page
# / - Search forward
# ? - Search backward
# q - Quit
# G - Go to end
# g - Go to beginning
head: View File Beginning
The head
command shows the first lines of a file:
# Show first 10 lines (default)
head file.txt
# Show first 5 lines
head -n 5 file.txt
# Show first 20 bytes
head -c 20 file.txt
# View multiple files
head file1.txt file2.txt
tail: View File End
The tail
command shows the last lines of a file:
# Show last 10 lines (default)
tail file.txt
# Show last 20 lines
tail -n 20 file.txt
# Follow file as it grows (useful for logs)
tail -f /var/log/syslog
# Follow with retry (if file doesn't exist yet)
tail -F application.log
Practical Viewing Examples
# Quick preview of file
head -n 3 document.txt
# View log file in real-time
tail -f /var/log/application.log
# Show middle of file (lines 10-20)
head -n 20 file.txt | tail -n 10
# Display with syntax highlighting (requires bat/batcat)
batcat script.py
# Count lines in file
wc -l file.txt
# Search within file
grep "error" log.txt
# Display with line numbers for reference
cat -n config.conf
External Resource: GNU Coreutils – Text Utilities
How to Copy, Move, and Delete Files?
File manipulation commandsβcp, mv, and rmβenable you to organize, relocate, and remove files. Furthermore, understanding these commands prevents accidental data loss.
cp: Copy Files and Directories
The cp
command duplicates files or directories:
# Copy file to new location
cp source.txt destination.txt
# Copy to different directory
cp file.txt /backup/
# Copy multiple files to directory
cp file1.txt file2.txt file3.txt /backup/
# Copy directory recursively
cp -r my_directory /backup/
# Copy with verbose output
cp -v file.txt /backup/
# Output: 'file.txt' -> '/backup/file.txt'
# Preserve file attributes (permissions, timestamps)
cp -p file.txt /backup/
# Interactive mode (ask before overwrite)
cp -i file.txt existing-file.txt
# Update only newer files
cp -u *.txt /backup/
mv: Move or Rename Files
The mv
command moves files or renames them:
# Rename file in same directory
mv oldname.txt newname.txt
# Move file to different directory
mv file.txt /home/user/Documents/
# Move multiple files
mv file1.txt file2.txt file3.txt /destination/
# Move directory
mv old_directory /new/location/
# Rename directory
mv old_name new_name
# Move with verbose output
mv -v file.txt /backup/
# Interactive mode (ask before overwrite)
mv -i file.txt existing-file.txt
# Never overwrite existing files
mv -n file.txt /destination/
rm: Remove Files and Directories
The rm
command permanently deletes files:
# Remove single file
rm file.txt
# Remove multiple files
rm file1.txt file2.txt file3.txt
# Remove with confirmation
rm -i important-file.txt
# Force removal without confirmation
rm -f file.txt
# Remove directory recursively
rm -r directory/
# Force remove directory (dangerous!)
rm -rf directory/
# Verbose output
rm -v file.txt
# Output: removed 'file.txt'
# Remove files matching pattern
rm *.txt
rm backup-*.log
Safe Deletion Practices
# ALWAYS verify before recursive deletion
ls -la directory/ # Check contents first
pwd # Verify location
rm -ri directory/ # Use interactive mode
# Create trash function for safer deletion
trash() {
mv "$@" ~/.trash/
}
# Safer rm alias (add to ~/.bashrc)
alias rm='rm -i'
# Remove empty directories only
rmdir empty-directory/
# Find and remove old files safely
find ~/Downloads -mtime +30 -type f -exec ls {} \; # List first
find ~/Downloads -mtime +30 -type f -delete # Then delete
Practical File Operation Examples
# Backup file before editing
cp important-file.conf important-file.conf.bak
# Move all PDFs to documents
mv *.pdf ~/Documents/
# Copy entire project with structure
cp -r ~/projects/webapp /backup/
# Rename multiple files (requires rename command)
rename 's/old/new/' *.txt
# Move files newer than reference
find . -newer reference-file -exec mv {} /destination/ \;
Related Guide: Linux File Permissions Explained Simply
How to Get Help with man and –help?
Linux provides comprehensive built-in documentation through the man
(manual) system and command help options. Therefore, you never need to memorize every command option.
man: Manual Pages
The man
command displays complete documentation for commands:
# View manual for any command
man ls
man cp
man chmod
# Navigate man pages:
# Space - Next page
# b - Previous page
# / - Search forward
# n - Next search result
# q - Quit
# h - Help (show navigation keys)
# Search manual page titles
man -k keyword
apropos keyword # Same as man -k
# Show brief description
whatis command
# View specific section
man 5 passwd # Section 5: File formats
man 1 passwd # Section 1: User commands
Manual Sections:
Section | Content | Example |
---|---|---|
1 | User commands | man 1 ls |
2 | System calls | man 2 open |
3 | Library functions | man 3 printf |
4 | Device files | man 4 null |
5 | File formats | man 5 passwd |
6 | Games | man 6 fortune |
7 | Miscellaneous | man 7 signal |
8 | System administration | man 8 mount |
–help: Quick Command Help
Most commands provide quick help with the --help
option:
# Quick help for command
ls --help
cp --help
mkdir --help
# Pipe to less for long help
cp --help | less
# Save help to file for reference
ls --help > ls-help.txt
type and which: Find Commands
# Show what type of command
type ls
# Output: ls is aliased to `ls --color=auto'
type cd
# Output: cd is a shell builtin
# Find command location
which ls
# Output: /usr/bin/ls
which python3
# Output: /usr/bin/python3
# Show all locations
which -a python
info: Detailed Documentation
Some commands have more detailed info pages:
# View info documentation
info coreutils
info bash
# Navigation in info:
# n - Next node
# p - Previous node
# u - Up one level
# q - Quit
Practical Help Examples
# Find commands related to compression
man -k compress
# Output:
# gzip (1) - compress or expand files
# bzip2 (1) - compress or expand files
# tar (1) - archive utility
# Quick reference for find command
find --help | grep -i "name"
# Learn about file command
man file
# Get help on bash builtins
help cd
help pwd
# Search for specific option in man page
man ls | grep -A 5 "sort"
External Resource: Linux Documentation Project
FAQ: Common Beginner Questions
How do I clear the terminal screen?
Use the clear
command or press Ctrl+L
:
# Clear screen
clear
# Keyboard shortcut (faster)
Ctrl+L
What’s the difference between rm and rmdir?
rm
removes files and can recursively delete directories with -r
. rmdir
only removes empty directories:
# Remove empty directory
rmdir empty_folder
# Remove file
rm file.txt
# Remove directory and contents
rm -r folder_with_files
How do I stop a running command?
Press Ctrl+C
to interrupt most commands:
# Command running...
Ctrl+C # Stops execution
# For background processes
# First find process ID
ps aux | grep process_name
# Then kill it
kill process_id
Can I undo file deletion with rm?
No, rm
permanently deletes files. They don’t go to a trash/recycle bin. Therefore:
# Create safer deletion function
alias rm='rm -i' # Always ask for confirmation
# Or use trash-cli
sudo apt install trash-cli
trash file.txt # Sends to trash instead
How do I view hidden files?
Use ls -a
to show all files, including those starting with a dot:
# Show hidden files
ls -a
# Output: . .. .bashrc .profile Documents
# Detailed listing with hidden files
ls -la
What does “Permission denied” mean?
You lack permissions to access that file or directory. Solutions:
# Check file permissions
ls -l file.txt
# Change permissions (if you own it)
chmod +r file.txt
# Use sudo for system files (if you have privileges)
sudo cat /var/log/syslog
External Resource: Linux Command Line for Beginners
Troubleshooting: Common Command Problems
Problem: Command Not Found
Symptom: bash: command: command not found
Cause: Command doesn’t exist or isn’t in PATH
Solution:
# Check if command exists
which command_name
# Install missing command (Ubuntu/Debian)
sudo apt install package_name
# Check PATH variable
echo $PATH
# Find where command might be
find /usr -name "command_name" 2>/dev/null
Problem: Permission Denied
Symptom: Permission denied
when accessing files
Cause: Insufficient permissions
Solution:
# Check file permissions
ls -l file.txt
# Make file readable
chmod +r file.txt
# Use sudo for system files
sudo cat /etc/shadow
# Check if you own the file
ls -l file.txt
# If not, ask owner to change permissions
Problem: Directory Not Empty (rmdir fails)
Symptom: rmdir: failed to remove 'directory': Directory not empty
Cause: Directory contains files
Solution:
# Check directory contents
ls -la directory/
# Remove directory and contents
rm -r directory/
# Interactive removal (safer)
rm -ri directory/
Problem: File Already Exists (cp/mv fails)
Symptom: Won’t overwrite existing files
Solution:
# Force overwrite with cp
cp -f source.txt destination.txt
# Interactive mode (asks before overwrite)
cp -i source.txt destination.txt
# Backup existing file first
cp destination.txt destination.txt.bak
cp source.txt destination.txt
Problem: Tab Completion Doesn’t Work
Symptom: Pressing Tab doesn’t auto-complete
Cause: bash-completion not installed
Solution:
# Install bash-completion (Ubuntu/Debian)
sudo apt install bash-completion
# Source completion script
source /etc/bash_completion
# Add to ~/.bashrc for permanent fix
echo "source /etc/bash_completion" >> ~/.bashrc
Problem: Too Many Arguments
Symptom: Argument list too long
Cause: Too many files matched by wildcard
Solution:
# Use find with exec instead
find . -name "*.txt" -exec rm {} \;
# Or use xargs
find . -name "*.txt" | xargs rm
# Process in batches
ls *.txt | xargs -n 100 rm
Diagnostic Commands:
Command | Purpose |
---|---|
type command | Check if command exists |
which command | Find command location |
echo $PATH | Check PATH variable |
ls -l file | Check file permissions |
pwd | Verify current location |
whoami | Check current user |
External Resource: Linux Command Troubleshooting Guide
Additional Resources
Official Documentation
- GNU Coreutils Manual – Complete command reference
- Bash Reference Manual – Shell documentation
- Linux Man Pages – Online manual pages
- POSIX Standards – Command portability
Interactive Tutorials
- Linux Journey – Visual interactive lessons
- TLDR Pages – Simplified command examples
- ExplainShell – Command breakdown tool
- Linux Command Library – Beginner tutorials
Related LinuxTips.pro Guides
- Understanding Linux File System Hierarchy – Directory structure
- Mastering Linux Navigation Commands – Advanced movement
- File Operations: Copy, Move, Delete Like a Pro – Advanced file management
- Linux File Permissions Explained Simply – Security basics
- Text Processing with grep, sed, and awk – File content manipulation
Practice Resources
- OverTheWire – Bandit – Command line challenges
- Cmdchallenge – Terminal practice
- Linux Survival – Interactive tutorials
Community Resources
- Unix & Linux Stack Exchange – Q&A community
- Reddit r/linux4noobs – Beginner help
- LinuxQuestions.org – Forums
Conclusion
Mastering basic Linux commands unlocks the full power of your Linux system through the terminal. By understanding pwd for location awareness, ls for directory exploration, cd for navigation, mkdir and touch for creation, cat and less for viewing, and cp, mv, rm for file management, you gain complete control over your filesystem.
The key to command mastery is consistent practice and progressive learning. Start with simple commands like pwd and ls to build confidence, then add file creation and manipulation as you become comfortable. Moreover, remembering that man and –help provide instant documentation means you’re never stuck without guidance.
These fundamental commands form the foundation for everything else in Linuxβfrom system administration to software development to data science. Consequently, investing time to master them accelerates every future Linux skill you develop.
Next Steps:
- Practice each command with the examples in this guide
- Create a small project using mkdir, touch, cp, and mv
- Explore the Navigation Commands guide for advanced movement
- Learn File Permissions for security control
Last Updated: October 2025 | Author: LinuxTips.pro Team | Share your favorite Linux commands in the comments!