Linux terminal cheat sheet
  • Access

  • Set privileges recursively: $ chmod -R 755 dir
  • Set privileges on a file: $ chmod 644 file
  • Set ownership on a file: $ chown username:group file
  • Set user on a file: $ chown username file
  • Set group on a file: $ chown :group file
  • Set ownership recursively: $ chown -R username:group directory
  • Apache

  • Start Apache: $ service apache2 start
  • Restart Apache: $ service apache2 restart
  • Crud

  • Create a file: $ touch file_name
  • Copy file to directory: $ cp file to_dir/
  • Rename file: $ mv original_name new_name
  • Move file to directory: $ mv file_name some_dir/file_name
  • Delete file: $ rm file_name
  • Clear file content w/o deleting: $ truncate -s 0 file_name
  • Create a directory: $ mkdir dir_name/
  • Recursive make dir: $ mkdir -p dir_name/
  • Copy directory A to directory B: $ cp -r dir_A/ dir_B/
  • Move directory A to directory B: $ mv dir_A/ dir_B/
  • Move the content of directory A to directory B: $ mv -v dir_A/* dir_B/
  • Compare directories, only showing the names of files that differ: $ diff -rq dir_A dir_B
  • Delete directory recursively: $ rm -r dir_name
  • Force directory deletion: $ rm -rf dir_name
  • Delete directories older than x days: $ find dir_name/* -mtime +x -exec rm -rf {} \;
  • Download file to dir: $ wget -P dir-name https://example.com/file-name
  • Open directory in the file manager: $ xdg-open dir-name/
  • Edit

  • Show line numbers: :set number
  • Move to INSERT mode; put cursor before: i
  • Move to INSERT mode; put cursor after: a
  • Move to INSERT mode; put cursor in a new line below: o
  • Move to INSERT mode; put cursor at the start of line: Shift + i
  • Move to INSERT mode; put cursor at the end of line: Shift + a
  • Move to INSERT mode; put cursor in a new line above: Shift + o
  • Move to COMMAND mode: ESC
  • Undo last operation: u
  • Go to the first line of the file: gg
  • Go to line 3 of the file: 3gg
  • Go to the last line of the file: G
  • Move 3 lines up: 3 + ↑
  • Move 5 lines down: 5 + ↓
  • Move 6 places right: 6 + →
  • Move 2 places back: 2 + ←
  • Go to the start of the line: 0
  • Go to the end of line: $
  • Search for pattern: /{pattern}
  • Search backward for pattern: ?{pattern}
  • Repeat search in same direction: n
  • Repeat search in opposite direction: N
  • Copy current line: yy
  • Copy everything inside double quotes: yi"
  • Copy everything inside parentheses: yi(
  • Copy everything inside brackets: yi[
  • Paste after cursor: p
  • Replace the first match in each line: :%s/old/new
  • Replace all with confirmation: :%s/old/new/gc
  • Delete the character under the cursor: x
  • Delete current line: dd
  • Delete lines 8 to 10: :8,10d
  • Swap the current line with the line below it: ddp
  • Delete all lines containing pattern: :g/{pattern}/d
  • Delete all lines not containing pattern: :g!/{pattern}/d
  • Delete a double-quoted string, including the quotes: da"
  • Delete everything in parentheses, including them: da(
  • Delete everything in brackets, including them: da[
  • Save file: :w
  • Quit if no changes were made: :q
  • Quit file without saving: :q!
  • Save file and quit: :wq
  • Install

  • Install package (yum): $ yum install package_name
  • Show package data (yum): $ yum info package_name
  • Uninstall package: $ yum remove package_name
  • Install a specific package: $ apt-get install package_name
  • Install multiple packages: $ apt-get install package1 package2
  • Update the package list: $ apt update
  • Upgrade all installed packages: $ apt-get upgrade
  • Update and upgrade without asking: $ sudo apt-get update && sudo apt-get upgrade --yes
  • Update; upgrade (even if update fails): $ sudo apt-get update; sudo apt-get upgrade
  • Upgrade specific package: $ apt-get upgrade package_name
  • Clean your system: $ apt-get autoremove
  • Uninstall package (apt): $ apt remove --assume-yes package_name
  • Remove PPA: $ add-apt-repository --remove ppa:[ppa_name]
  • Keyboard shortcuts

  • Open terminal: Ctrl+Alt+T
  • Tab completion: Tab (press twice to list all options)
  • Switch windows: Alt+Tab
  • Switch languages: Super+Space
  • Previous commands: up arrow
  • Recent commands: Ctrl+R
  • Kill process: Ctrl+C
  • Exit session: Ctrl+D
  • Copy: Ctrl+Shift+C
  • Clear the terminal: Ctrl+L
  • Repeat last command: !!
  • Suspend process: Ctrl+Z
  • Paste: Ctrl+Shift+V
  • Hide the history: Ctrl+L
  • List

  • Detailed list: $ ls -l
  • Including hidden: $ ls -a
  • List by time: $ ls -t
  • Matrix: $ cmatrix
  • Put anything in a cow's mouth: $ echo "whatever ..." | cowsay
  • List of characters other than cows: $ cowsay -l
  • Put words in the mouth of a different character: $ echo "whatever ..." | cowsay -f daemon
  • ASCII text banner: $ figlet "your text here"
  • Network

  • Ping a host (domain or IP): $ ping example.com
  • Get whois information for domain: $ whois domain
  • Connect a remote server: $ ssh -l domain-user example.com
  • Process

  • Show active processes: $ ps
  • Show the hierarchy of the commands: $ ps f
  • Show jobs status: $ jobs
  • Bring a job into the foreground: $ fg %n
  • Send a job into the background: $ bg %n
  • Show the top processes: $ top
  • Monitor all traffic on HTTP (port 80): $ tcpdump -i eth0 'port 80'
  • Kill process safely by pid: $ kill pid
  • Dirty killing (if nothing else works): $ kill -9 pid
  • Suspend a process (like Ctrl+Z): $ kill -TSTP pid
  • Restart a suspended process: $ kill -CONT pid
  • Kill all processes by pattern: $ pkill -f pattern
  • Read

  • Read a file: $ cat filename
  • Read the first N lines of a file: $ head -n N filename
  • Read while showing the line numbers: $ nl filename
  • Read in reverse order: $ tac filename
  • Count lines, words, characters: $ wc filename
  • Show the first lines: $ head filename
  • Show the last lines: $ tail filename
  • Show lines matching pattern: $ cat filename | grep {pattern}
  • Watch the last lines as they update: $ tail -f filename
  • Put in alphabetical order: $ sort filename
  • Remove duplicate lines: $ sort filename | uniq
  • Read a file with pagination: $ less filename
  • System

  • Install a Debian package: $ sudo dpkg -i [package.deb]
  • Remove Debian package: $ sudo dpkg -r [package]
  • Check os version: $ lsb_release -a
  • Show current date and time: $ date
  • Show current uptime: $ uptime
  • Show memory usage: $ free -h
  • Check available disk space: $ df -h
  • How much space a file or directory takes: $ du -sh path
  • DNS name of the machine: $ hostname
  • Show the IP address: $ hostname -i
  • Show command info: $ man command-name
  • Show the latest commands: $ history
  • Show Linux system data: $ uname -a
  • Show who is online: $ w
  • Who am I logged in as: $ whoami
  • Terminal

  • Run command in exactly the same way you last ran it: $ ![command_name]
  • Rerun the last command: $ !!
  • Rerun last command with privileges: $ sudo !!
  • Show a list of the latest commands used: $ history
  • Run command number xxx from the history: $ !xxx
  • Search in history: Ctrl+R
  • Wipe the history: $ history -c
  • Clear the screen (history is kept): Ctrl+L
  • Decrease font size: Ctrl+Minus(-)
  • Increase font size: Ctrl+Plus(+)
  • Move cursor to the beginning of the line: Ctrl+A
  • Delete from the cursor to the end of the line: Ctrl+K
  • Move cursor to the end of the line: Ctrl+E
  • Full screen (IN & OUT): F11
  • Run command A then B: $ [command_A] && [command_B]
  • Run command A then B (independently): $ [command_A]; [command_B]
  • Zip

  • Archive to tar: $ tar -cvf archive.tar dir_to_archive/
  • Extract from tar: $ tar -xvf archive.tar
  • Compress to gzip: $ tar -czvf archive.tar.gz dir_to_compress/
  • Extract from gzip: $ tar -xzvf archive.tar.gz
  • Archive to zip: $ zip -r archive.zip dir_to_archive/
  • Extract from zip: $ unzip archive.zip