Operations 4 min read

How the Thanos.sh Script Randomly Deletes Half of Your Files on macOS

This article explains the open‑source Thanos.sh utility that randomly selects and displays half of the files in a directory on macOS, provides installation steps for required tools, and shows how to modify it for destructive deletion.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How the Thanos.sh Script Randomly Deletes Half of Your Files on macOS

Project Overview

The Thanos.sh project is an open‑source shell script that randomly selects half of the regular files in the current directory and outputs their contents. Replacing the cat command with rm makes the script delete the selected files.

Installation on macOS

macOS requires brew and the GNU coreutils package, which provides gshuf (a GNU implementation of shuf). Install them with:

# Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install coreutils (provides gshuf)
brew install coreutils

Script Details

#!/bin/sh
let "i=`find . -type f | wc -l`/2"
if [[ uname=="Darwin" ]]; then
    find . -not -name "Thanos.sh" -type f -print0 | gshuf -z -n $i | xargs -0 -- cat
else
    find . -not -name "Thanos.sh" -type f -print0 | shuf -z -n $i | xargs -0 -- cat
fi
# To delete instead of display, replace 'cat' with 'rm'

Explanation

Count all regular files in the current directory and divide by two (stored in variable i).

List all files, excluding the script itself, using find with null‑terminated output.

On macOS, pipe the list to gshuf; on other Unix‑like systems, use shuf to randomly select $i entries.

Pass the selected file paths to xargs, which runs cat to display their contents. Replace cat with rm to delete them.

Safety Notice

The script is destructive and has no undo capability. It should only be used in controlled, experimental environments; running it on personal machines or production systems can cause irreversible data loss.

Repository

GitHub: https://github.com/hotvulcan/Thanos.sh/ (1.8K stars, 235 forks)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ShellmacOSrandom selectiondestructive script
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.