Skip to content

Instantly share code, notes, and snippets.

@andynameistaken
Last active January 2, 2026 23:37
Show Gist options
  • Select an option

  • Save andynameistaken/befb889c2d60edc4b8be3f16106c50dc to your computer and use it in GitHub Desktop.

Select an option

Save andynameistaken/befb889c2d60edc4b8be3f16106c50dc to your computer and use it in GitHub Desktop.
Bash

Sudo

  • sudo -s

    • Starts a shell with the target user’s privileges (root by default) but keeps your current environment variables.
    • Uses your current shell ($SHELL) instead of the target user’s login shell.
    • Good when you only need elevated permissions without changing the overall environment context.
  • sudo -i

    • Simulates a full root login: runs the target user’s login shell (passwd), reads that user’s profile files (profile, ~/.profile, etc.), and sets up their environment.
    • Essentially gives you a fresh login session as root, including home directory changes.
    • Ideal when you need the complete root environment (e.g., for system-level configuration that depends on root’s PATH or dotfiles).

Practical Guidance

  • Need just a quick root privilege bump inside your current shell context? Use sudo -s.
  • Need the full root experience with their shell, home, and environment? Use sudo -i.

File Search

# Basic search for "text" in a file
grep "text" filename.txt

# Case-insensitive search for "text" in a file
grep -i "text" filename.txt

# Recursive search for "text" in all files under the current directory
grep -r "text" .

# Recursive and case-insensitive search for "text"
grep -ri "text" .

# Count the number of lines that contain "text" in a file
grep -c "text" filename.txt

# Display line numbers while searching for "text" in a file
grep -n "text" filename.txt

# Search for lines that do NOT contain "text" in a file
grep -v "text" filename.txt

# Use extended regular expressions (ERE) with grep (e.g., search for "text" or "string")
grep -E "text|string" filename.txt

# Search for a whole word, not part of a word
grep -w "text" filename.txt

# Display only the names of files (with paths) that contain "text", without showing the matching lines
grep -rl "text" /path/to/search

# Exclude files matching a pattern (e.g., exclude all .log files)
grep --exclude=*.log -r "text" .

# Include files matching a specific pattern (e.g., search only .txt files)
grep --include=\*.txt -r "text" .

# Search for "text" in all files, but exclude searching in directories named "cache"
grep -r --exclude-dir=cache "text" .

# Use grep with output from another command (search for "text" in the list of running processes)
ps aux | grep "text"

# Search for a pattern that starts with a hyphen (-) by using -- before the pattern
grep -- "-text" filename.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment