Bash Cheat Sheet

A cheat sheet for bash commands.

Command History

!!            # Run the last command

touch foo.sh
chmod +x !$   # !$ is the last argument of the last command i.e. foo.sh
pwd                       # Print current directory path
ls                        # List directories
ls -a|--all               # List directories including hidden
ls -l                     # List directories in long form
ls -l -h|--human-readable # List directories in long form with human readable sizes
ls -t                     # List directories by modification time, newest first
stat foo.txt              # List size, created and modified timestamps for a file
stat foo                  # List size, created and modified timestamps for a directory
tree                      # List directory and file tree
tree -a                   # List directory and file tree including hidden
tree -d                   # List directory tree
cd foo                    # Go to foo sub-directory
cd                        # Go to home directory
cd ~                      # Go to home directory
cd -                      # Go to last directory
pushd foo                 # Go to foo sub-directory and add previous directory to stack
popd                      # Go back to directory in stack saved by `pushd`

Creating Directories

Moving Directories

Deleting Directories

Creating Files

Standard Output, Standard Error and Standard Input

Moving Files

Deleting Files

Reading Files

File Permissions

#
Permission
rwx
Binary

7

read, write and execute

rwx

111

6

read and write

rw-

110

5

read and execute

r-x

101

4

read only

r--

100

3

write and execute

-wx

011

2

write only

-w-

010

1

execute only

--x

001

0

none

---

000

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

User
Group
Others
Description

6

4

4

User can read and write, everyone else can read (Default file permissions)

7

5

5

User 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

Last updated