DEV Community

Cover image for How to Delete Files and Directories in Mac Terminal
Jenesh Napit
Jenesh Napit

Posted on

How to Delete Files and Directories in Mac Terminal

Deleting files and directories is usually easy with Finder for Mac users. While graphical interface works great most of the times, using terminal commands usually offers more flexibility and power. In this article we'll learn various methods to delete files and directories on macOS using the command line.

This article was first published on MyDevPa.ge blog, check it out for helpful tutorials for Software Developers.

How to Delete a File in Mac

Using the rm command

The most common way to delete a file in Mac's terminal is using the rm (remove) command:

rm filename
Enter fullscreen mode Exit fullscreen mode

Replace filename with the name of the file you want to delete. If the file is write-protected or you don't have sufficient permissions, you may need to use sudo:

sudo rm filename
Enter fullscreen mode Exit fullscreen mode

Securely deleting files

macOS provides a built-in command for securely deleting files, which is similar to the shred command in Linux:

rm -P filename
Enter fullscreen mode Exit fullscreen mode

The -P flag overwrites the file with zeroes three times before deleting it, making it much harder to recover.

How to Delete a Directory in Mac

Using the rmdir command

To delete an empty directory, use the rmdir command:

rmdir directory_name
Enter fullscreen mode Exit fullscreen mode

Using the rm command

To delete a directory and all its contents, use the rm command with the -r (recursive) option:

rm -r directory_name
Enter fullscreen mode Exit fullscreen mode

Be very careful with this command, as it will delete the directory and everything inside it without asking for confirmation.

Dealing with Locked Files

If you encounter a "Operation not permitted" error when trying to delete a file, it may be locked. To unlock and delete it:

  1. First, remove the "locked" flag:
   chflags nouchg filename
Enter fullscreen mode Exit fullscreen mode
  1. Then delete the file:
   rm filename
Enter fullscreen mode Exit fullscreen mode

Handling Hidden Files

Hidden files in macOS start with a dot (.). To delete a hidden file or directory, you can use the same rm commands, but you'll need to either:

  1. Type the full name including the dot (you can use tab to autocomplete):
   rm .hidden_file
Enter fullscreen mode Exit fullscreen mode
  1. To see hidden files in the current directory before deleting:
   ls -a
Enter fullscreen mode Exit fullscreen mode

Additional Tips and Considerations

  1. Use caution: The rm command deletes files permanently. Unlike moving files to the Trash, this action is typically irreversible.

  2. Use the -i flag for confirmation: Add -i to rm commands to prompt for confirmation before each deletion:

   rm -i filename
Enter fullscreen mode Exit fullscreen mode
  1. Wildcard deletion: You can use wildcards to delete multiple files. For example, to delete all .txt files:
   rm *.txt
Enter fullscreen mode Exit fullscreen mode
  1. Deleting files with special characters: If a filename contains spaces or special characters, enclose it in quotes:
   rm "file with spaces.txt"
Enter fullscreen mode Exit fullscreen mode
  1. File recovery: While it's possible to recover deleted files using third-party software, it's not guaranteed. Always have backups of important data.

Conclusion

Understanding how to delete files and directories using Mac terminal commands gives you more control over your system. Remember to always double-check before deleting, especially when using powerful commands like rm -r. With practice, you'll become more comfortable managing your files efficiently from the command line.

Remember, with great power comes great responsibility. Sometimes you can't undo so always ensure you have backups of important data, and be cautious when using deletion commands, especially with sudo privileges.

Visit MyDevPa.ge to create your free portfolio website in a minute for free!

Top comments (1)

Collapse
 
jenesh profile image
Jenesh Napit

If anyone is interesting in writing their articles on MyDevPa.ge checkout the contact email at the bottom of the page and I'll get back to you, thanks!