0% found this document useful (0 votes)
3 views36 pages

Comprehensive Linux Commands Reference Guide

The document is a comprehensive reference guide for Linux commands, organized into sections covering date and time commands, system navigation, file and directory operations, file content operations, system operations, system information, process management, file permissions, and archive and compression. Each command includes its purpose, syntax, common options, and examples of usage. This guide serves as a quick reference for users to efficiently utilize various Linux commands.

Uploaded by

aniket.nagane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views36 pages

Comprehensive Linux Commands Reference Guide

The document is a comprehensive reference guide for Linux commands, organized into sections covering date and time commands, system navigation, file and directory operations, file content operations, system operations, system information, process management, file permissions, and archive and compression. Each command includes its purpose, syntax, common options, and examples of usage. This guide serves as a quick reference for users to efficiently utilize various Linux commands.

Uploaded by

aniket.nagane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Comprehensive Linux Commands Reference Guide

Table of Contents

1. Date and Time Commands 7. Process Management


2. System Navigation 8. File Permissions
3. File and Directory Operations 9. Archive and Compression
4. File Content Operations 10. Text Processing
5. System Operations 11. Pipes and Redirection
6. System Information 12. Quick Reference Summary

1. Date and Time Commands

1.1 date - Display or Set System Date

Purpose: Display or set the current date and time.

Syntax: date [OPTION]... [+FORMAT]

Common Options:

-d, --date=STRING - Display time described by STRING


-f, --file=DATEFILE - Process dates from file
-r, --reference=FILE - Display last modification time of FILE
-s, --set=STRING - Set date/time
-u, --utc - Print or set UTC time
Most Commonly Used Options:

date - Display current date and time


date +"%Y-%m-%d" - Custom format (YYYY-MM-DD)
date -d "tomorrow" - Display tomorrow's date

Examples:

# Basic usage
date
# Output: Mon Dec 25 14:30:45 PST 2023

# Custom format
date +"%Y-%m-%d %H:%M:%S"
# Output: 2023-12-25 14:30:45

# Relative dates
date -d "next Monday"
date -d "2 days ago"
date -d "+1 month"

# Set date (requires sudo)


sudo date -s "2023-12-25 14:30:00"

1.2 time - Measure Program Execution Time

Purpose: Measure the execution time of commands.

Syntax: time [OPTION]... COMMAND [ARG]...


Common Options: -f, --format=FORMAT - Specify output format
-o, --output=FILE - Write output to file
-a, --append - Append to output file
-v, --verbose - Verbose output

Examples:

# Basic usage
time ls -la
# Output: real 0m0.003s
# user 0m0.001s
# sys 0m0.002s

# Time a complex command


time find /home -name "*.txt"

# Verbose output
time -v sleep 2

2. System Navigation

2.1 man - Manual Pages

Purpose: Display manual pages for commands.

Syntax: man [OPTION]... [SECTION] PAGE...


Common Options:

-a, --all - Display all matching manual pages


-f, --whatis - Display short description
-k, --apropos - Search manual page names and
descriptions
-w, --where - Show location of manual pages

Most Commonly Used:

man command_name - View manual for command


man -k keyword - Search for commands related to keyword

Examples:

# View manual for ls command


man ls

# Search for commands related to "copy"


man -k copy

# Get brief description


man -f grep
# View specific section (section 5 for file formats)
man 5 passwd

2.2 pwd - Print Working Directory

Purpose:Display the current working directory path.

Syntax: pwd [OPTION]...

Common Options:

-L, --logical - Use PWD from environment


(default)
-P, --physical - Avoid all symlinks
Examples:

# Basic usage
pwd
# Output: /home/username/Documents

# Physical path (resolving symlinks)


pwd -P

2.3 cd - Change Directory

Purpose: Change the current working directory.

Syntax: cd [DIRECTORY]

Special Directories:

~ - Home directory
- - Previous directory
.. - Parent directory
. - Current directory
Examples:

# Go to home directory
cd
cd ~

# Go to specific directory
cd /etc/nginx

# Go to parent directory
cd ..

