LLI Wiki — My Digital Knowledge Base

Dangerous bash commands to avoid

Nine shell commands that can destroy data or make a system unusable, and why.

Some shell commands that look harmless can delete data, exhaust a machine's resources or make it unusable. This article summarises nine such examples, originally presented in 9 Evil Bash Commands Explained on dev.to.

Destructive directory change

An alias named cd that points to rm -rf turns a simple directory change into a recursive deletion.

alias cd='rm -rf'

alias declares a shortcut for a command. It only exists in the current session, unless it is added to a file loaded at terminal startup, such as ~/.bashrc.

Fork bomb

:(){ :|:& };:

This is a recursive function with no termination condition. Each call invokes the function twice in the background. Written on several lines with a readable name, it looks like this:

evil() {
  evil | evil &
}
evil

The machine quickly consumes all of its memory and CPU, which makes this a denial-of-service attack.

Erasing a disk

dd if=/dev/zero of=/dev/sda

dd copies data from a source to a target. /dev/zero is an unlimited source of zero bytes and /dev/sda is the target disk. The disk contents are overwritten with zeros.

A variant overwrites the disk ten times with random bytes, which makes recovery practically impossible:

for i in {1..10}; do dd if=/dev/urandom of=/dev/sda; done

Losing uncommitted changes

git reset --hard

The --hard option resets the index and the working tree to the last commit. Uncommitted changes to tracked files are discarded, and Git cannot restore them.

Archive that overwrites its source

With the following order, tar overwrites the source file while creating the archive:

tar -czvf /path/to/file archive.tgz

The correct order is:

tar -czvf archive.tgz /path/to/file

With -f, tar takes the next argument as the archive name. If the first file is the source, it is destroyed before tar finds out that the second file does not exist.

Permissions changed across the whole tree

chmod -R 777 /

Instead of:

chmod -R 777 ./

chmod -R applies permissions recursively. Targeting / instead of the current directory changes the permissions of the entire system, which can make it unusable.

Ownership changed across the whole tree

chown -R root:root /

Instead of:

chown -R root:root ./

The same risk applies to ownership changes. Every file on the system is affected, which usually means reinstalling the system.

Checking an encrypted volume

fsck -y /dev/sda

The -y option automatically answers yes to every correction fsck proposes. On an encrypted volume, a check can only run after the volume is unlocked. Otherwise, the repair attempt can destroy the volume.

Mastodon LLIMastodon SocialFosstodon