Skip to content

1.1 Bash Cheat Sheet

Antoine Dangeard edited this page Jan 6, 2024 · 1 revision

Bash

File and Folder Navigation

  • pwd: Print the current working directory.
  • ls: List files and directories in the current directory.
  • ls -l: Detailed list with file permissions and sizes.
  • ls -a: Show hidden files.
  • cd: Change directory.
  • cd ..: Move up one directory.
  • cd ~: Navigate to the home directory.
  • cd /path/to/directory: Navigate to a directory using absolute path.
  • cd path/to/directory: Navigate to a directory using relative path.
  • mkdir directoryname: Create a new directory.

Viewing and Editing Files with Vim

  • vim <filename>: Open a file in the Vim text editor.
  • Press i to enter insert mode (for editing).
  • Press Esc to exit insert mode.
  • To save changes and exit: :wq while not in insert mode.
  • To exit without saving: :q! while not in insert mode.

File Permissions (chmod)

  • chmod permissions filename: Change file permissions.
  • Example: chmod +x script.sh makes a script executable.

Environment Variables

  • echo $VARIABLE: Display the value of an environment variable.
  • export VARIABLE=value: Set an environment variable.
  • unset VARIABLE: Remove an environment variable.

.bashrc (Bash Run Commands)

  • .bashrc is a script executed every time you start a new Bash session (i.e. a new terminal tab).
  • Useful to run scripts that set environment variables for new terminals.

Searching with grep and Piping

  • grep pattern filename: Search for a regex pattern in a file.
  • Example: grep "error" log.txt searches for "error" in log.txt.
  • Piping (|): Send the output of one command as input to another.
  • Example: ls | grep "file" only lists files containing "file" in their names.

Wildcards (for pattern matching)

  • *: Matches any characters (e.g., *.txt matches all .txt files).
  • ?: Matches a single character.