# Go to previous directory
cd -

# Go to subdirectory
cd Documents/Projects

3. File and Directory Operations


3.1 mkdir - Make Directories Purpose:

Create directories.
Syntax: mkdir [OPTION]... DIRECTORY...

Common Options:

-p, --parents - Create parent directories as needed


-m, --mode=MODE - Set file permissions
-v, --verbose - Print message for each created
directory

Most Commonly Used:

mkdir directory_name - Create single directory


mkdir -p path/to/directory - Create nested directories

Examples:

# Create single directory


mkdir new_folder

# Create multiple directories


mkdir dir1 dir2 dir3

# Create nested directories


mkdir -p projects/web/frontend
# Create with specific permissions
mkdir -m 755 public_folder

# Verbose output
mkdir -v test_dir

3.2 ls - List Directory Contents

Purpose: List files and directories.

Syntax: ls [OPTION]... [FILE]...

Common Options:

-a, --all - Show hidden files


-l - Long format listing
-h, --human-readable - Human readable sizes
-t - Sort by modification time
-r, --reverse - Reverse order
-S - Sort by file size
-R, --recursive - List subdirectories recursively

Most Commonly Used:

ls - Basic listing
ls -la - Long format with hidden files
ls -lh - Long format with human-readable sizes

Examples:
# Basic listing
ls

# Long format with hidden files


ls -la

# Human-readable sizes
ls -lh

# Sort by modification time (newest first)


ls -lt

# Sort by size (largest first)


ls -lS

# Recursive listing
ls -R

# List specific file types


ls *.txt
ls -l *.pdf

3.3 rmdir - Remove Empty Directories

Purpose: Remove empty directories.

Syntax: rmdir [OPTION]... DIRECTORY...

Common Options:

-p, --parents - Remove parent directories if empty


-v, --verbose - Output diagnostic for every directory processed
--ignore-fail-on-non-empty - Ignore failure due to non-empty
directories

Examples:

# Remove empty directory


rmdir empty_folder

# Remove nested empty directories


rmdir -p path/to/empty/dirs

# Verbose output
rmdir -v test_dir

# Remove multiple empty directories


rmdir dir1 dir2 dir3

3.4 cp - Copy Files and Directories

Purpose: Copy files or directories.

Syntax: cp [OPTION]... SOURCE DEST

Common Options:

-r, -R, --recursive - Copy directories recursively


-i, --interactive - Prompt before overwrite
-f, --force - Force copy, overwrite without prompt
-v, --verbose - Explain what is being done
-p, --preserve - Preserve file attributes
-u, --update - Copy only when source is newer
Most Commonly Used:

cp file1 file2 - Copy file


cp -r dir1 dir2 - Copy directory recursively

Examples:

# Copy file
cp [Link] [Link]

# Copy with confirmation


cp -i [Link] [Link]

# Copy directory recursively


cp -r project_folder backup_folder

# Copy preserving attributes


cp -p [Link] /backup/

# Copy multiple files to directory


cp *.txt /backup/

# Copy with verbose output


cp -v [Link] [Link]

3.5 mv - Move/Rename Files and Directories

Purpose: Move or rename files and directories.

Syntax: mv [OPTION]... SOURCE DEST

Common Options:
-i, --interactive - Prompt before overwrite
-f, --force - Force move without prompting
-v, --verbose - Explain what is being done
-u, --update - Move only when source is newer
-n, --no-clobber - Do not overwrite existing files

Examples:

# Rename file
mv old_name.txt new_name.txt

# Move file to directory


mv [Link] /home/user/Documents/

# Move with confirmation


mv -i [Link] /backup/

# Move multiple files


mv *.log /var/log/archive/

# Rename directory
mv old_folder new_folder

