Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Mac / How to Delete Files and Directories from MacOS Terminal

How to Delete Files and Directories from MacOS Terminal

February 19, 2020 by James Palmer

In this quick guide, we will see how to remove files and folders using the macOS Terminal application. Keep in mind that when you delete a file using the Terminal, there is no way to retrieve it. So be careful using it.
If you have been using Linux terminal, you will be familiar with the commands cause they are the same.

Deleting a file using Terminal

First, we will create a new folder from which we are going to delete files.

$ mkdir TestFolder

Navigate to the Test Folder using cd command (type the first couple of letters of the word TestFolder and press tab to auto-complete the directory name):

$ cd TestFolder/

Now let’s create a test file:

$ touch testfile.txt
$ ls
testfile.txt

To remove this file we will type the following:

$ rm testfile.txt

Remove a folder using Terminal

We will now create another folder within our TestFolder:

$mkdir AnotherTestFolder

To remove it, we use rmdir command:

$ rmdir AnotherTestFolder/

Remember, this command will only work on an empty folder, it won’t delete a folder if it contains some files in it.

Force remove files and folders recursively

To remove a non-empty folder with files in it, we have to use the rm command with the following flags -Rf.
First, we will create the subfolder again and a couple of files within it:

$ mkdir AnotherTestFolder
$ touch AnotherTestFolder\file1.txt
$ touch AnotherTestFolder\file2.txt

$ rm -Rf AnotherTestFilder\

The command for “rm” means to remove, the flag “R” is recursive which will allow you to delete the contents of a directory, and “f” means force, causing the Terminal to delete the file regardless of the error it may cause.

Removing all hidden files from a folder

To remove all hidden files in a folder (once that starts with a dot eg. .testfile) we will use:

$ rm -rf .*

Conclusion

We saw how the basic commands for file and folder removal work. There are a lot of advanced commands for other use cases, like deleting files of a certain type (like temp or backup files), finding and deleting files with the desired filename pattern, etc.

Related

Filed Under: Mac

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in