Skip to content
Home
Linux Command Cheat Sheet

Linux Command Cheat Sheet

Linux Linux 9 min read 1774 words Intermediate ExcellentWiki Editorial Team

The Linux command line is the most powerful tool in a developer’s arsenal. While graphical interfaces are convenient, the terminal gives you precise control, composability, and the ability to automate everything. This cheat sheet covers the commands you will use daily, organized by category.

The philosophy of Unix command-line tools is that each command does one thing well, and tools are combined with pipes (|) and redirections (>, >>, <) to build complex pipelines. For example, ps aux | grep nginx | wc -l counts the number of nginx processes by combining process listing, text search, and line counting.

File Operations

Create, copy, move, and delete files and directories:

ls -la                   # list files with details
ls -lh                   # human-readable sizes
ls -lt                   # sorted by modification time

cp file.txt /backup/     # copy file
cp -r dir/ /backup/      # copy directory recursively
cp -a dir/ /backup/      # copy preserving attributes

mv file.txt new.txt      # rename
mv file.txt /target/     # move

rm file.txt              # delete file
rm -rf dir/              # delete directory (careful!)
rm -i file.txt           # prompt before each delete

mkdir dir                # create directory
mkdir -p a/b/c           # create nested directories

find . -name "*.py"      # find files by name
find . -type f -size +10M  # files larger than 10MB

The ls command accepts many options: -S sorts by size, -t by time (newest first), -r reverses sort order. The find command is incredibly powerful — beyond -name and -size, you can combine tests with -and and -or, execute commands on found files with -exec, and filter by modification time with -mtime.

Danger Warning

Commands like rm -rf and mv can cause irreversible data loss if used carelessly. Always double-check the path before deleting. Consider aliasing rm to rm -i for an interactive prompt, or use trash-cli (a safer alternative that moves files to a trash folder) for everyday deletions.

Viewing Files

Read file contents without opening an editor:

cat file.txt               # print entire file
less file.txt              # scroll through (q to quit)
head -20 file.txt          # first 20 lines
tail -20 file.txt          # last 20 lines
tail -f log.txt            # follow new lines (live)
wc -l file.txt             # line count
nl file.txt                # numbered lines

less is more than a pager — it supports search (/pattern), line number navigation (N G), and even tail-mode (F, like tail -f). For very large files, less is preferred over cat because it loads only the visible portion into memory.

Text Processing

The Unix text processing suite — grep, sed, awk, sort, and uniq — forms the backbone of command-line data analysis.

grep "error" log.txt                # search for pattern
grep -r "todo" src/                  # recursive search
grep -i "warning" log.txt            # case-insensitive
grep -v "debug" log.txt              # exclude matches
grep -c "error" log.txt              # count matches

sed 's/old/new/g' file.txt           # replace all occurrences
sed -i '.bak' 's/old/new/g' file.txt  # in-place with backup
awk '{print $1, $3}' data.txt         # print columns
sort file.txt                         # sort lines
uniq file.txt                         # remove duplicates (sort first)

grep

Grep supports extended regex with -E (or egrep), recursively searches with -r, and shows surrounding context with -C 3 (three lines before and after). Use grep -l "pattern" *.txt to list only filenames that contain a match — useful for finding which files reference a function or configuration key.

sed

Sed’s most common use is find-and-replace. The s/old/new/g command substitutes old with new globally on each line. The -i flag edits files in place — always provide a backup extension like .bak the first time you run a sed command in case the pattern is wrong.

awk

