Linux Commands Cheat Sheet

Search 71 commonly used commands by category โ€” files and directories, permissions, text processing, processes, network, compression, and vi/vim editor shortcuts.

Files & Directories

๐Ÿ“

ls

View the list of files and folders in a directory.

Basic syntax and options

  • Basic form: `ls [options] [path]`
  • -l: shows detailed info like file size, permissions, and modified date.
  • -a: shows all files, including hidden files (those starting with a dot).
  • -h: shows file sizes in human-readable units (KB/MB).

ls -al โ†’ view a detailed list including hidden files.

๐Ÿ“‚

cd

Change your current working directory.

Basic syntax and options

  • `cd path` moves you to the given directory.
  • `cd ..` moves up one directory, `cd ~` moves to your home directory.
  • `cd -` returns to the previous directory you were in.

cd /var/log โ†’ move into the /var/log directory.

๐Ÿ“

pwd

Prints the full path of the directory you're currently in.

Basic syntax and options

  • Short for Print Working Directory.
  • Often used in shell scripts to save the current path into a variable.
๐Ÿ—‚๏ธ

mkdir

Creates a new directory (folder).

Basic syntax and options

  • `mkdir foldername` creates a folder.
  • The -p option creates nested folders in one go even if the intermediate paths don't exist.

mkdir -p project/src/utils โ†’ create nested folders in a single command.

๐Ÿ—‘๏ธ

rmdir

Deletes an empty directory.

Basic syntax and options

  • Can't delete a directory that has contents โ€” use `rm -r` for that.
โœจ

touch

Creates an empty file, or updates the modified time of an existing file.

Basic syntax and options

  • If the file doesn't exist it creates one; if it does, only its last-modified time is updated to now.

touch notes.txt โ†’ create an empty text file.

๐Ÿ“‹

cp

Copies files or directories.

Basic syntax and options

  • Used as `cp source destination`.
  • You need the -r option to copy an entire directory (folder).
  • The -i option asks for confirmation before overwriting a file with the same name.

cp -r src backup โ†’ copy the entire src folder to backup.

โžก๏ธ

mv

Moves a file or directory, or renames it.

Basic syntax and options

  • Keeping the same location but changing the name renames the file; specifying a different path moves it.

mv old.txt new.txt โ†’ rename a file.

โŒ

rm

Deletes files or directories.

Basic syntax and options

  • By default it only deletes files โ€” deleting a directory requires the -r (recursive) option.
  • The -f option force-deletes without confirmation, so use it carefully.
  • There's no recycle bin concept โ€” once deleted, recovery is very difficult.

rm -rf old_folder โ†’ force-delete a folder and everything inside it (use with caution).

๐Ÿ“„

cat

Prints a file's contents straight to the screen.

Basic syntax and options

  • You can concatenate and print several files at once (`cat a.txt b.txt`).
  • Often used to quickly check the contents of a short file.
๐Ÿ”Ž

find

Finds files or directories matching given conditions.

Basic syntax and options

  • `find path -name "pattern"` searches by name.
  • -type f (file) / -type d (directory) lets you filter by type.
  • The -mtime option lets you search based on modification time.

find . -name "*.log" โ†’ find every .log file under the current folder.

๐ŸŒณ

tree

Shows the directory structure as a tree, all at a glance.

Basic syntax and options

  • Not preinstalled on some distros, so you may need to install it separately (e.g. `apt install tree`).
  • The -L number option limits how many folder levels deep it displays.

Permissions & Users

๐Ÿ”

chmod

Changes access permissions (read/write/execute) for a file or directory.

Basic syntax and options

  • Permissions are expressed by adding read(4), write(2), and execute(1) for owner, group, and others in that order.
  • For example, 755 means the owner can do everything, while the group and others can only read and execute.
  • You can also set permissions symbolically (+ / -), like `chmod +x script.sh`.

chmod 755 script.sh โ†’ grant the owner read/write/execute, and everyone else read/execute.

๐Ÿ‘ค

chown

Changes the owner (user) of a file or directory.

Basic syntax and options

  • Used as `chown username filename`.
  • You can change both owner and group at once with `chown user:group filename`.

chown user1 file.txt โ†’ change the owner of file.txt to user1.

