This document contains the content of /resources/docs/text/linux_cheat_sheet.txt. Download the file by setting the ?act=download parameter, or access the raw file at either srcs.cc or src.cerium.cc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 Linux Terminal Cheat Sheet This is a derivative work under a CC BY-NC-SA 4.0 license. See license information section for more details. This document should be viewed with a monospace font and without line wrapping. This document aims to improve the original by adding content, removing outdated information, improving readability and formatting, improving examples, and various other changes. Legend | Placeholders are contained in '<' and '>', they should be replaced with the relevant string, e.g. '<file>' could be 'options.json' | A number will be appended to indicate separate placeholders of the same type, e.g. <file1>, <file2> | Multiple options are each separated by a '/' in the placeholder, some placeholders with multiple options include literal strings of text which should not be replaced, e.g. '<y/n>' could be 'y' or 'n' | | <file> is the path or name of a file | <directory> is the path to a directory, it should be inside '' or "" if the path includes whitespaces or other special characters | <command> is any valid terminal command | <number> can be any valid whole number, and can have decimals or other restrictions if specified | <package> can be the name of any valid package in a package repository | <address> can be any valid URL or IP address | <archive> is the path or name of an archive file | <text> can be almost any string of characters | <disk> can be any valid disk path, e.g. '/dev/sda' | <device> can be any valid disk or partition path, e.g. '/dev/nvme0n1p1' or '/dev/sdb' | <source> can be a file or directory acting as a source | <dest> can be a file or directory acting as a destination | <time> can be any valid date or time | <user> can be any valid user on a system | <group> can be any valid group on a system | | <YYYY> is the year | <MM> is the month, from 01-12 | <DD> is the day of the month, from 01-31 | <D> is the day of the week, from 0-6, 0 being Sunday | <h> is the hour in 24-hour time, from 00-23 | <m> is the minute, from 00-59 | <s> is the second, from 00-59 Table of Contents | Section Line number |-------------------------------------------------------------- | Basic Information .............................. 78 | Terminal Shortcuts 80 | File Path Syntax 104 | Console Terminology 114 | Shells 122 | Text Editors 129 | Basic Commands ................................. 138 | Terminal Navigation 140 | System Information 170 | Time and Date 187 | Installing Software ............................ 199 | Arch Linux (Using pacman) 201 | Debian and Derivatives (Using apt) 215 | From Source (Do Your Own Research) 227 | File Data ...................................... 238 | File Manipulation 240 | File Searching 287 | Reading, Sorting, and Filtering Data 301 | Compressing and Extracting Data 346 | Mounting and Filesystems 358 | Permissions .................................... 383 | Users and Groups 385 | File Permissions 395 | Network ........................................ 414 | Local Area Network 416 | Internet 432 | Data Over Internet 445 | Programs ....................................... 476 | Scheduling Programs 478 | Background Processes 500 | Process Management 508 | Managing Commands .............................. 537 | Flow Redirection 539 | Chaining Commands 557 | Executing and Scripting 565 | License Information ............................ 595 |-------------------------------------------------------------- Basic Information ################################################################################################################################################################## Terminal Shortcuts --------------------------------------------------------------------------------------------------------------------------------------------------------------- Ctrl+L | Clear the terminal screen Ctrl+D | Exit (sends a signal indicating the end of a text stream) Ctrl+C | Kills the current program (soft kill, lets the program intercept the command to clean up before exiting) Ctrl+Z | Pause current program Shift+PgUp | Scrolls up the terminal Shift+PgDn | Scrolls down the terminal Ctrl+A | Cursor to the start of the line Ctrl+E | Cursor to the end of the line Ctrl+U | Delete everything left of the cursor Ctrl+K | Delete everything right of the cursor Ctrl+W | Delete first word on the left 'Up Arrow' | Navigates previous commands 'Down Arrow' | Navigates previous commands Ctrl+R | Searches previous commands Tab | Auto complete a file path or command Alt+. | Inserts the last argument from previous commands !! | Repeat last command Ctrl+Alt+F<number> | Selects <number> console, F1-F6 switches between different virtual consoles, and F7 is used for the X session (with GUI windows and apps) File Path Syntax ----------------------------------------------------------------------------------------------------------------------------------------------------------------- / | Root directory, parent path to all the files on a system (always used to specify absolute paths) | e.g. '/home/user/Documents/data/data-tuesday.csv' is the absolute path for './data/data-tuesday.csv' when working in '~/Documents' . | Working directory | e.g. 'cd ./downloaded_files/' functions the same as 'cd /home/user/Downloads/downloaded_files/' when working in '/home/user/Downloads' ~ | Home directory | e.g. 'cd ~/Documents/' .. | Parent of working directory |e.g. 'cd ..' functions the same as 'cd /home/user/' when working in '/home/user/Downloads' Console Terminology -------------------------------------------------------------------------------------------------------------------------------------------------------------- Console | Sometimes refers to a physical terminal (like the connected keyboard and monitor), and is a low-level part of the system | can also be called a tty (teletypewriter) Terminal | Sometimes refers to a terminal emulator, or the hardware (like a connected keyboard and monitor), running a shell Terminal Emulator | A software wrapper for a shell Shell | A program which interprets and processes input and returns output Command Line | An interface used to enter commands (Command-Line Interface, CLI) Shells --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- sh | A programming language described by the POSIX standard (sh is a specification, not implementation, and usually links to another implementation like bash) bash | A common implementation of the POSIX standard, although not fully POSIX-compliant (it may change the behaviour of valid POSIX shell scripts), it has more functionality than sh | you can search for lists of differences between shells dash | The default shell for Debian, fully POSIX-compliant but not fully compatible with bash zsh | The default shell for Mac OS, very feature-rich but is incompatible with other shells in multiple ways Text Editors --------------------------------------------------------------------------------------------------------------------------------------------------------------------- nano <file> | Opens the nano editor to create or edit <file> | shortcuts and options are displayed at the bottom of the terminal vim <file> | Opens the vim editor to create or edit <file> | exit by typing ':q' in normal mode, ':wq' to save and exit, or ':q!' to exit without saving | enter insert mode (start typing) by pressing 'i', exit to normal mode with the 'Esc' key, and enter visual mode (selecting text) with 'v' | in normal mode, type 'dd' to delete the selected line, 'yy' to copy the line, 'P' to paste before cursor, and 'p' to paste clipboard after cursor | select text by starting visual mode in the first position, then moving the cursor to the second position, press 'y' to copy, or 'd' to delete, then 'p' to paste Basic Commands ##################################################################################################################################################################### Terminal Navigation -------------------------------------------------------------------------------------------------------------------------------------------------------------- ls -a | Lists all files and directories in working directory ls -l | Lists details of all files and directories in working directory ls -h | Lists all files and directories with with units (k, M, G, etc.) appended to the sizes for readability ls <directory> | Lists files and directories in <directory> ls *.<text> | Lists all .<text> files | e.g. "ls *.sh" lists all .sh files, such as 'startup.sh' or 'cleanfiles.sh' ls !(<text>) | Lists all files except <text> ls <text1> <text2> | Lists results for both <text1> and <text2> cd <directory> | Changes working directory to <directory> cd / | Changes working directory to root directory cd .. | Changes to parent of working directory cd - | Changes to previous working directory pwd | Prints the working directory history | Lists history of commands alias <text>=<command> | Sets an alias for <text> to function as <command> <text1>=<text2> | Sets variable '<text1>' to be '<text2>' | you can use '<text2>' in other commands by typing '$<text1>' man <command> | Shows the manual page for <command> less <file> | Shows the contents of <file> in an interactive, dynamic interface <command> | less | Shows the output of <command> in an interactive, dynamic interface echo <text> | Prints <text> System Information --------------------------------------------------------------------------------------------------------------------------------------------------------------- free | Lists information about system memory free -h | Lists information about system memory in a human readable format dmidecode -t memory | Lists information about physical memory lspci | Lists PCI and PCIe devices lspci -nnk | Lists PCI and PCIe devices with some detail and drivers lspci -vvv | Lists detailed information about PCI and PCIe devices lsusb | Lists USB devices lsusb -v | Lists detailed USB device information lsusb -vvt | Lists detailed USB device information as a tree lscpu | Lists CPU information uname -a | Lists system software information including: hostname, kernel, and architecture hostnamectl | Lists various system information neofetch | Lists various system information Time and Date -------------------------------------------------------------------------------------------------------------------------------------------------------------------- timedatectl | Prints information about current system time, time zone, and synchronization hwclock --show | Prints the current date and time from the hardware clock date | Prints the current system time and date date +%H | Prints the current hour with preceding zero date +%H:%M:%S | Prints the current hour, minute, and seconds timedatectl set-ntp true | Enables the synchronization of system time with an NTP server hwclock --systohc | Sets the hardware clock from the system clock timedatectl set-time 'YYYY-MM-DD hh:mm:ss' | Sets the system time and date date 'MMDDhhmmYYYY' | Sets the system time and date Installing Software ################################################################################################################################################################ Arch Linux (Using pacman) -------------------------------------------------------------------------------------------------------------------------------------------------------- pacman -S <package> | Installs <package> and any dependencies pacman -R <package> | Uninstalls <package> pacman -Rs <package> | Uninstalls <package> and any dependencies which are no longer required pacman -Qs <text> | Searches repositories and lists packages with <text> pacman -Syu | Updates all packages | 'S' synchronizes packages including all dependencies | 'y' refreshes package database | 'u' upgrades outdated packages yay -S <package> | Installs <package> from the AUR if available | you can install yay with "pacman -S --needed git base-devel && git clone https://aur.archlinux.org/yay.git && cd yay && makepkg -si" Debian and Derivatives (Using apt) ----------------------------------------------------------------------------------------------------------------------------------------------- apt install <package> | Installs <package> and any dependencies apt remove <package> | Uninstalls <package> apt autoremove | Uninstalls orphaned dependencies (packages no longer required) | best practice to run after "apt remove <package>" to clean any unused dependencies apt search <text> | Searches repositories and lists packages with <text> apt update | Updates repositories and checks for newer package versions apt upgrade | Upgrades current packages | best practice to first run "apt update" to get the latest packages apt full-upgrade | Upgrades the entire system (will remove currently installed packages if needed to update the system) From Source (Do Your Own Research) ----------------------------------------------------------------------------------------------------------------------------------------------- 1. | wget <address> | Downloads the source file (is usually .tar.gz, .tar.xz, .zip, or another compressed format) 2. | ls | Verify file name and existence 3. | tar -xf <archive> | Decompress the file | | if .zip: "unzip <text>.zip" 4. | ls | Check for a new directory (most likely has the same, or similar name to <archive>) 5. | cd <directory> | Change working directory to the new <directory> 6. | ./configure | Create a makefile 7. | make | Build the software package 8. | sudo make install | Install the package to the correct locations on the machine File Data ########################################################################################################################################################################## File Manipulation ---------------------------------------------------------------------------------------------------------------------------------------------------------------- cat <file> | Prints the content of <file> | append " | less" for better readability cat > <file> | Creates or overwrites <file>, each line you enter will be written to each line of <file> head -n <number> <file> | Prints the content of <file> at <number> lines from the top tail -n <number> <file> | Prints the content of <file> at <number> lines from the bottom tail -f <file> | Prints new data as <file> grows mkdir <directory> | Creates <directory> | e.g. "mkdir ../Downloads/plaintexts /mnt/tempstorage1/photos/" | "mkdir rich_texts" | "mkdir '~/My Photos/Tuesday/' " mkdir -p <directory> | Creates <directory> as well as any specified parent directories if required cp <file1> <file2> | Copies and renames <file1> to <file2> | e.g. "cp ~/Downloads/example.png /mnt/my_images/renamedexample.png" cp <file> <directory> | Copies <file> into <directory> cp -R <directory1> <directory2> | Copies and renames <directory1> to <directory2> cp <text>* <directory> | Copies all files starting with '<text>' to <directory> | e.g. "cp data-* ../data" copies all files starting with 'data-' (such as 'data-tuesday.csv') to the 'data' directory mv <file1> <file2> | Moves and renames <file1> to <file2> | e.g. "mv photo.jpg photo-wednesday.jpg" renames photo.jpg | "mv photo.jpg .." moves photo.jpg into parent directory | "mv /mnt/newusb/data.txt ~/Downloads/sortme.txt" moves and renames data.txt mv <file> <directory> | Moves <file> into <directory> mv <directory1> <directory2> | Moves <directory1> into <directory2> rm <file> | Deletes <file> (or multiple) | e.g. "rm data-tue* data-wed* *.svg" removes all files starting with 'data-tue' and all .svg files | "rm !(data-monday.txt)" removes all files except data-monday.txt rm -r <directory> | Deletes <directory> (or multiple) rm -i <file> | Asks for confirmation before deleting each file rm -f <file> | Forces the deletion of <file> shred <file> | Overwrites the data of <file> with random data shred -f <file> | Forces overwriting the data of <file> with random data shred -u <file> | Overwrites the data of <file> with random data and removes the file touch <file> | Updates the accessed and modified dates of <file> to the current time | <file> will be created if it does not exist ln <file1> <file2> | Creates <file2> as a hard link to <file1> | A hard link is a file which points to the same inode (the same data on the disk) ln -s <file1> <file2> | Creates <file2> as a symbolic link to <file1> | A symbolic link (or soft link) is a file which points to another file/directory ln -s <directory1> <directory2> | Creates <directory2> as a symbolic link to <directory1> File Searching ------------------------------------------------------------------------------------------------------------------------------------------------------------------- locate <text> | Searches all file names in a database for <text> | append " | less" for better readability locate -r /<text>$ | Searches all file names in a database for exact matches of <text> updatedb | Updates the database find <directory> -name "<text>" | Searches all file names in <directory> for "<text>" | e.g. "find ~ -name 'linux_ch*'" lists all files beginning with 'linux_ch' | "find ~ -name '*.png'" lists all .png files | append " | less" for better readability find <directory> -size +<number><k/M/G/T> | Searches <directory> for files larger than <number>, k/M/G/T are decimal prefixes find -name "<text>" -mtime -<number> | Searches <directory> for files named "<text>" modified less than <number> days ago find -type d | Searches working directory for directories Reading, Sorting, and Filtering Data --------------------------------------------------------------------------------------------------------------------------------------------- grep <text> <file> | Prints lines with <text> in <file> grep -i <text> <file> | Prints lines with <text> in <file> while ignoring capitalization grep -v <text> <file> | Prints lines without <text> in <file> grep -r <text> <directory> | Prints file names with <text> in <directory> grep -E "^<text>" <file> | Prints lines starting with <text> in <file> grep -E "<text>$" <file> | Prints lines ending with <text> in <file> grep -E "...<text>" <file> | Prints lines containing any 3 characters followed by <text> in <file> grep -E "[0-9]" <file> | Prints lines containing numbers in <file> grep -E "[a-zA-Z]" <file> | Prints lines containing alphabetical letters in <file> grep -E "^[A-Z].*\.$" <file> | Prints lines that start with a capital letter which is followed by any character, and the line ends with a period in <file> sort <file> | Prints the sorted content of <file> alphabetically sort -u <file> | Prints the sorted content of <file> alphabetically and removes duplicates sort -o <file1> <file2> | Prints the sorted content of <file2> alphabetically and outputs it to <file1> sort -r <file> | Prints the sorted content of <file> reverse alphabetically sort -R <file> | Prints the sorted content of <file> randomly uniq <file> | Prints the content of <file> without any duplicated adjacent lines diff <file1> <file2> | Displays the differences between <file1> and <file2> diff -c <file1> <file2> | Displays the differences between <file1> and <file2> with context for each difference diff -u <file1> <file2> | Displays the differences between <file1> and <file2> as one file wc <file> | Prints the number of lines, number of words, byte size, and file path | e.g. "wc Documents/plaintexts/*" lists all files in the plaintexts/ directory | "wc ~/Documents/data/data-friday.txt" cut -c <number1>-<number2> <file> | Prints everything between and including the <number1> character to <number2> character of each line sed 's/<text1>/<text2>/g' <file> | Prints the content of <file>, replacing every occurrence of <text1> with <text2> | 's' specifies the substitution operation | 'g' searches for all matches in each line sed '<number> s/<text1>/<text2>/g' <file> | Prints the content of <file>, replacing every occurrence of <text1> with <text2> on line <number> sed '<number1>,<number2> s/<text1>/<text2>/g' <file> | Prints the content of <file>, replacing every occurrence of <text1> with <text2> on lines <number1>-<number2> sed 's/<text1>/<text2>/<number>g' <file1> | Prints the content of <file>, replacing every occurrence of <text1> which is after and including the <number> occurrence in each line, with <text2> sed -n '<number1>,<number2>p' <file> | Prints lines <number1> to <number2> in <file> | 'p' prints the matched lines | '-n' makes it not print the full contents of <file> as well sed -n '/^<text>/p' <file> | Prints every line that starts with "<text>" in <file> sed -n -e '1~2p' -e '1~7p' <file> | Prints every 2nd line starting at 1, and every 7th line starting at 1 in <file> sed '<number1>,<number2>d' <file> | Deletes lines <number1>-<number2> in <file> sed '<number>,$d' <file> | Deletes lines from line <number> to the last line in <file> sed '/<text>/d' <file> | Deletes all lines with "<text>" in <file> Compressing and Extracting Data -------------------------------------------------------------------------------------------------------------------------------------------------- tar -zcf <text>.tar.gz <file> | Compresses <file> with gzip into the archive <text>.tar.gz | 'z' uses gzip, 'j' uses bzip2, 'J' uses xz | e.g. "tar -Jcf all_data.tar.xz data-monday.txt data-tuesday.txt data-wednesday.txt" tar -xf <archive> | Decompresses <archive> and outputs contents to working directory tar -tf <archive> | Lists contents of <archive> without decompressing it zcat <file> | View compressed file in the console (same as cat) zmore <file> | View compressed file in the console (same as more) zless <file> | View compressed file in the console (same as less) Mounting and Filesystems --------------------------------------------------------------------------------------------------------------------------------------------------------- du -h | Lists storage usage of directories, human readable sizes du -ah | Lists storage usage of files & directories, human readable sizes du -sh | Lists storage summary of usage of directories, human readable sizes lsblk | Lists block storage devices df | Lists file systems, their available space, and mount points df -h | Lists file systems, their available space with human readable sizes, and mount points fdisk -l | Lists all file systems in /proc/partitions fdisk <disk> | Creates and modifies partition tables on <disk> | in fdisk, enter 'm' for help, and 'q' to quit without saving mkfs.<text> <device> | When <text> is a valid filesystem, formats <device> with that filesystem | e.g. "mkfs.ext4 /dev/sda2" mount <device> <directory> | Mounts <device> to <directory> umount <device> | Unmounts <device> dd if=<text1>.txt of=<text2>.txt conv=ucase | Creates a copy of <text1>.txt with all uppercase letters named <text2>.txt dd if=<disk1> of=<disk2> | Clones <disk1> to <disk2>, <disk2> should be at least equal size to <disk1> dd if=/dev/zero of=<disk> | Overwrites all data on <disk> dd if=<text>.<iso/img> of=<disk> | Flashes <text>.<iso,img> to <disk> | use mkfs with appropriate filesystem on <disk> beforehand dd if=<disk> of=<text>.<iso/img> | Makes an image of <disk> Permissions ######################################################################################################################################################################## Users and Groups ----------------------------------------------------------------------------------------------------------------------------------------------------------------- useradd <text> | Creates a new user named <text>, an alternative command 'adduser' is made to be more user-friendly passwd <user> | Changes the password for <user> userdel <user> | Deletes <user>, an alternative command 'deluser' is made to be more user-friendly usermod -l <text> <user> | Changes the name of <user> to <text> groupadd <text> | Create a new user group named <text>, an alternative command 'addgroup' is made to be more user-friendly groupdel <group> | Deletes <group>, an alternative command 'delgroup' is made to be more user-friendly usermod -aG <group> <user> | Adds <user> to <group> File Permissions ----------------------------------------------------------------------------------------------------------------------------------------------------------------- chown <user> <file> | Sets ownership of <file> to <user> chown <user>:<group> <file> | Sets user ownership of <file> to <user> and group ownership to <group> chown -R <user>:<group> <directory> | Sets user ownership of <directory> and all subdirectories to <user> and group ownership to <group> chmod <u/g/o/a><+/-/=><r/w/x> <file> | Modifies permissions of <file> | 'u' is user, 'g' is group, 'o' is other, 'a' is all | '+' adds a right, '-' removes a right, '=' sets a right | 'r' is read permission, 'w' is write permission, 'x' allows execution | e.g. "chmod a+x startup.sh" makes startup.sh executable by anyone | "chmod ug+rwx data.txt" gives full permissions of data.txt to user and group chmod <number1><number2><number3> <file> | Modifies permissions of <file> | <number> can be any whole number from 0-7 | 'r'=4, 'w'=2, 'x'=1 | <number1> is user, <number2> is group, <number3> is other | e.g. 'rwx'='4+2+1'=7 | 'rx'='4+1'=5 | "chmod 777 startup.sh" gives everyone read, write, and execute permissions | "chmod 664 photo.jpg" gives user and group read and write permissions, and read permission to others Network ############################################################################################################################################################################ Local Area Network --------------------------------------------------------------------------------------------------------------------------------------------------------------- ip <a/addr> | Displays information about network interfaces nmcli | Displays status and configurations of network interfaces nmcli device wifi list | Lists available Wi-Fi access points nmcli device wifi connect <text1> password <text2> | Connect to SSID <text1> with the password <text2> nmcli --ask device wifi connect <text> | Connect to open security SSID <text> nmcli connection edit type ethernet | Opens an interactive menu to manage ethernet connections nmtui | Displays an interactive TUI menu to configure network interfaces ufw status | Displays open ports on the local system ufw <enable/disable> | Enables and disables the ufw firewall ufw allow <number> | Opens port <number> ufw deny <number> | Closes port <number> Internet ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ping <address> | Pings <address> ping -c <number> <address> | Pings <address> <number> times ping -i <number> <address> | Pings <address> every <number> seconds traceroute <address> | Traces the route of packets sent to <address> tracepath <address> | Traces the path of packets sent to <address> (very similar to traceroute) dig <address> | When <address> is a URL, it queries DNS and prints the response as well as other information nslookup <address> | When <address> is a domain, it prints the IP address, when <address> is an IP address, it prints the domain name netstat -a | Lists all connected and listening sockets | append " | less" for better readability Data Over Internet --------------------------------------------------------------------------------------------------------------------------------------------------------------- wget <address> | Downloads the content content <address> | e.g. "wget google.com" downloads 'index.html' from 'google.com' | "wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.17.2.tar.xz" downloads the compressed Linux 4.17.2 kernel curl <address> | Prints the content at <address> curl -O <address> | Downloads the content at <address> with the remote filename curl -o <text> <address> | Downloads the content at <address> with the filename of <text> ssh <address> | Connects to SSH server at <address>, allows you to interact with the host remotely ssh <user>@<address> | Connects to SSH server at <address> as <user> ssh <address> -p <number> | Connects to SSH server at <address> over port <number> ssh-keygen -t rsa | Creates an SSH key pair in '~/.ssh', run this command on the client system ssh-copy-id <address> | Copies the public key to <address> (the server), run this command on the client system sftp <user>@<address> | Connects to SFTP server at <address> as <user>, allows you to securely transfer files between machines | in sftp, enter 'help' or '?' for help | enter "get <file>" to download a file on the server | enter "put <file> <directory>" to upload local <file> to remote <directory> | enter "rm <file>" to delete <file> on the server | enter "rmdir <directory>" to delete <directory> on the server | enter "mkdir <directory>" to create <directory> on the server scp <source> <user>@<address>:<dest> | Copies <source> on local machine to <dest> at <address> scp <user>@<address>:<source> <dest> | Copies <source> from <address> to <dest> on local machine rsync <source> <user>@<address>:<dest> | Copies <source> on local machine to <dest> at <address> rsync <user>@<address>:<source> <dest> | Copies <source> from <address> to <dest> on local machine rsync -P <user>@<address>:<source> <dest> | Copies <source> from <address> to <dest> on local machine and allows resuming the command after interruption w3m <address> | Opens a TUI internet browser and loads <address>, not included on most distributions Programs ########################################################################################################################################################################### Scheduling Programs -------------------------------------------------------------------------------------------------------------------------------------------------------------- watch <command> | Executes <command> every 2 seconds and displays the output watch -n <number> <command> | Executes <command> every <number> seconds (up to 1 decimal place) and displays the output at <time> | Allows you to enter commands which will execute on <time> atq | Lists pending commands atrm <number> | Removes pending command <number> sleep <number> | Pauses for <number> seconds | <number> can be appended with s, m, h, or d to specify units crontab -e | Creates or edits the current user's crontab entry | in crontab, valid format is "<m> <h> <DD> <MM< <D> <command>" | replace units with '*' for any | e.g. "* * * * * <command>" executes every minute | "00 15 1,10,15,18 * * <command>" executes on days 1, 10, 15, and 18 each month at 3:00pm | "45 17 1-21 * * <command>" executes every day at 5:45pm on days 1-21 each month | "0 */2 * * * <command>" executes every 2 hours | "/30 9-17 * * 1-5 <command>" executes every 30 minutes, from 9am-5pm on Monday to Friday crontab -l | Lists the current user's crontab entry crontab -r | Deletes the current user's crontab entry Background Processes ------------------------------------------------------------------------------------------------------------------------------------------------------------- <command> & | Runs <command> in the background nohup <command> | Runs <command> and continues running when terminal is closed jobs | Lists programs running in the background fg <number> | Makes program <number> run in the foreground bg <number> | Makes program <number> run in the background Process Management --------------------------------------------------------------------------------------------------------------------------------------------------------------- w | Lists current users and what they are doing top | Shows a dynamic list of running processes and system resources | 'q' to quit, 'h' for help, 'k' to kill a process htop | Shows an interactive dynamic list of running processes and system resources with color ps | Lists currently running processes ps -ef | Lists all currently running processes | append " | less" for better readability ps -ejH | Lists process tree kill <number> | Gracefully kill process with <number> process ID (sends signal to process to exit) | identical to "kill -15 <number>" | can replace <number> with process name kill -9 <number> | Kills process with <number> process ID (requests the OS to immediately cease process) killall <text> | Kills multiple processes by name, matching "<text>" systemctl enable <text>.service | Enables the <text>.service systemd unit systemctl start <text>.service | Starts the <text>.service systemd unit systemctl stop <text>.service | Stops the <text>.service systemd unit systemctl status <text>.service | Displays information and the most recent logs of the <text>.service systemd unit journalctl -u <text>.service | Prints log entries of the <text>.service systemd unit journalctl -f <text>.service | Prints the most recent log entries of the <text>.service systemd unit and continuously prints new entries halt | Halts the machine shutdown -H | Halts the machine poweroff | Shuts down the machine shutdown -P | Shuts down the machine reboot | Restarts the machine shutdown -r | Restarts the machine Managing Commands ################################################################################################################################################################## Flow Redirection ----------------------------------------------------------------------------------------------------------------------------------------------------------------- <command> > <file> | Standard output of <command> is redirected to <file> | e.g. "ps -ejH > output.txt" | "cat > data.txt" <command> 2> <file> | Error output of <command> is redirected to <file> <command> &> <file> | Standard and error output of <command> is redirected to <file> | e.g. "&> /dev/null" can be used to silence the output of a command <command> >> <file> | Standard output of <command> is redirected and appended to <file> | e.g. "ls -a notadir/ >> data.txt 2>> errors.log" <command> 2>> <file> | Error output of <command> is redirected and appended to <file> <command> &>> <file> | Standard and error output of <command> is redirected and appended to <file> <command> < <file> | Inputs <file> to <command> <command> << "<text>" | <command> stops at <text> | e.g. "cat > <file> << endhere" will not write the entered lines until you enter 'endhere' on a new line <command> <<< "<text>" | Inputs <text> to <command> Chaining Commands ---------------------------------------------------------------------------------------------------------------------------------------------------------------- <command1> ; <command2> | Executes <command1> then executes <command2> <command1> | <command2> | Executes <command1> and redirects the output to <command2> <command1> && <command2> | Executes <command1> and executes <command2> if <command1> was successful <command1> || <command2> | Executes <command1> and executes <command2> if <command1> failed <command1> && { <command2> ; <command3> ; } | Executes <command1> and executes both <command2> then <command3> if <command1> was successful <command1> && <command2> || <command3> | Executes <command1> and executes <command2> if <command1> was successful, or <command3> if <command1> failed Executing and Scripting ---------------------------------------------------------------------------------------------------------------------------------------------------------- <file> | Executes <file> (assuming <file> has correct executable permission) | e.g. "~/Downloads/install.sh" runs 'install.sh' | "./uninstall.sh" runs the 'uninstall.sh' file in the working directory | "/usr/bin/pwd" functions the same as 'pwd' | "/usr/bin/man ls" functions the same as 'man ls' script.sh | This is a file named 'script.sh', with the contents of each line after each '>' character >echo "Here is the time:" | each newline character is interpreted the same as ';', it will execute each command one after the other >date +%H:%M:%S | e.g. 'date' followed by "echo End" on a new line functions the same as "date; echo End" >echo "That is the time." | the script exits after the last command >echo "Goodbye." easystart.sh | This script is named 'easystart.sh', it should include '&&' between every command to stop on failure, but works as an example >mkdir ./my-program | first, it creates a 'my-program/' directory in the working directory >cd ./my-program | second, it changes the working directory to 'my-program/' >wget example.com/someprogram && ./bashinstall.sh | third, it downloads something from 'example.com/program', and if successful, executes 'bashinstall.sh' >./myprogram | fourth, it executes 'myprogram' bashinstall.sh | This script is specifically a bash script, which uses a shebang (on the first line) to be run with bash >#!/bin/bash | indicates that the script will be executed with bash >InstallDir='/usr/local/bin/myapp' | sets the 'InstallDir' variable to a specific directory >mkdir $InstallDir && echo "Created app directory" | creates the specified installation directory and tells the user that it made a directory >pacman -S somedependency --noconfirm | installs a dependency without any confirmations >cd $InstallDir && wget mydomain.com/app | changes working directory to $InstallDir and downloads an app >./install && echo "Installed app" | runs an install script and tells the user the app was installed >systemctl enable app && systemctl restart app | enables and starts the app as a service >echo "Success!" | tells the user "Success!" >sleep 2 | pauses for 2 seconds before exiting License Information ################################################################################################################################################################ This document is a derivative work of the "Linux Bash Shell Cheat Sheet" PDF by Raph D. from 2011: https://web.archive.org/web/20111018212827/http://dl.dropbox.com/u/397277/bash_shell_cheat_sheetV2.pdf The original document was published on their blog (archived) here: https://web.archive.org/web/20120106085933/http://freeworld.posterous.com/ An available copy of the original can be found here: https://oit.ua.edu/wp-content/uploads/2020/12/Linux_bash_cheat_sheet-1.pdf Linux Cheat Sheet copyright 2024 is licensed under CC BY-NC-SA 4.0. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/ More resources