How to delete files and folders in Terminal on Mac

One of the most basic computer functions — deleting files and folders — is also one of the most essential. If you never get rid of anything, soon enough all those extra gigabytes will take a toll on your Mac’s processing power, RAM, and hard drive, not to mention your digital life will resemble a dreadful episode of Hoarders. 

So deleting files is good and healthy. But how do you do that? Most people right-click on what they need gone and choose Move to Trash from the menu or use the File option in the menu bar. Others employ the ⌘ + Delete shortcut, which works across the system (even within dialog windows). Experts, however, often find themselves defaulting to the command line delete directory feature. Let’s see how and why you should learn it too.

Why delete file command line feature is important

Since macOS is a Unix-based system, nearly all system tasks you do every day with the help of its graphic interface can also be performed via the command line tool called Terminal.

With regards to delete file or delete folder command line functionality, Terminal lets you:

  • Effortlessly erase one or multiple files, folders, and apps, bypassing any error messages you can get when you go the traditional route.
  • Remove files from Trash, including ones you can’t delete by simply emptying the trash.
  • Get rid of files that are invisible to you within Finder (usually system or root files, for example, .htaccess).
  • Delete files and folders in cases when Finder is unresponsive.

Note: The Mac command line delete file feature is final and irreversible. While it lets you avoid any error messages, it also removes the files completely, without any possibility of retrieving them later on.

How to use delete file command line feature

Removing files from your Mac forever using Terminal is deceptively simple: just use the rm command followed by the name of the file. Here’s how it works in practice:

  1. Launch Terminal from your Utilities folder in Applications.
  2. Check which directory  you’re in by typing ls -la
  3. Then navigate down a directory with cd [directoryname] or up a directory with cd ../
  4. When you’re in the same directory as the file you want to delete, type rm [filename.extension]
  5. If you want to delete multiple files at once, list them all, but make sure there’s a space between each one.
  6. Press Enter to execute the command.

Now all the files you specified after rm are gone for good. Navigate directories in your Mac and repeat the process as many times as you want. 

Surprised there was no confirmation before your files were deleted? Luckily there’s a way to add one as a safeguard for not deleting the wrong file by accident. Just use -i after the rm but before the first filename, like this: rm -i [filename.extension]. Terminal will then ask you whether you’re sure you want to delete the file. Reply y or yes followed by Enter and the file will be gone. This also works for multiple files, but you’ll have to confirm the removal of each one separately.

terminal-command-line-delete-file

How to make command line delete directory

Surprisingly, you can’t delete a folder using the rm command because it has its own: rmdir.

Otherwise, rmdir works exactly the same as rm:

  1. Navigate to the appropriate directory using Terminal.
  2. Type rmdir [directoryname]
  3. Hit Enter.

Sadly, you can’t use the -i hack when you’re deleting folders, so be extra careful! 

Another thing to keep in mind is that rmdir only deletes the directory, but can’t delete any files or folders located within that directory.

To delete a folder with everything in it, you need to use rm -r followed by the folder’s name. Using -i to create a warning here is possible and done like this: rm -ir [foldername].

terminal-command-line-delete-folder-mac
Tip:

When it’s too difficult to find a folder or file via Terminal, but you can detect them in Finder, simply drag and drop the file onto the Terminal window to generate its path.

Find and delete heavy folders in seconds

Terminal commands are indispensable when you encounter stubborn files that just won’t go away, giving you all kinds of errors. But when you want to offload some space-hogging files and folders (including invisible ones) to release some much-needed Mac productivity and speed, you need to use an efficient app that can help you avoid manually navigating directories up and down.

CleanMyMac X is exactly what you need. It’s one of the best Mac optimization utilities in the world and contains a wealth of useful features, from streamlining your system files to scanning your Mac for viruses.

However, what we’re looking for now specifically is the Space Lens feature:

  1. Launch CleanMyMac X (free download here).
  2. Navigate to Space Lens in the sidebar.
  3. Click Scan.
  4. The app will graphically show you which files or folders take up the most space in each directory. Navigate through them all and check the ones ready for deletion. When done, just hit Remove.
Space Lens CMMX

By using Space Lens with CleanMyMac X, it’s easy to get rid of gigabytes of junk, and it all takes under a minute. Don’t forget to check back and repeat the process every month or so, and your Mac will keep performing at top speed for years to come.

CleanMyMac X
CleanMyMac X

Your Mac. As good as new.