Skip to content

Bash

Commonly used commands

If you want to see which commands you run often you can run the following command and you can even add in your .bashrc as an alias.

Using history to see commonly used commands
history | awk '{print $2}' | sort | uniq -c | sort -rn | head

See the largest directories in your current directory

This command will allow you to view 10 largest directories in your current directory.

10 largest sub directories
du -hs */ | sort -hr | head

See which apps are talking on your network

This command allows you to see what apps are consuming internet.

Bash
ss -p

Replace words in a file using 'sed'

Replacing words using sed
1
2
3
4
5
# To replace the word quinn in a file with target_env
$> sed -i 's/quinn/target_env/g' file_path

# To replace the word quinn in multiple files
$> sed -i 's/quinn/target_env/g' *

Get the current directory of a script being called

Multiple ways to get a scripts directory
1
2
3
4
5
# Option 1:
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

# Option 2:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

Check if a list is empty

Bash
1
2
3
if [[ ${#list[@]} -eq 0 ]]; then
  echo "List is empty"
fi

Iterate of a list (for loop)

Bash
for i in "${list[@]}"; do
  echo "$i"
done
## Make sure the current user is root

```bash title="Make sure the current user is root"
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

Check if a command exists

Check if a command exists
1
2
3
4
5
if ! command -v docker &> /dev/null
then
    echo "Docker could not be found"
    exit
fi

Check if a file exists

Check if a file exists
1
2
3
4
5
if [ -f "$FILE" ]; then
    echo "$FILE exists."
else
    echo "$FILE does not exist."
fi

Check if a directory exists

Check if a directory exists
1
2
3
4
5
if [ -d "$DIRECTORY" ]; then
    echo "$DIRECTORY exists."
else
    echo "$DIRECTORY does not exist."
fi

Check if a variable is empty

Check if a variable is empty
1
2
3
4
5
6
if [ -z "$var" ]
then
      echo "\$var is empty"
else
      echo "\$var is NOT empty"
fi

Check if a variable is not empty

Check if a variable is not empty
1
2
3
4
5
6
if [ -n "$var" ]
then
      echo "\$var is NOT empty"
else
      echo "\$var is empty"
fi

Check if a variable is set

Check if a variable is set
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi

Set a timeout for a command

Set a timeout for a command
timeout 10s command

Check if a command is running

Check if a command is running
1
2
3
4
5
6
if pgrep -x "command" > /dev/null
then
    echo "Running"
else
    echo "Stopped"
fi

Timestamps

Timestamps
1
2
3
4
5
6
7
8
# Timestamp in seconds
date +%s

# Timestamp in milliseconds
date +%s%3N

# Timestamp with formatting, i.e 20240104_121201
$(date +%Y%m%d_%H%M%S)

Get filename without extension

Get filename without extension
1
2
3
filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"

String to lowercase

String to lowercase
echo "HELLO" | tr '[:upper:]' '[:lower:]'

User commands

See who's logged in
1
2
3
4
5
6
7
8
# Prints out the users who are currently logged in
who

# Prints out the users and how many are connected, can use -q or --count
who --count

# Prints out the users who are currently logged in and what they are doing
w
Kill a user session
# This will kill the user section who's tty is pts/0
pkill -9 -t pts/0
Get your current tty
# This will print out your current tty
tty

View process resources

View process resources
# This will print out the resources for the current process
ps -o %cpu,%mem -p $$
Using top to view process resources
# For a high level view of the resources being used by processes
top

# For a high level view of the resources being used by processes, but only show the processes for the current user
top -u $USER

# For a high level view of the resources being used by processes, but only show the processes for the current user and sort by memory usage
top -u $USER -o %MEM

# For a high level view of the resources being used by processes, but only show the processes for the current user and sort by cpu usage
top -u $USER -o %CPU

# To view the resources being used by a specific process
top -p $PID
# or
top -p `pgrep "process_name"`

Delete files older than x

Delete files older than 5 days
find /path/to/files* -mtime +5 -delete
Delete files older than 5 days, but not in a specific directory
# This will delete files older than 5 days, but not in the directory /path/to/keep
find /path/to/files* -mtime +5 -not -path "/path/to/keep/*" -delete
Delete files older than 5 minutes
find /path/to/files* -mmin +5 -delete