Skip to the content.

Linux Cheatsheet

A quick-reference guide for essential and advanced Linux commands, covering file operations, process management, networking, permissions, text processing utilities, archiving, and remote file downloads. ## File Commands ```bash ls -la # List files in long format including hidden files (.dotfiles) cd # Change working directory pwd # Print current working directory path mkdir -p # Create a directory and any missing parent directories rm # Delete a file rm -rf # Force delete a directory recursively (dangerous!) cp # Copy a file cp -r # Copy a directory recursively mv # Move or rename a file/directory touch # Create an empty file or update its modified timestamp cat # Output entire file contents to terminal head -n 20 # View the first 20 lines of a file tail -n 20 # View the last 20 lines of a file tail -f # Follow and view append logs to a file in real-time less # Read a file with interactive paginated scrolling ``` ## Process Commands ```bash ps aux # View all currently running active processes ps aux | grep # Find process ID (PID) of a specific process top # Interactive real-time process monitor htop # Modern, colorful, interactive process monitor (recommended) kill # Terminate a process gracefully by ID (SIGTERM) kill -9 # Force terminate a process immediately (SIGKILL) killall # Terminate all processes matching a specific name pkill # Terminate processes matching a partial name bg # Resume a suspended process in the background fg # Move a background process to the foreground jobs # List all active background jobs lsof -i :8080 # List processes listening on a specific port (e.g. 8080) ``` ## Networking ```bash ping # Send ICMP echo requests to verify host connectivity ip addr # View local network interfaces and IP addresses (modern) ifconfig # View network interfaces and IP configurations (legacy) ss -tulpn # List active listening TCP/UDP sockets with PIDs (modern) netstat -tulpn # List active TCP/UDP listening connections (legacy) traceroute # Trace route hops to a destination host nslookup # Lookup IP address of a domain dig # Advanced DNS query tool (returns record details) nc -zv # Scan a port to check if it's open (Netcat) ``` ## Permissions ```bash # chmod [octal] (Numeric notation) chmod 755 script.sh # Owner: rwx, Group: r-x, Others: r-x chmod 644 config.txt # Owner: rw-, Group: r--, Others: r-- # chmod [symbolic] (Symbolic notation) chmod +x script.sh # Add execute permission (x) for everyone chmod u+w config.txt # Add write permission (w) for Owner/User (u) chmod g-w config.txt # Remove write permission (w) from Group (g) chmod o-r config.txt # Remove read permission (r) from Others (o) # Ownership chown # Change owner of a file chown : # Change both owner and group of a file chown -R # Change owner of a directory recursively chgrp # Change group ownership of a file ``` ## grep (Text Searching) ```bash grep "pattern" file.txt # Search for a pattern inside a file grep -i "pattern" file.txt # Case-insensitive pattern search grep -r "pattern" # Recursively search for a pattern in all files under directory grep -v "pattern" file.txt # Invert match: show lines NOT containing the pattern grep -E "regex" file.txt # Search using Extended Regular Expressions (ERE) grep -n "pattern" file.txt # Show line numbers alongside matches grep -C 3 "pattern" file.txt # Show 3 lines of context around each match (A=after, B=before) ``` ## sed (Stream Editor) ```bash sed 's/old/new/' file.txt # Replace the first occurrence of 'old' with 'new' per line sed 's/old/new/g' file.txt # Replace ALL occurrences of 'old' with 'new' globally in file sed -i 's/old/new/g' file.txt # In-place edit: replace and save modifications directly to the file sed '/pattern/d' file.txt # Delete lines matching a specific pattern sed -n '5,10p' file.txt # Print only lines 5 through 10 of a file ``` ## awk (Text Processing) ```bash awk '{print $1}' file.txt # Print only the first column of every line (whitespace separated) awk -F',' '{print $2}' f.csv # Print the second column using a custom comma delimiter (-F) awk '/pattern/ {print $3}' f # If a line matches the pattern, print its third column awk '$3 > 100 {print $1, $3}' # Conditional: print columns 1 and 3 if column 3 exceeds 100 awk '{sum += $1} END {print sum}' f # Sum up all numbers in the first column and output total ``` ## find (File Searching) ```bash find . -name "*.log" # Find files ending in .log in current directory and subdirectories find /path -iname "config*" # Find files starting with 'config' (case-insensitive) find . -type d # Find only directories (-type f for files, -type l for symlinks) find . -size +100M # Find files larger than 100 Megabytes find . -mtime -7 # Find files modified within the last 7 days find . -type f -exec chmod 644 {} \; # Execute action: apply chmod 644 to all found files ``` ## tar (Archiving) ```bash tar -czvf archive.tar.gz # Compress a directory into a tar.gz archive (c=create, z=gzip, v=verbose, f=file) tar -xzvf archive.tar.gz # Extract a tar.gz archive (x=extract) tar -cjvf archive.tar.bz2 # Compress a directory into a high-ratio tar.bz2 archive (j=bzip2) tar -xjvf archive.tar.bz2 # Extract a tar.bz2 archive tar -tf archive.tar # List the contents of an archive without extracting it ``` ## curl (Data Transfer) ```bash curl # Output website content/response to the terminal curl -o file.html # Download URL content and save it to a specific file curl -O # Download file and save it using its remote filename curl -I # Fetch HTTP response headers only (GET HEAD) curl -H "Auth: Key" # Send custom HTTP header with request curl -d "param1=val" # Send POST request data (defaults to application/x-www-form-urlencoded) curl -i -X POST -H "Content-Type: application/json" -d '{"id":1}' # Send JSON POST request ``` ## wget (File Downloader) ```bash wget # Download a file to the current directory wget -O custom.zip # Download and save a file using a custom local filename wget -c # Resume a partially downloaded file wget -b # Download a file in the background (runs in background log) wget --limit-rate=500k # Limit download speed to 500 Kilobytes per second wget -r --no-parent # Recursively download a directory/site under a specific URL path ```