Privacy bash script for macOS

The following bash script automates the process of running various privacy tasks on macOS, including clearing your Bash and ZSH terminal history, dns cache, system cached files and CUPS printer jobs cache. It will also empty the bin/trash on all volumes and purge inactive memory usage.

Profile picture of Thomas Kokholm
Thomas Kokholm
December 02, 2022

The script below automates the process of the follow seven privacy tasks on macOS:

  • Clear Bash terminal history - Clear ZSH terminal history

  • Clear DNS cache

  • Purge inactive memory usage

  • Clear system cache files

  • Empty trash on all volumes

  • Clear CUPS printer job cache

Usage

  1. Paste the script below in a file (i.e. privacy.sh).

  2. Open up your terminal

  3. Navigate to where you saved the file (i.e. privacy.sh)

  4. Give the file execute permissions by typing : $ chmod +x privacy.sh

  5. Finally run the script by typing $ ./privacy.sh

Note that your terminal might ask for your administrator password in order to process the script!

#!/usr/bin/env bash



# ----------------------------------------------------------

# -------------Request administrator privileges-------------

# ----------------------------------------------------------

if [ "$EUID" -ne 0 ]; then

script_path=$([[ "$0" = /* ]] && echo "$0" || echo "$PWD/${0#./}")

sudo "$script_path" || (

echo 'Administrator privileges are required.'

exit 1

)

exit 0

fi




# ----------------------------------------------------------

# --------------------Clear bash history--------------------

# ----------------------------------------------------------

echo '--- Clear bash history'

rm -f ~/.bash_history

# ----------------------------------------------------------




# ----------------------------------------------------------

# --------------------Clear zsh history---------------------

# ----------------------------------------------------------

echo '--- Clear zsh history'

rm -f ~/.zsh_history

# ----------------------------------------------------------




# ----------------------------------------------------------

# ---------------------Clear DNS cache----------------------

# ----------------------------------------------------------

echo '--- Clear DNS cache'

sudo dscacheutil -flushcache

sudo killall -HUP mDNSResponder

# ----------------------------------------------------------




# ----------------------------------------------------------

# ------------------Purge inactive memory-------------------

# ----------------------------------------------------------

echo '--- Purge inactive memory'

sudo purge

# ----------------------------------------------------------




# ----------------------------------------------------------

# -----------------Clear system cache files-----------------

# ----------------------------------------------------------

echo '--- Clear system cache files'

sudo rm -rfv /Library/Caches/* &>/dev/null

sudo rm -rfv /System/Library/Caches/* &>/dev/null

sudo rm -rfv ~/Library/Caches/* &>/dev/null

# ----------------------------------------------------------




# ----------------------------------------------------------

# ----------------Empty trash on all volumes----------------

# ----------------------------------------------------------

echo '--- Empty trash on all volumes'

# on all mounted volumes

sudo rm -rfv /Volumes/*/.Trashes/* &>/dev/null

# on main HDD

sudo rm -rfv ~/.Trash/* &>/dev/null

# ----------------------------------------------------------




# ----------------------------------------------------------

# ---------------Clear CUPS printer job cache---------------

# ----------------------------------------------------------

echo '--- Clear CUPS printer job cache'

sudo rm -rfv /var/spool/cups/c0*

sudo rm -rfv /var/spool/cups/tmp/*

sudo rm -rfv /var/spool/cups/cache/job.cache*

# ----------------------------------------------------------




echo 'Privacy and security script completed!'

echo 'Press any key to exit.'

read -n 1 -s

This script is based on the open source tool by UndergroundWires. Check out the GitHub repository for details and even more privacy scripts.