# Verbose move
mv -v source/* destination/
4. File Content Operations

4.1 cat - Display File Contents

Purpose: Display, combine, or create files.

Syntax: cat [OPTION]... [FILE]...

Common Options:

-n, --number - Number all output lines


-b, --number-nonblank - Number non-empty lines
-s, --squeeze-blank - Suppress multiple blank lines
-A, --show-all - Show all characters including non-printing
-E, --show-ends - Display $ at end of each line

Examples:

# Display file contents


cat [Link]

# Display multiple files


cat [Link] [Link]

# Number lines
cat -n [Link]

# Create file (redirect output)


cat > [Link]
# Type content, then Ctrl+D to save
# Append to file
cat >> existing_file.txt

# Combine files
cat [Link] [Link] > [Link]
5. System Operations

5.1 mount - Mount Filesystems

Purpose: Mount filesystems to directories.

Syntax: mount [OPTION]... DEVICE DIRECTORY

Common Options:

-t, --types=FSTYPE - Specify filesystem type


-o, --options=OPTS - Mount options

-r, --read-only - Mount read-only


-a, --all - Mount all filesystems in /etc/fstab
-v, --verbose - Verbose mode

Examples:

# Mount USB drive


sudo mount /dev/sdb1 /mnt/usb

# Mount with specific filesystem type


sudo mount -t ext4 /dev/sdb1 /mnt/disk

# Mount read-only
sudo mount -r /dev/cdrom /mnt/cdrom

# Mount with options


sudo mount -o rw,user /dev/sdb1 /mnt/usb
# View all mounted filesystems
mount

5.2 umount - Unmount Filesystems

Purpose: Unmount filesystems.

Syntax: umount [OPTION]... DIRECTORY|DEVICE

Common Options:

-f, --force - Force unmount


-l, --lazy - Lazy unmount
-v, --verbose - Verbose mode
-a, --all - Unmount all mounted
filesystems

Examples:

# Unmount by directory
sudo umount /mnt/usb

# Unmount by device
sudo umount /dev/sdb1

# Force unmount
sudo umount -f /mnt/stuck

# Lazy unmount
sudo umount -l /mnt/busy
5.3 lpr - Line Printer

Purpose: Submit files for printing.

Syntax: lpr [OPTION]... [FILE]...

Common Options:

-P printer - Specify printer


-# num - Number of copies
-J title - Job title
-o option - Printer-specific options
-r - Remove file after printing

Examples:

# Print file
lpr [Link]

# Print to specific printer


lpr -P laser_printer [Link]

# Print multiple copies


lpr -# 3 [Link]

# Print with job title


lpr -J "Important Report" [Link]

# Print with options


lpr -o media=A4 -o sides=two-sided [Link]
6. System Information

6.1 history - Command History

Purpose: Display command history.

Syntax: history [OPTION]... [N]

Common Options:

-c - Clear history list


-d offset - Delete history entry at offset
-a - Append new history lines to history
file
-w - Write current history to history file

Examples:

# Show all history


history

# Show last 10 commands


history 10

# Clear history
history -c

# Search history (interactive)


Ctrl+R

# Execute command from history


!15 # Execute command #15
!! # Execute last command
!grep # Execute last command starting with 'grep'

6.2 who - Show Logged-in Users

Purpose:Display information about currently logged-in users.

Syntax: who [OPTION]... [FILE | ARG1 ARG2]

Common Options:

-a, --all - Show all information


-b, --boot - Show time of last system boot
-d, --dead - Show dead processes
-H, --heading - Print column headings
-u, --users - Show idle time for each user
Examples:

# Basic usage
who

# Show all information


who -a

# Show with column headings


who -H

# Show current user


whoami

# Show boot time


who -b
7. Process Management

7.1 kill - Terminate Processes

Purpose: Send signals to processes to terminate them.

Syntax: kill [OPTION]... PID...

Common Options:

-s, --signal=SIGNAL - Specify signal to send


-l, --list - List signal names
-9 - SIGKILL (force kill)
-15 - SIGTERM (graceful termination, default)

Most Common Signals:

SIGTERM (15) - Graceful termination (default)


SIGKILL (9) - Force kill
SIGHUP (1) - Hang up

SIGSTOP (19) - Stop process

Examples:

# Graceful termination
kill 1234

# Force kill
kill -9 1234
kill -KILL 1234
# List all signals
kill -l

# Kill by process name (using killall)


killall firefox

# Kill all processes of a user


pkill -u username
8. File Permissions

8.1 chmod - Change File Permissions

Purpose: Change file and directory permissions.

Syntax: chmod [OPTION]... MODE FILE...

Permission Modes:

Numeric: 755, 644, etc.


Symbolic: u+x, g-w, o=r, etc.

Permission Values:

r (read) = 4

w (write) =

2x
(execute) = 1

Common Options:

-R, --recursive - Change permissions recursively


-v, --verbose - Output diagnostic for every file
processed
-c, --changes - Report only when changes are made
Examples:

# Numeric permissions
chmod 755 [Link] # rwxr-xr-x
chmod 644 [Link] # rw-r--r--
chmod 600 [Link] # rw-------

# Symbolic permissions
chmod +x [Link] # Add execute permission
chmod u+w [Link] # Add write for user
chmod g-r [Link] # Remove read for group
chmod o=r [Link] # Set other to read only

# Recursive permissions
chmod -R 755 /var/www/

# Verbose output
chmod -v 644 *.txt

8.2 passwd - Change Password

Purpose: Change user password.

Syntax: passwd [OPTION]... [USERNAME]

Common Options:

-l, --lock - Lock user account


-u, --unlock - Unlock user account
-d, --delete - Delete password (make account passwordless)
-e, --expire - Expire user password
-S, --status - Display password status

Examples:

# Change current user password


passwd

# Change another user's password (as root)


sudo passwd username

# Lock user account


sudo passwd -l username

# Unlock user account


sudo passwd -u username

# Check password status


sudo passwd -S username
9. Archive and Compression

9.1 tar - Archive Files

Purpose: Archive files and directories.

Syntax: tar [OPTION]... [FILE]...

Common Options:

-c, --create - Create archive


-x, --extract - Extract archive
-t, --list - List archive contents
-f, --file=ARCHIVE - Archive filename
-v, --verbose - Verbose output
-z, --gzip - Filter through gzip
-j, --bzip2 - Filter through bzip2

Most Commonly Used Combinations:

tar -czf - Create gzipped archive


tar -xzf - Extract gzipped archive
tar -cjf - Create bzip2 archive

Examples:

# Create tar archive


tar -cf [Link] files/

# Create gzipped archive


tar -czf [Link] files/

# Create bzip2 archive


tar -cjf [Link].bz2 files/

# Extract archive
tar -xf [Link]
tar -xzf [Link]

# List archive contents


tar -tf [Link]

# Extract with verbose output


tar -xvf [Link]

# Extract to specific directory


tar -xf [Link] -C /destination/

9.2 zip - Create ZIP Archives

Purpose: Create and manipulate ZIP archives.

Syntax: zip [OPTIONS] [Link] files...

Common Options:

-r, --recurse-paths - Recurse into directories


-u, --update - Update existing entries
-d, --delete - Delete entries from archive
-v, --verbose - Verbose output
-9 - Maximum compression
-e, --encrypt - Encrypt archive
Examples:

# Create zip archive


zip [Link] [Link] [Link]

# Create zip with directory


zip -r [Link] project_folder/

# Maximum compression
zip -9 -r [Link] files/

# Encrypt zip
zip -e [Link] sensitive_file.txt

# Update existing archive


zip -u [Link] new_file.txt

# Extract zip (using unzip)


unzip [Link]

# List zip contents


unzip -l [Link]

10. Text Processing

10.1 wc - Word, Line, Character Count

Purpose: Count lines, words, and characters in files.


Syntax: wc [OPTION]... [FILE]...

Common Options:

-l, --lines - Count lines only


-w, --words - Count words only
-c, --bytes - Count bytes
-m, --chars - Count characters
-L, --max-line-length - Length of longest line

Examples:

# Count lines, words, characters


wc [Link]
# Output: 10 50 300 [Link] (lines words chars)

# Count lines only


wc -l [Link]

# Count words only


wc -w [Link]

# Count characters
wc -m [Link]
# Count for multiple files
wc *.txt

# Count from standard input


echo "Hello World" | wc -w

10.2 sort - Sort Lines in Files

Purpose: Sort lines in text files.

Syntax: sort [OPTION]... [FILE]...

Common Options:

-r, --reverse - Reverse sort order


-n, --numeric-sort - Numeric sort
-u, --unique - Remove duplicate lines
-k, --key=KEYDEF - Sort by specific field

-t, --field-separator=SEP - Field separator

-o, --output=FILE - Write output to file

Examples:

# Basic sort
sort [Link]

# Reverse sort
sort -r [Link]

# Numeric sort
sort -n [Link]
# Remove duplicates
sort -u [Link]

# Sort by specific column (space-separated)


sort -k 2 [Link]

# Sort with custom separator


sort -t ',' -k 2 [Link]

# Sort and save to file


sort [Link] -o sorted_file.txt

10.3 grep - Search Text Patterns

Purpose: Search for patterns in files using regular expressions.

Syntax: grep [OPTION]... PATTERN [FILE]...

Common Options:

-i, --ignore-case - Case insensitive search


-v, --invert-match - Invert match (show non-matching)
-n, --line-number - Show line numbers
-r, --recursive - Search directories recursively
-l, --files-with-matches - Show only filenames
-c, --count - Count matching lines
-w, --word-regexp - Match whole words only

Most Commonly Used:

grep "pattern" file - Basic search


grep -i "pattern" file - Case insensitive
- Recursive search
grep -r "pattern" directory/

Examples:

# Basic search
grep "error" [Link]

# Case insensitive search


grep -i "ERROR" [Link]

# Show line numbers


grep -n "function" [Link]

# Recursive search in directory


grep -r "TODO" src/

# Count matches
grep -c "error" [Link]

# Show only filenames with matches


grep -l "import" *.py

# Invert match (show lines without pattern)


grep -v "debug" [Link]

# Whole word match


grep -w "cat" [Link]

# Multiple patterns
grep -E "error|warning" [Link]

# Use with regular expressions


grep "^[0-9]" [Link] # Lines starting with digit
grep "\.txt$" filelist # Lines ending with .txt
11. Pipes and Redirection

11.1 Pipes ( | ) - Connect Commands

Purpose: Connect the output of one command to the input of another.

Syntax: command1 | command2 | command3

Common Redirection Operators:

> - Redirect output to file (overwrite)


>> - Redirect output to file (append)
< - Redirect input from file
2> - Redirect error output
&> - Redirect both output and error

Basic Examples:

# Basic pipe
ls -l | grep ".txt"

# Multiple pipes
cat [Link] | grep "error" | wc -l

# Sort and remove duplicates


cat [Link] | sort | uniq

# Find largest files


ls -la | sort -k5 -nr | head -10

# Search in compressed files


zcat [Link] | grep "pattern"

# Process CSV data


cat [Link] | cut -d',' -f2 | sort | uniq -c

Redirection Examples:

# Output redirection
ls -l > [Link] # Overwrite
ls -l >> [Link] # Append
command 2> [Link] # Redirect errors
command &> all_output.txt # Redirect everything

# Input redirection
sort < [Link]

# Here document
cat << EOF > [Link]
This is a configuration file
with multiple lines
EOF

# Tee command (output to both file and stdout)


ls -l | tee [Link] | grep ".txt"

Advanced Pipeline Examples:

# Find files modified in last 7 days and show sizes


find . -mtime -7 -type f | xargs ls -lh | sort -k5 -hr

# Count occurrences of each word in a file


cat [Link] | tr ' ' '\n' | sort | uniq -c | sort -nr
# Monitor log file in real-time
tail -f /var/log/syslog | grep "ERROR"

# Extract specific columns from CSV


cat [Link] | cut -d',' -f1,3,5 | head -20

# Find and replace text in multiple files


find . -name "*.txt" | xargs sed -i 's/old_text/new_text/g'

# System monitoring pipeline


ps aux | awk '{print $3, $11}' | sort -nr | head -10

# Network analysis
netstat -tuln | grep LISTEN | awk '{print $4}' | cut -d':' -f2

You might also like