Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

How to Easily Delete a File in Mac

Learning how to use the Macbook terminal to delete a file will make you a powerful developer especially when manipulating files and folders.

In this article we’ll learn various methods to delete files and folders on macOS using the command line.

How to remove a file

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

Bash
rm filename

How to remove protected files

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:

Bash
sudo rm filename

How to securely delete files

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

Bash
rm -P filename

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

How to delete a folder

To delete an empty directory, use the rmdir command:

Bash
rmdir directory_name

How to delete folder and all it’s files

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

Bash
rm -r directory_name

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

Working 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:
Bash
chflags nouchg filename
  1. Then delete the file:
Bash
rm filename

Working with 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):
Bash
rm .hidden_file
  1. To see hidden files in the current directory before deleting:
Bash
ls -a

Additional Tips

  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:
Bash
rm -i filename
  1. Wildcard deletion: You can use wildcards to delete multiple files. For example, to delete all .txt files:
Bash
rm *.txt
  1. Deleting files with special characters: If a filename contains spaces or special characters, enclose it in quotes:
Bash
rm "file with spaces.txt"
  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.

This is the Macbook I use and is the best for now and as a future proof purchase.

Deleting Specific File Types at Once

This technique utilized grep with rm to search for files matching a specific pattern and then delete those files. Usually, you would list the files first using ls or find, pipe the results through grep to filter them, and finally pipe them to xargs rm to remove the files.

Example: Say you want to delete all .log files in a directory that contain the word “error” in their filenames.

Here’s how you can do it:

Bash
ls | grep "error.*\.log" | xargs rm

Explanation

  1. ls: Lists all files in the current directory.
  2. grep "error.*\.log": Filters the list to only include files that contain “error” followed by anything (.*) and ending with .log.
  3. xargs rm: Takes the output from grep and passes it to rm to delete the files.

Important Note

Before running the command with rm, you can replace rm with echo to see which files would be deleted:

Bash
ls | grep "error.*\.log" | xargs echo

This will list the files that match the pattern without deleting them, allowing you to review the list and ensure it’s correct. Once you’re confident, you can proceed with the rm command.

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.

Jenesh Napit
Jenesh Napit

Currently a Software Engineering Manager, managing 2 teams. Previously a Senior Software Engineer. Besides writing articles, I also provide career coaching for aspiring engineers looking to get their first role and help get promotions in their current roles.