Useful Commands - Cheat Sheet
I have collected all my usefull commands in one place.
Linux Commands Cheatsheet
A comprehensive collection of useful Linux commands for system administration, development, and everyday tasks.
Table of Contents
- System Management
- File Operations
- Networking
- Process Management
- Service Management
- Compression and Archiving
- Permissions
- Package Management
- System Information
- Docker
- Java Development
- Maven
- Spring Boot
- Useful Aliases
System Management
Update and Upgrade
# Update the package lists
sudo apt-get update
# Upgrade installed packages
sudo apt upgrade -y
# Update and upgrade in one command
sudo apt update && sudo apt upgrade -y
Firewall Setup
# Install the Uncomplicated Firewall
sudo apt install ufw -y
# Enable the firewall
sudo ufw enable
# Allow common ports
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
sudo ufw allow 22 # SSH
sudo ufw allow ssh # SSH (alternative)
Reboot and Shutdown
# Reboot the system
sudo reboot
# Power off the system
sudo poweroff
# Halt the system
sudo halt
# Shutdown with options
sudo shutdown
File Operations
Viewing Files
# Display file contents
cat file.txt
# View file with pagination
less file.txt
# View last lines of file (follow mode)
tail -f file.log
# View last 1000 lines and follow updates
tail -n 1000 -f file.log
Creating and Modifying Files
# Write to a file (overwrites existing content)
echo "text" > file.txt
# Append to a file
echo "text" >> file.txt
File Management
# Copy a file
cp source_file destination_file
# Move or rename a file
mv source_file destination_file
# Remove a file
rm file.txt
File Search
# Find a file by name
find /path -name "filename"
# Search inside files for a string
grep -r "text_to_search" /path/to/search
# Colorize grep output
grep --color=auto "pattern" file
Networking
Network Information
# Install network tools
sudo apt install net-tools
# Check IP address
ip addr show
# Alternative for IP address
ifconfig
# Ping a host
ping example.com
# Display routing table
netstat -rn
# List all TCP connections with ports and processes
sudo netstat -tnlp
# Find process using specific port
sudo netstat -tnlp | grep :22
sudo netstat -tnlp | grep :8080
Port Management
# Install lsof
sudo apt-get install lsof
# Find which app is using a port
sudo lsof -i :PORT_NUMBER
# Example: sudo lsof -i :8080
# Kill an app using a port
sudo kill -9 $(sudo lsof -t -i :PORT_NUMBER)
Process Management
Process Monitoring
# List all running processes
ps aux
# Interactive process viewer
top
# Sort processes by memory usage
ps auxf | sort -nr -k 4
# Top 10 processes by memory usage
ps auxf | sort -nr -k 4 | head -10
# Sort processes by CPU usage
ps auxf | sort -nr -k 3
# Top 10 processes by CPU usage
ps auxf | sort -nr -k 3 | head -10
# Find Java/Spring Boot processes
ps aux | grep java
ps aux | grep spring-boot
Process Control
# Kill a process by PID
kill -9 PID
Service Management
Systemd Commands
# Check status of all services
service --status-all
# Check status of a specific service
sudo systemctl status service_name
# Start a service
sudo systemctl start service_name
# Stop a service
sudo systemctl stop service_name
# Restart a service
sudo systemctl restart service_name
# Enable a service at boot
sudo systemctl enable service_name
# Disable a service at boot
sudo systemctl disable service_name
Nginx Commands
# Test Nginx configuration
nginx -t
# Restart Nginx
systemctl restart nginx
Compression and Archiving
Tar Commands
# Compress files (tar.gz)
tar -czvf archive.tar.gz file1 file2
# Extract tar.gz files
tar -xzvf archive.tar.gz
Zip Commands
# Zip a file
zip archive.zip file1 file2
# Unzip a file
unzip archive.zip
Permissions
# Change permissions of a file
chmod 755 file.txt
# Change ownership of a file
sudo chown user:user file.txt
# Change permissions recursively
chmod -R 755 /path/to/directory
Package Management
Debian/Ubuntu (apt)
# Update the package list
sudo apt update
# Upgrade all packages
sudo apt upgrade
# Install a package
sudo apt install package_name
# Uninstall a package
sudo apt remove package_name
# Purge a package (completely remove config files as well)
sudo apt purge package_name
# Search for a package
apt search package_name
System Information
# Check disk usage
df -h
# Check memory usage
free -h
# Free memory info
free -m -l -t
# Check CPU usage
top
# Check system uptime
uptime
# CPU information
lscpu
# GPU memory information
grep -i --color memory /var/log/Xorg.0.log
Docker
Image Management
# Pull an image from Docker Hub
docker pull image_name
# View all images
docker images
# Remove an image
docker rmi image_name
# Build an image from a Dockerfile
docker build -t image_name .
# Remove dangling/unused images
docker image prune
Container Management
# Run a Docker container
docker run image_name
# Run with options
docker run -d -p host_port:container_port --name container_name image_name
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop a running container
docker stop container_name_or_id
# Start a stopped container
docker start container_name_or_id
# Remove a stopped container
docker rm container_name_or_id
# Check logs of a container
docker logs container_name_or_id
# Follow container logs with limited output
docker logs --follow --tail=50 container_name_or_id
# Exec into a running container
docker exec -it container_name_or_id /bin/bash
# Stop and remove all containers
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)
Java Development
# Compile a Java file
javac MyClass.java
# Run a compiled Java class
java MyClass
# Run a JAR file
java -jar myapp.jar
# Set classpath for external libraries
java -cp .:path_to_lib/library.jar MyClass
# Check Java version
java -version
Maven
# Clean the target directory
mvn clean
# Compile the source code
mvn compile
# Run unit tests
mvn test
# Package the project
mvn package
# Clean and build the project
mvn clean install
# analyze dependencies
mvn dependency:analyze
# Skip tests during build
mvn clean install -DskipTests
mvn clean install -DskipTests=true
# Check for updates of dependencies
mvn versions:display-dependency-updates
Spring Boot
# Run a Spring Boot application (packaged as JAR)
java -jar target/myapp.jar
# Run from source using Maven
mvn spring-boot:run
# Run in background with output to log file
nohup mvn spring-boot:run > app.log 2>&1 &
# Run in background with timestamped log file
nohup mvn spring-boot:run > "app_$(date '+%Y-%m-%d_%H-%M-%S').log" 2>&1 &
# Skip tests during build
mvn clean install -DskipTests=true
# Run in background
nohup mvn spring-boot:run &
# Generate a Spring Boot project
curl https://start.spring.io/starter.zip -d dependencies=web -o demo.zip
# Run with active profile
java -jar target/myapp.jar --spring.profiles.active=dev
# Run with debugging enabled
mvn spring-boot:run -Dspring-boot.run.fork=false -X
Useful Aliases
Add these to your /etc/bash.bashrc
file for system-wide aliases or ~/.bashrc
for user-specific aliases:
# File listing aliases
alias ll='ls -al' # List all files in long format
alias lll='ls -alh' # List all files with human-readable sizes
alias lt='ls --human-readable --size -1 -S --classify' # Sort by size
# Navigation shortcuts
alias cls='clear' # Clear the screen
alias ..='cd ..' # Go up one directory
alias ...='cd ../../../' # Go up three directories
alias ....='cd ../../../../' # Go up four directories
# Package management shortcuts
alias yep='sudo apt install' # Simplified installation
alias nop='sudo apt remove' # Simplified removal
alias update='sudo apt update && sudo apt upgrade -y' # Update and upgrade
# System commands
alias reboot='sudo /sbin/reboot'
alias poweroff='sudo /sbin/poweroff'
alias halt='sudo /sbin/halt'
alias shutdown='sudo /sbin/shutdown'
# Network monitoring
alias whatsup='service --status-all' # Check all services status
alias iftop='iftop -i eth1'
# System information
alias meminfo='free -m -l -t' # Memory information
alias path='echo -e ${PATH//:/\\n}' # Show PATH entries one per line
alias now='date +"%T"' # Current time
alias nowtime=now
alias nowdate='date +"%d-%m-%Y"' # Current date
# Process monitoring
alias psmem='ps auxf | sort -nr -k 4' # Sort processes by memory usage
alias psmem10='ps auxf | sort -nr -k 4 | head -10' # Top 10 by memory
alias pscpu='ps auxf | sort -nr -k 3' # Sort processes by CPU usage
alias pscpu10='ps auxf | sort -nr -k 3 | head -10' # Top 10 by CPU
# System information
alias cpuinfo='lscpu' # CPU information
alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log' # GPU memory info
# Enhanced command defaults
alias grep='grep --color=auto' # Colorize grep output
To add these aliases and make them available immediately:
# Add to global bashrc (requires sudo)
sudo echo "... aliases here ..." >> /etc/bash.bashrc
# Or add to user's bashrc
echo "... aliases here ..." >> ~/.bashrc
# Update aliases in the current session
source ~/.bashrc
Shell Script Creation & Execution
# Create and run a shell script
echo '#!/bin/bash
echo "Hello, World!"' > script.sh && chmod +x script.sh && ./script.sh
# Make a script executable
chmod +x deploy.sh
Print file and folders formatted
find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"