Read the Manual: rm

Another old Unix standby… without too many extra flags.

People interested in learning how the command lines tools on their computer work.

The rm command is the Unix way to remove directory entries”. Right off the bat, opening the man page, I learned that the unlink command exists, is an alias to rm, and… has different rules than rm. Amazing.

Let’s get unlink out of the way first: you are only allowed to pass a single argument, and it is not allowed to be a directory. It is, as far as I can tell, basically a command that just invokes the unlink POSIX operation directly on a single file. Could be handy sometimes!

Now, rm has a bunch of flags:

  • -d tries to remove directories as well as other kinds of files; by default rm will not touch directories.

  • -f tries to remove everything you specify, regardless of file permissions_by default rm will not delete non-writable files.

  • -R tries to remove the file hierarchy rooted in each file argument”, i.e. if you hand it a directory it recursively deletes it and its contents — so it implies -d.

  • -r is equivalent to -R. I have literally only ever used -r, though: I did not know -R existed!

If that sounds dangerous, it is, so there are also the -i and -I flags:

  • -i wants confirmation for every. single. file. you delete. regardless of file permissions. 😳

  • -I is like -i but only prompts if you are deleting more than 3 files or doing -r.

In the WHOA bucket: -W attempts to undelete the files you pass to it. But it can only be used to recover files covered by whiteouts in a union file system”, which I don’t think APFS is? I don’t know what file systems do work that way, actually (feel free to chime in!).

You can absolutely hose yourself with rm with some of those! If you decide to delete everything in the current directory, rm -rf ./*, and you forget that leading ., you are in for a world of hurt: you just forcibly recursively deleted your entire machine. I have done this! I caught it relatively quickly and had a local Time Machine backup, so I survived, but it was terrifying, and I was not sure I was going to get out of it without doing a complete restore from that Time Machine backup. It taught me a really important lesson, though!

A good tip, then, is to either build the habit of always passing -I if you are going to rm -rf, or possibly even to alias rm to rm -I so you don’t accidentally delete everything on your system.