==================== LINUX CLI CHEATSHEET ==================== ---------------------------------------- BASIC NAVIGATION ---------------------------------------- pwd Print Working Directory - shows where you are. ls List files in current directory. ls -l Long format (permissions, owner, size, date). ls -a Show ALL files, including dotfiles (.bashrc, .git, etc.). ls -lah l = long, a = all, h = human file sizes (KB/MB/GB). cd /path/to/dir Change directory to /path/to/dir. cd .. Go up one directory. cd ~ Go to your home directory. cd - Go back to the previous directory (like a back button). ---------------------------------------- WORKING WITH FILES AND DIRECTORIES ---------------------------------------- mkdir NAME Make a directory called NAME. mkdir -p path/to/nested/dir Make nested directories; "-p" creates parents as needed. touch file.txt Create an empty file (or update its timestamp). cp source dest Copy file from source to dest. cp -r dir1 dir2 Copy directory dir1 to dir2 (recursive). mv old new Move or rename a file or directory. rm file.txt Remove file.txt. rm -r dir Recursively delete directory "dir" and its contents. DANGEROUS: there is no recycle bin. rm -rf dir r = recursive, f = force (no prompts). VERY DANGEROUS - double-check before running. cat file.txt Print the file to the terminal. less file.txt View file one screen at a time, scroll with arrows, q to quit. head file.txt Show first 10 lines. tail file.txt Show last 10 lines. tail -f logfile.log Follow a log file as it grows. Ctrl+C to stop. ---------------------------------------- GLOBBING / WILDCARDS (PATTERNS) ---------------------------------------- * Matches any number of characters (including none) Example: *.php -> all .php files ? Matches exactly one character Example: file?.txt -> file1.txt, fileA.txt, but NOT file10.txt [abc] Matches one character in the set Example: file[12].txt -> file1.txt, file2.txt [0-9] Range: any digit Example: file[0-9].txt -> file0.txt ... file9.txt ---------------------------------------- REDIRECTION AND PIPES (|, >, >>, 2>&1) ---------------------------------------- command > file Redirect STDOUT (normal output) to a file (overwrite). command >> file Append STDOUT to the file (do not overwrite). command < file Use "file" as STDIN (input) to the command. command 2> errors.log Redirect STDERR (error output) to errors.log. command > output.log 2>&1 Redirect BOTH normal output and errors to output.log. Breakdown of "2>&1": 2 = file descriptor 2 (STDERR) > = redirect &1 = to the same place as 1 (STDOUT) command1 | command2 Take output of command1 and feed it as input to command2. Example: ps aux | grep apache2 ---------------------------------------- PROCESSES AND SYSTEM INFO ---------------------------------------- ps aux Show all processes. ps aux | grep NAME Find processes with NAME in their line. top Live view of CPU and memory usage and processes. (q to quit) htop (if installed) Nicer version of top with colors and better UI. btop (if installed) System monitor with CPU, RAM, disks, etc. kill PID Ask process PID to terminate (polite). kill -9 PID Force kill (last resort). df -h Disk free space (human readable). du -sh * Show size of directories/files in current folder. free -h Memory usage (human readable). uptime How long system has been running and load averages. ---------------------------------------- HANDLING SYSTEM FREEZES (TTY, GNOME SHELL, SAFE REBOOT) ---------------------------------------- Ctrl+Alt+F3 (or F4, F5) Switch from frozen desktop (GUI) to a text console (TTY). Log in with your user (jcc) and password. Ctrl+Alt+F1 or Ctrl+Alt+F2 Switch back to the graphical desktop (GNOME) from a TTY. (Exact key can vary; try F1 then F2.) # Find what is misbehaving top Basic live process view (CPU%, MEM%, etc.). htop Nicer process view (if installed). Look for processes pegging CPU or eating tons of RAM. # Kill an offending GUI app from TTY pkill soffice.bin Kill LibreOffice (Calc/Writer/etc.) if it has frozen the desktop. killall -9 PROGRAM Force kill a specific program by name. Example: killall -9 chrome # Restart GNOME Shell without reboot (Xorg sessions) # (Run this from a TTY when the desktop is frozen.) export DISPLAY=:0 gnome-shell --replace & Restart the GNOME Shell compositor. On Xorg this can revive a frozen desktop without reboot. If on Wayland, you usually need to log out / back in instead. # Clean reboot from TTY sudo reboot Reboot the system cleanly (preferred over holding power button). sudo poweroff Power the system down cleanly. # Emergency "magic" reboot when EVERYTHING is stuck (REISUB) Alt+SysRq+R E I S U B Hold Alt + SysRq (Print Screen), then slowly press: R - take keyboard control back E - send TERM to processes I - send KILL to remaining processes S - sync disks U - remount disks read-only B - reboot Safer than just yanking power, but you may still lose unsaved work. ---------------------------------------- USER AND PERMISSIONS (chmod, chown) ---------------------------------------- whoami Show current user. id Show your user id, group id, etc. sudo command Run command as root (superuser). WARNING: you can break things; think before running. chmod 755 file Set permissions (rwxr-xr-x). 7 = read + write + execute 5 = read + execute chmod +x script.sh Make script executable. chown user:group file Change owner and group of a file. ---------------------------------------- APT PACKAGE MANAGEMENT (Ubuntu / Pop!_OS / Raspberry Pi OS) ---------------------------------------- sudo apt update Refresh package lists. sudo apt upgrade Upgrade installed packages. sudo apt install package Install a package. sudo apt remove package Remove a package. sudo apt autoremove Remove unused dependencies. ---------------------------------------- SSH AND SERVER MANAGEMENT ---------------------------------------- ssh user@host Connect to remote machine (host can be IP or domain). ssh -p PORT user@host Connect using a non-default port. scp file user@host:/path Copy a file to a remote host (over SSH). scp user@host:/path/file . Copy file from remote host to current directory. sudo systemctl status service Show status of a service (example: apache2, ssh). sudo systemctl restart service Restart a service. sudo systemctl stop service Stop a service. sudo systemctl start service Start a service. journalctl -u servicename View logs for a specific service. journalctl -u servicename -f Follow logs in real time (like tail -f). ---------------------------------------- GREP / RIPGREP (SEARCHING INSIDE FILES) ---------------------------------------- grep "text" file Search for "text" inside file. grep -R "text" /path Recursive search for "text" under /path. grep -R --line-number "text" /path Show line numbers too. rg "text" If ripgrep (rg) is installed: Fast recursive search for "text" from current directory. rg "text" path/ Search only under "path/". ---------------------------------------- FIND / FD-FIND (SEARCH FOR FILES) ---------------------------------------- find . -name "*.php" Find all .php files under current directory. find /var/www -type f -name "index.*" Find all regular files named index.* under /var/www. fd pattern If fd-find (fd) is installed: Simpler, faster file search. Examples: fd index . Find "index" in current tree. fd ".php$" Find PHP files by regex. ---------------------------------------- GIT BASICS (FOR YOUR TIMEcard PROJECT) ---------------------------------------- git status Show changed files, branch, etc. git add file Stage a specific file. git add . Stage all changes in current directory and below. git commit -m "message" Create a commit with message. git push Push commits to remote repository. git pull Fetch and merge latest changes from remote. git log --oneline --graph --decorate Pretty log view with branches. ---------------------------------------- BASH HISTORY AND SHORTCUTS (!, !$, !!, etc.) ---------------------------------------- Up Arrow / Down Arrow Scroll through command history. Ctrl+R Reverse search through history. Start typing part of a previous command and it finds matches. history Print your command history with numbers. !! (double bang) Repeat the ENTIRE previous command. Example: apt install somepackage (fails with permission) sudo !! (runs: sudo apt install somepackage) Breakdown: ! = pull from history !! = previous command again !$ (bang-dollar) Use LAST ARGUMENT of previous command. Example: sudo mkdir MyNewFolder cd !$ What actually runs: cd MyNewFolder Breakdown: ! = pull from history $ = the last argument !* (bang-star) Use ALL arguments of previous command. Example: cp file1.txt file2.txt backup/ ls !* What actually runs: ls file1.txt file2.txt backup/ !string Run the most recent command that starts with "string". Example: systemctl restart apache2 ... !sys This re-runs "systemctl restart apache2". !?string? Run the most recent command that CONTAINS "string". Example: sudo nano /etc/apache2/sites-available/000-default.conf ... !?apache2? Runs that nano command again. Alt+. Insert the last argument from the previous command into the current prompt. Example: sudo mkdir some-long-folder-name cd Becomes: cd some-long-folder-name Press Alt+. repeatedly to cycle through last args from earlier commands. ---------------------------------------- COMMON CONTROL KEYS (TERMINAL) ---------------------------------------- Ctrl+C Send interrupt signal to the running process (stop it). Ctrl+Z Suspend the current process (background it). Then you can use "fg" to bring it back. fg Bring the last suspended job to the foreground. bg Continue the last suspended job in the background. Ctrl+D End-of-input (log out of a shell, or end input to a program). Tab Auto-complete file and command names. Double Tab to show possible completions.