How to Delete Files Using the Terminal in Mac OS X

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
You can delete files using the terminal in Mac OS X.
Image Credit: Prostock-Studio/iStock/GettyImages

Most people choose Move to Trash after a right-click when they want to remove a file in Mac OS X. This works in most cases, and when you use this method, you can restore the deleted file at any time. However, if you run into errors when trying to delete a file, want to remove a file that isn't visible in Finder, or you want the file to be deleted entirely and not restorable in a single action, use the Mac Terminal to delete the file. This is easy to do even if you don't have experience using Terminal, but it doesn't offer the same leniency when it comes to accidental deletion, so you need to be careful.

Advertisement

Why Use Mac Terminal

Video of the Day

Terminal is the command line control system for a Mac, and it comes with every Mac computer and laptop. If you're familiar with Unix systems, you'll be right at home working in Terminal. There are many advantages to learning some of the basic commands you need. The most significant benefit is that Terminal allows you to change your Mac's settings and perform operations with files that you can't achieve with the graphical user interface alone.

Advertisement

Video of the Day

With Terminal, you can delete files that give error messages when you try to delete them, delete files when Finder isn't working correctly, or delete otherwise invisible files.

The “rm” Mac Command

Open Terminal (in the Utilities folder of Applications) and check the directory you're currently in by typing "ls –la" into the prompt and pressing Enter. Navigate down to the next directory by typing "cd [directory name]" with the name of the specific directory in place of "[directory name]" and without quotation marks, or up to the previous directory by typing "cd../" and then pressing Enter. Your aim is to navigate to the directory that contains the file you want to delete. However, if you want to avoid this step you can use the drag and drop approach.

Advertisement

When you're in the right folder (or if you're using drag and drop), use the "rm" Mac command to delete the chosen file. Type "rm [filename.extension]" without quotations and with the relevant filename and extension in place of "[filename.extension]"), and then press Enter to delete the file. Alternatively, you can open Terminal, type "rm," leave a space and then drag the file from Finder into the Terminal window to auto-fill the full file extension.

Advertisement

Advertisement

Adding a Safeguard

The main downside to the Terminal method of deleting files is that there is no "are you sure?" type of prompt to stop you from deleting something important. You can rectify this by adding the interactive flag "-i" to the command. You type "rm -i" before the filename and extension to create a prompt. When you select Enter after typing this command, you see a prompt that says "remove [filename.extension]?" and you can type "y" or "yes" to confirm or "n" or "no" to cancel the deletion.

Advertisement

Deleting Directories Using Terminal

You can also delete a folder with Mac Terminal, and there are a couple of different options depending on your needs. If you have an empty folder to remove, you can use the remove directory Terminal command, "rmdir," in the same way as the "rm" command, although the safeguard doesn't work in this case. For example, "rmdir Pictures" would delete the directory "Pictures" from the directory you're currently in.

Advertisement

However, to delete a file including its files and any subfolders with files, use the "-R" (or -r) option at the end of the "rm" command. This means "recursive" and deletes everything from that directory downward. So if your "Pictures" folder had pictures in it or even sub-folders, you could type "rm -R Pictures" from the relevant directory and then select Enter. You can use the safeguard here by typing "rm -iR [Folder name]" into Terminal.

Advertisement

Advertisement

references