Awk is a full programming language for text processing. The pattern {print $1, $3} prints the first and third columns of each line. By default, awk splits on whitespace. You can change the field separator with -F,` for CSV files. Awk also supports BEGIN/END blocks for header/footer processing, numeric comparisons, and custom functions.

Process Management

Monitor and control running processes:

ps aux                   # all running processes
ps aux | grep nginx      # find specific process
top                      # live process viewer
htop                     # better process viewer (if installed)
kill PID                 # terminate process
kill -9 PID              # force kill
killall nginx            # kill all processes by name

Use kill -15 (SIGTERM) as the default termination signal — it lets the process clean up. Only use kill -9 (SIGKILL) for unresponsive processes because the process cannot catch it and may leave corrupted state (like orphaned temporary files). The pgrep and pkill commands combine grep and kill: pkill -f "python script.py" kills all processes matching the pattern in their command line.

Networking

Test connectivity, transfer data, and inspect network configuration:

curl https://example.com          # HTTP request
curl -o file.txt https://example.com/file  # download
wget https://example.com/file     # download (alternative)

ping google.com                   # test connectivity
traceroute google.com             # trace network path
ss -tln                           # listening TCP ports
ss -uan                           # all UDP connections
netstat -tln                      # if ss is not available

ip a                              # network interfaces and IPs
ip r                              # routing table
nslookup example.com              # DNS lookup (or `dig example.com`)

Curl is the Swiss Army knife of HTTP debugging. Useful options include: -v for verbose headers, -X POST for custom HTTP methods, -H "Authorization: Bearer token" for custom headers, -d '{"key":"value"}' for POST data, and -w "\n%{http_code}" to print the response status code. For API debugging, combine curl with jq to pretty-print JSON responses.

Disk Usage

Monitor storage space and disk usage:

df -h                    # disk free (human-readable)
du -sh dir/              # directory size
du -sh * | sort -rh      # sizes of all items, sorted
lsblk                    # block devices
mount                    # mounted filesystems

du -sh * | sort -rh is the fastest way to find the largest folders in the current directory. Use ncdu (if installed) for an interactive disk usage analyzer — it is like du with a visual, navigable interface.

Compression

Archive and compress files for backups or transfers:

tar -czf archive.tar.gz dir/        # compress directory
tar -xzf archive.tar.gz             # extract
tar -czf archive.tar.gz --exclude='node_modules' dir/

zip -r archive.zip dir/             # zip directory
unzip archive.zip                   # unzip

gzip file.txt                       # compress (replaces with .gz)
gunzip file.txt.gz                  # decompress

Tar arguments follow a pattern: c for create, x for extract, z for gzip compression, f for file output. Use the --exclude flag to skip unnecessary files like node_modules or .git. For faster compression, use tar -cf archive.tar dir/ without compression, then compress with a multi-threaded tool like pigz (parallel gzip).

Permissions

Control access to files and directories:

chmod 644 file.txt     # rw-r--r--
chmod 755 script.sh    # rwxr-xr-x
chmod 600 secret.txt   # rw-------
chown user:group file  # change owner:group
umask 022              # default permissions for new files

The three digits represent owner, group, and others permissions, where each digit is the sum of read (4), write (2), and execute (1). Use 750 for directories (rwxr-x---) — owner has full access, group can read and traverse, and others have no access. The chown command changes file ownership; use chgrp if you only need to change the group.

System Information

Get details about your hardware, kernel, and OS:

uname -a                # kernel version
cat /etc/os-release     # OS distribution
free -h                 # memory usage
lscpu                   # CPU info
uptime                  # how long since boot
dmesg | tail            # kernel messages

Use lshw (install separately) for a comprehensive hardware inventory. The free -h command shows total, used, and available RAM — pay attention to the “available” column rather than “free” because Linux caches files in unused memory for performance.

Package Management

Install, update, and remove software packages:

Debian/Ubuntu

apt update && apt upgrade     # update all packages
apt install package            # install
apt remove package             # uninstall
apt search keyword             # search packages

Always run apt update before apt upgrade to refresh the package index. Use apt list --upgradable to see which packages will be updated. For system upgrades between releases, use apt full-upgrade or do-release-upgrade.

RHEL/Fedora

dnf update                    # update all packages
dnf install package            # install
dnf remove package             # uninstall
dnf search keyword             # search packages

SSH

Connect to and manage remote servers securely:

ssh user@host                  # connect to remote
ssh -p 2222 user@host          # custom port
ssh-keygen -t ed25519          # generate SSH key
ssh-copy-id user@host          # copy public key to server
ssh -i key.pem user@host       # use specific key
scp file.txt user@host:/path/  # copy file over SSH

Always use Ed25519 keys (the -t ed25519 option) instead of RSA — they are faster and more secure. Add your key to ~/.ssh/config with a Host alias to avoid typing full addresses:

Host myserver
    HostName 192.168.1.100
    User admin
    Port 2222
    IdentityFile ~/.ssh/mykey

Then connect with just ssh myserver.

Shortcuts

Speed up your terminal workflow:

ShortcutAction
Ctrl+CKill current command
Ctrl+ZSuspend current command
Ctrl+DEnd-of-file (exit shell)
Ctrl+WDelete word before cursor
Ctrl+UDelete entire line
Ctrl+RReverse search history
TabAuto-complete
!!Repeat last command
!$Last argument of last command

Ctrl+R followed by a keyword searches your command history — press Ctrl+R repeatedly to cycle through all matches. !! is invaluable when you forget sudo: type sudo !! to rerun the last command with elevated privileges. The !$ shortcut expands to the last argument of the previous command: mkdir new-project && cd !$.


Related: See our grep command guide and file permissions guide.

Frequently Asked Questions

What is the minimum system requirement for linux command cheat sheet?

System requirements vary by implementation. Most modern solutions require at least 4GB of RAM, a multi-core processor, and a stable internet connection. For specific applications, refer to the vendor documentation. Hardware requirements typically increase with scale — enterprise deployments need significantly more resources than personal or small business setups.

How does this compare to alternative approaches?

Every technology choice involves trade-offs. Some prioritize ease of use over customization, while others offer maximum control at the cost of complexity. Evaluating your specific needs, technical expertise, and growth plans helps determine the right fit. Many organizations use a combination of approaches to balance competing priorities.

What security considerations should I be aware of?

Security should be considered from the start, not as an afterthought. Keep all software updated, use strong authentication, encrypt sensitive data, and follow the principle of least privilege. Regular security audits and staying informed about emerging threats are essential practices for maintaining a secure deployment.

How do I troubleshoot common issues?

Start by isolating the problem: check logs, verify configurations, and test components individually. Common issues include network connectivity problems, permission errors, and version incompatibilities. Systematic troubleshooting — changing one variable at a time — helps identify root causes efficiently. Online communities and documentation are valuable resources when you encounter unfamiliar problems.

Section: Linux 1774 words 9 min read Intermediate 756 articles in section Report inaccuracy Back to top