Bash-Cheat-Sheet

0
README.md

Bash Cheat Sheet

A cheat sheet for bash commands.

Command History

Navigating Directories

Creating Directories

Moving Directories

Deleting Directories

Creating Files

Standard Output, Standard Error and Standard Input

Moving Files

Deleting Files

Reading Files

File Permissions

#PermissionrwxBinary
7read, write and executerwx111
6read and writerw-110
5read and executer-x101
4read onlyr--100
3write and execute-wx011
2write only-w-010
1execute only--x001
0none---000

For a directory, execute means you can enter a directory.

UserGroupOthersDescription
644User can read and write, everyone else can read (Default file permissions)
755User can read, write and execute, everyone else can read and execute (Default directory permissions)
  • u - User
  • g - Group
  • o - Others
  • a - All of the above

Finding Files

Find binary files for a command.

locate
uses an index and is fast.

find
doesn't use an index and is slow.

Find in Files

Replace in Files

Compressing Files

zip

Compresses one or more files into *.zip files.

gzip

Compresses a single file into *.gz files.

tar -c

Compresses (optionally) and combines one or more files into a single *.tar, *.tar.gz, *.tpz or *.tgz file.

Decompressing Files

unzip

gunzip

tar -x

Disk Usage

Memory Usage

Packages

Shutdown and Reboot

Identifying Processes

Process Priority

Process priorities go from -20 (highest) to 19 (lowest).

Killing Processes

Date & Time

Scheduled Tasks

HTTP Requests

Network Troubleshooting

DNS

Hardware

Terminal Multiplexers

Start multiple terminal sessions. Active sessions persist reboots.

tmux
is more modern than
screen
.

Secure Shell Protocol (SSH)

Set default user and port in

~/.ssh/config
, so you can just enter the name next time:

Secure Copy

Bash Profile

  • bash -
    .bashrc
  • zsh -
    .zshrc

Bash Script

Variables

Environment Variables

Functions

Exit Codes

Conditional Statements

Boolean Operators

  • $foo
    - Is true
  • !$foo
    - Is false

Numeric Operators

  • -eq
    - Equals
  • -ne
    - Not equals
  • -gt
    - Greater than
  • -ge
    - Greater than or equal to
  • -lt
    - Less than
  • -le
    - Less than or equal to
  • -e
    foo.txt - Check file exists
  • -z
    foo - Check if variable exists

String Operators

  • =
    - Equals
  • ==
    - Equals
  • -z
    - Is null
  • -n
    - Is not null
  • <
    - Is less than in ASCII alphabetical order
  • >
    - Is greater than in ASCII alphabetical order

If Statements

Inline If Statements

While Loops

For Loops

Case Statements