๐Ÿ‘ฅ

chgrp

Changes the group that owns a file or directory.

Basic syntax and options

  • Used as `chgrp groupname filename`.
๐Ÿ›ก๏ธ

sudo

Runs a command with administrator (root) privileges.

Basic syntax and options

  • Commonly said to stand for "Substitute User DO."
  • Used for tasks that require more than a regular user's permissions, like changing system settings or installing packages.
  • Asks for the password of the currently logged-in account before running.
๐Ÿ”„

su

Switches to another user (root by default).

Basic syntax and options

  • Typing just `su` tries to switch to root; `su username` switches to a specific account.
  • Unlike sudo, it requires the password of the account you're switching to.
๐Ÿ™‹

whoami

Prints the name of the currently logged-in user.

Basic syntax and options

  • Handy for quickly checking which account you're working as, in scripts or on a remote server.

Text Processing & Search

๐Ÿ”

grep

Finds lines containing a given string (pattern) in a file or input.

Basic syntax and options

  • `grep "pattern" filename` is the basic form.
  • The -i option searches case-insensitively.
  • The -r option searches recursively through files in a directory.
  • The -n option also shows the line number of each match.

grep -rn "TODO" ./src โ†’ find every line containing TODO, with line numbers, in all files under src.

โœ‚๏ธ

sed

A stream editor that replaces or edits text according to rules.

Basic syntax and options

  • The most common use is string substitution: `sed 's/old/new/g' file`
  • The trailing g means replace every match on each line.
  • The -i option edits the original file directly instead of printing to the screen.

sed -i 's/foo/bar/g' file.txt โ†’ replace every foo with bar in file.txt and save it.

๐Ÿงฎ

awk

Both a command and a small programming language for processing text by line and column (field).

Basic syntax and options

  • By default it splits each line into fields (`$1`, `$2`, โ€ฆ) based on whitespace.
  • Often used to pull out a specific column from a log file.

awk '{print $1}' access.log โ†’ print only the first field of each line.

โฌ‡๏ธ

tail

Prints only the last few lines of a file.

Basic syntax and options

  • Defaults to 10 lines.
  • The -f (follow) option is often used to watch a log file in real time.

tail -f /var/log/syslog โ†’ keep showing new content as it's added to the log file.

๐Ÿ”€

sort

Sorts text line by line.

Basic syntax and options

  • Sorts alphabetically by default.
  • The -n option sorts numerically.
  • The -r option reverses the sort order.
๐Ÿงน

uniq

Collapses consecutive duplicate lines into one.

Basic syntax and options

  • Doesn't work properly on unsorted data, so it's usually paired with `sort` (`sort file | uniq`).
  • The -c option also shows how many times each line repeated.
๐Ÿ”ข

wc

Counts the lines, words, and characters (bytes) in a file.

Basic syntax and options

  • -l prints only the line count, -w only the word count, -c only the byte count.

wc -l file.txt โ†’ print the total number of lines in the file.

๐Ÿ“œ

less

A viewer that lets you scroll through a file's contents a screen at a time.

Basic syntax and options

  • Unlike cat, it doesn't load the whole file into memory at once, so it opens large files quickly and lightly.
  • Scroll with the arrow keys, search with `/searchterm`, and quit with q.
๐Ÿ†š

diff

Compares two files line by line and shows the differences.

Basic syntax and options

  • Shows which lines were added, removed, or changed, which is useful for comparing code or config files.

diff old.txt new.txt โ†’ show the lines that differ between the two files.

Processes & System

โš™๏ธ

ps

Shows a list of currently running processes.

Basic syntax and options

  • `ps aux` is the most common form โ€” it shows detailed info for every process from every user.

ps aux | grep nginx โ†’ find only the running processes whose name contains nginx.

๐Ÿ“Š

top

Shows running processes in real time, ranked by CPU and memory usage.

Basic syntax and options

  • The screen refreshes periodically on its own; press q to quit.
  • htop is a widely used, more user-friendly upgrade (requires separate installation).
๐Ÿ›‘

kill

Sends a signal to a process to terminate it.

Basic syntax and options

  • Used as `kill processID (PID)`.
  • If the default signal doesn't stop it, use the -9 (force kill) option.

