Topic 2 of 5 · 4 min read
Destructive and irreversible shell commands
Goal: Flag the destructive commands in an agent's run log and name a safer alternative for each.
A destructive command is one you cannot undo, so the question to ask is "what is the cheapest way to see or undo this first?" An agent does not feel risk. It picks the shortest path to a green result, and sometimes that path is rm -rf.
Check yourself · 1 of 3
Which command is the safest way to force-update a branch you own after rewriting its history?
Why it matters
There is no trash can in the shell. rm removes the file, and on most servers there is no backup of an untracked file. Some commands are shared in effect: a force push rewrites a branch other people use, and killing a process by name can stop a neighbour's service. The cost lands on other people.
The usual suspects
- Recursive delete:
rm -rf. Safer: delete a named path, list it first withls, or move it to a temp folder. - Force push:
git push --forceoverwrites remote history. Safer:--force-with-lease, which refuses if someone else pushed, or a new branch. - Hard reset and clean:
git reset --hardandgit clean -fddrop uncommitted work. Safer:git stashfirst, orgit clean -nto preview. - Recursive permissions:
chmod -R 777 .andchown -Rchange every file below. Safer: change one file, or one directory, with the minimum mode. - Download and run:
curl https://example.com/install.sh | shruns code you have not read. Safer: download to a file, read it, then run it. - Kill by name:
pkill nodeorkillall pythonstops every matching process on the machine, including other people's. Safer: kill the process id you started. - Database and disk:
DROP TABLE,DELETEwithoutWHERE,ddandmkfs.
Before and after
Weak, from an agent's run log:
git push --force origin main
pkill -f node
rm -rf node_modules .git/hooks
Fixed:
git push --force-with-lease origin fix/login-bug
kill 48213
rm -rf node_modules
The work moves to its own branch, the kill names a pid the agent recorded when it started the server, and the unrelated .git/hooks stays.
What goes wrong
- The agent "cleans up" to make an error go away. A failing build is fixed by deleting the lockfile or the cache of a different project.
- A command is scoped by guess.
pkill nodeassumed only the agent's own server was running. - A flag is added to silence a prompt.
-fand--yesremove the one moment where you could say no.
Set your agent's permissions so risky commands always ask, keep work in git with a clean tree before a long run, and have the agent show a dry run first.
Examples
Reading a run log
git reset --hard origin/main
git clean -fd
chmod -R 777 /var/www
The first two discard every uncommitted change and every untracked file. The third makes a whole web root writable by anyone. Safer: git stash -u first, git clean -n to preview, and chmod 640 config.php on the one file that needed it.
The install one-liner
curl -fsSL https://example.com/install.sh | sudo bash
This runs unread code as root. Save it with curl -fsSLo install.sh ..., read it, and run it without sudo if you can.
Key points
- Ask what is the cheapest way to preview or undo a command before approving anything that deletes or rewrites.
- Prefer --force-with-lease over --force, and push to a new branch instead of rewriting a shared one.
- Recursive chmod and chown touch every file below the path, so scope them to one file or directory.
- Never pipe a download straight into a shell; save it, read it, then run it.
- Stop only the process id the agent started; kill by name or pattern can stop other tenants' services.
Common mistakes
- Approving a command because the agent said it was safe, without asking what it deletes or overwrites.
- Allowing a force push to a shared branch when force-with-lease on a new branch would do.
- Using pkill, killall or a name-based pattern on a server that runs other people's services.
- Letting an agent run hard resets or cleans with uncommitted work in the tree.
Check yourself · 2 of 3
An agent wants to run curl -s https://tools.example.org/setup.sh | sudo sh. What is the main problem?
Check yourself · 3 of 3
A shared server runs several tenants' Node apps. The agent's dev server hangs. What is the safest way to stop it?
Practice items, not counted towards your mastery.
Start here
- The Missing Semester of Your CS Education (MIT) (opens in a new tab)CourseFree
Free lectures on the shell, shell scripting, data wrangling and command-line tools.
- ShellCheck (opens in a new tab)ToolFree
Paste a script or command and see unquoted variables and other classic traps flagged.
- The Linux Command Line (free book) (opens in a new tab)BookFree
A full book on navigation, permissions, processes, pipes and scripting.
More for this topic
- chmod(1) Linux manual page (opens in a new tab)DocsFree
Exact meaning of file modes, symbolic and octal, and recursive changes.