kill -9 1234 โ†’ force-terminate the process with PID 1234.

๐Ÿ’ฅ

killall

Terminates multiple processes at once by name.

Basic syntax and options

  • Convenient because you can kill processes by their executable name without knowing the PID.

killall firefox โ†’ terminate every process named firefox.

๐Ÿงพ

jobs

Shows the background or stopped jobs in the current terminal session.

Basic syntax and options

    โฏ๏ธ

    bg / fg

    Resumes a stopped job in the background (bg) or foreground (fg).

    Basic syntax and options

    • After pausing a job with Ctrl+Z, you can continue it in the background with `bg`, or bring it back to the foreground with `fg`.
    ๐Ÿงฐ

    systemctl

    Manages services (daemons) on systemd-based Linux systems.

    Basic syntax and options

    • `systemctl start/stop/restart servicename` starts, stops, or restarts a service.
    • `systemctl status servicename` checks its current status.
    • `systemctl enable servicename` sets it to start automatically at boot.

    systemctl restart nginx โ†’ restart the nginx service.

    ๐Ÿ’ฝ

    df

    Shows the total size and usage of disks (filesystems).

    Basic syntax and options

    • The -h option shows sizes in human-readable units (like GB).

    df -h โ†’ show the usage of each disk partition in GB/MB.

    ๐Ÿ“ฆ

    du

    Shows how much space a file or directory is using.

    Basic syntax and options

    • `du -sh foldername` shows the total size of an entire folder at a glance.
    ๐Ÿง 

    free

    Shows the system's memory (RAM) usage.

    Basic syntax and options

    • The -h option displays it in human-readable units.

    Network

    ๐Ÿ“ถ

    ping

    Sends a signal to a given address to check network connectivity and response time.

    Basic syntax and options

    • Keeps showing response times until you press Ctrl+C.

    ping google.com โ†’ keep checking the response time to google.com.

    ๐ŸŒ

    curl

    Requests data from a URL and retrieves the response โ€” commonly used for testing APIs.

    Basic syntax and options

    • By default it prints the response body to the screen.
    • Use -O to save the response to a file, and -I to check only the headers.

    curl -I https://example.com โ†’ check only the response headers for that site.

    โฌ‡๏ธ

    wget

    Downloads the file a URL points to.

    Basic syntax and options

    • More specialized for file downloads than curl.
    • If a download is interrupted, the -c option lets you resume it.
    ๐Ÿ”‘

    ssh

    Securely connects (remote login) to a remote server.

    Basic syntax and options

    • Communication is encrypted, letting you safely control a remote server.

    ssh user@192.168.0.10 โ†’ connect to the server at that IP as the user account.

    ๐Ÿ“ค

    scp

    Securely copies files between local and remote machines using SSH.

    Basic syntax and options

    • Used as `scp source destination`; a path on the server side is written as `user@host:path`.

    scp file.txt user@server:/home/user/ โ†’ send a local file to a remote server.

    ๐Ÿงญ

    ip / ifconfig

    Checks configuration info like a network interface's IP address.

    Basic syntax and options

    • `ip addr` is the standard command on modern distros, while `ifconfig` is the older way (some distros need to install it separately).
    ๐Ÿ”Œ

    netstat / ss

    Checks currently open network connections and port status.

    Basic syntax and options

    • `ss` is the modern command that replaces netstat, and it runs faster.
    ๐Ÿ›ฐ๏ธ

    traceroute

    Shows the network route (hops) packets take to reach a destination.

    Basic syntax and options

    • Used to check where along the route a delay or problem is occurring.

    Archive & Misc

    ๐Ÿ“ฆ

    tar

    Bundles multiple files into a single archive file, or unpacks one.

    Basic syntax and options

    • Often bundles and compresses at the same time, so it helps to remember the option combinations.
    • c=create, x=extract, z=gzip compression, v=show progress, f=specify filename.
    • Examples: -czvf (compress and bundle), -xzvf (extract while decompressing).

    tar -czvf backup.tar.gz ./project โ†’ compress the project folder with gzip into backup.tar.gz.

    ๐Ÿ—œ๏ธ

    gzip / gunzip

    Compresses a file to .gz format (gzip) or decompresses it (gunzip).

    Basic syntax and options

    • Can only compress a single file, so it's often used together with tar when bundling multiple files.
    ๐ŸŽ

    zip / unzip

    Compresses or extracts files and folders in .zip format.

    Basic syntax and options

    • A compression format that's well compatible with other operating systems like Windows.

    unzip archive.zip โ†’ extract the contents of archive.zip into the current folder.

    ๐Ÿ•˜

    history

    Shows a list of previously entered commands.

    Basic syntax and options

    • Typing `!number` re-runs the command at that number in the history.
    ๐Ÿท๏ธ

    alias

    Registers a short nickname for a long command you use often.

    Basic syntax and options

    • Adding it to a shell config file (like ~/.bashrc) makes it apply automatically every time you open a new terminal.

    alias ll="ls -al" โ†’ register ll so that typing it alone runs ls -al.

    ๐Ÿ“š

    man

    Opens the official manual (documentation) for a command.

    Basic syntax and options

    • Scroll with the arrow keys, search inside the manual with `/searchterm`, and quit with q.

    man ls โ†’ see the full options and usage of the ls command.

    ๐Ÿ’ฌ

    echo

    Prints a string or variable's value to the screen.

    Basic syntax and options

    • Also often used in shell scripts to check a value, or to append content to a file.

    echo "text" >> file.txt โ†’ append a line containing "text" to the end of file.txt.

    ๐Ÿ“Œ

    which

    Finds the path of the program file that actually runs for a given command.

    Basic syntax and options

    • Useful for checking which file actually runs when multiple versions of the same command name are installed.

    which python3 โ†’ show the actual file path that runs for the python3 command.

    vi/vim Editor

    ๐Ÿ“

    vi ๊ธฐ๋ณธ ๋ชจ๋“œ

    vi (vim) has three modes โ€” command, insert, and command-line โ€” and everything you do involves switching between them.

    Basic syntax and options

    • Opening a file starts you in command mode (you can't type text directly).
    • Pressing i, a, etc. switches to insert mode, where you can type text.
    • Pressing Esc in insert mode returns you to command mode.
    • Pressing colon (:) in command mode switches to command-line mode, where you enter commands like save and quit.

    vi file.txt โ†’ open file.txt in vi.

    โœ๏ธ

    i / a / I / A

    Switches to insert mode and starts typing text.

    Basic syntax and options

    • i: starts inserting "before" the cursor position.
    • a: starts inserting "after" the cursor position.
    • I: starts inserting at the beginning of the line; A: at the end of the line.
    ๐Ÿ”™

    Esc

    Returns to command mode from insert mode or another mode.

    Basic syntax and options

    • Almost everything in vi happens in command mode, so it helps to make a habit of pressing Esc to return to command mode before your next action.
    ๐Ÿงญ

    h / j / k / l

    Moves the cursor one space at a time (left/down/up/right).

    Basic syntax and options

    • h: left, j: down, k: up, l: right.
    • Designed so you can move quickly without taking your hands off the home row, unlike the arrow keys.
    ๐Ÿƒ

    w / b / e, 0 / $

    Moves the cursor by word or by line.

    Basic syntax and options

    • w: moves to the start of the next word, b: to the start of the previous word.
    • e: moves to the end of the current (or next) word.
    • 0: moves to the beginning of the line, $: to the end of the line.
    ๐Ÿ“„

    gg / G / :์ˆซ์ž

    Jumps straight to a position anywhere in the document.

    Basic syntax and options

    • gg: jumps to the very first line of the document.
    • G: jumps to the very last line of the document.
    • Typing `:number` and pressing Enter jumps straight to that line number.

    :42 โ†’ jump to line 42.

    โœ‚๏ธ

    dd

    Deletes (cuts) the current line.

    Basic syntax and options

    • A deleted line is copied into a temporary register, so you can paste it with p.
    • Combined with a number (`3dd`), it deletes several lines at once.
    ๐Ÿ“‹

    yy

    Copies (yanks) the current line.

    Basic syntax and options

    • Unlike dd, it copies without deleting.
    • Adding a number, like `3yy`, copies several lines at once.
    ๐Ÿ“ฅ

    p / P

    Pastes content you've copied or deleted.

    Basic syntax and options

    • p: pastes "after" the cursor line/position.
    • P: pastes "before" the cursor line/position.
    โ†ฉ๏ธ

    u / Ctrl+r

    Undoes or redoes an action.

    Basic syntax and options

    • u: undoes the most recent action.
    • Ctrl+r: redoes an action you undid.
    ๐Ÿ”ก

    x

    Deletes a single character under the cursor.

    Basic syntax and options

    • Handy for fixing a small typo right away.
    • Adding a number, like `3x`, deletes several characters at once.
    ๐Ÿ–๏ธ

    v / V

    Switches to visual mode for selecting a range.

    Basic syntax and options

    • v: selects a range character by character.
    • V: selects a range line by line.
    • After selecting a range, pressing d (delete), y (copy), etc. applies it to the whole selection.
    ๐Ÿ”

    :%s/old/new/g

    Finds and replaces a string across the whole document or a given range.

    Basic syntax and options

    • Leaving out % applies it only to the current line; leaving out g replaces only the first match on each line.
    • Adding c at the end (`:%s/old/new/gc`) asks for confirmation before each replacement.

    :%s/foo/bar/g โ†’ replace every foo with bar throughout the document.

    ๐Ÿ’พ

    :w / :q / :wq / :q!

    Saves your work or quits the editor.

    Basic syntax and options

    • :w โ€” saves the current content.
    • :q โ€” quits the editor (when there are no unsaved changes).
    • :wq (or ZZ) โ€” saves and quits.
    • :q! โ€” force-quits without saving changes.

    :wq โ†’ save and quit vi.

    ๐Ÿ”ข

    :set number

    Shows line numbers at the start of each line.

    Basic syntax and options

    • Turn it on with `:set number` (or `:set nu`), and off again with `:set nonumber`.
    ๐Ÿ”‚

    .

    Pressing the period (.) key repeats the edit you just made, exactly once more.

    Basic syntax and options

    • Especially useful when you need to apply the same edit in several places throughout a document.

    What does this page cover?

    Commands you run into often on the terminal but can never quite remember the options for, organized asFiles & Directories (ls, cd, cp, etc. โ€” basic operations) โ†’ Permissions & Users(chmod, sudo, etc. โ€” access control) โ†’ Text Processing & Search (grep, sed, awk, etc.) โ†’Processes & System (ps, top, systemctl, etc.) โ†’ Network (ping, curl, ssh, etc.) โ†’Archive & Misc (tar, history, alias, etc.) โ†’ vi/vim Editor (mode switching, cursor movement, save/quit shortcuts, etc.). Tap a category tab above or type a command into the search box to jump straight to what you need.

    Why do commands feel slightly different across distros?

    The commands on this page work about the same across most Linux distributions โ€” including Ubuntu and Debian-based ones โ€” as well as on macOS Terminal. That said, some commands like tree aren't preinstalled and need a separate install, and others like ifconfig are being replaced by newer commands like ip on modern distros. If a command doesn't work, first check whether it needs to be installed through your distro's package manager (apt, dnf, etc.).

    A note

    This page introduces the basic usage of commonly used Linux commands as educational content. Commands that delete files or change the system โ€” especially rm -rf โ€” can cause damage that's hard to undo if used incorrectly, so please double-check the command and path before running it on a real system. For more detailed options, also check each command's man page.

    Frequently Asked Questions

    Do these commands work in Windows Command Prompt (cmd) too?

    No. Windows cmd and PowerShell use different syntax. However, if you install WSL (Windows Subsystem for Linux) on Windows 10 or later, you can use the commands on this page exactly as shown, inside a real Linux environment.

    Why does sudo keep asking for my password?

    For security, sudo's authentication expires after a set time (about 15 minutes by default), so it asks again. This prevents accidentally making a big change to the system by running an admin-level command without any confirmation each time.

    Where can I find a command that's not on this page?

    There's currently no dedicated contact page for this language version. In the meantime, typing man commandname in your terminal will show you that command's official documentation right away.

    Are vi and vim the same thing?

    vim (Vi IMproved) is an enhanced version of the original vi editor, adding features like syntax highlighting, extended undo history, and plugins. On most modern Linux distributions, running it as vi actually opens vim under the hood, so the shortcuts on this page work the same either way.