In order to delete a directory (dir) on your Mac running macOS via the Terminal application you can make use of the rmdir command
Syntax:rmdir path-of-the-directory - if the dir is empty
rm -r path-of-the-directory
Example:
Let's first create a directory in the Desktop and name it myDir, let's add a few files to it, I use the touch command just to create blank files abc.txt, xzy.csv so that the directory is not empty.
% mkdir myDir
% cd myDir
% touch abc.txt
% touch xyz.csv
% cd ..
% rmdir myDir
rmdir: myDir: Directory not empty
As you can see the myDir directory is not deleted as it is not empty, in order to delete the directory with files and folders in it you can make use of rm command with -r parameter.
Example:code2care@mac Desktop % rm -r myDir
![remove dir on mac using rm command remove dir on mac using rm command](https://code2care.org/macos/delete-directory-using-mac-terminal-rm-rmdir-command/images/remove dir on mac using rm command.png)
Troubleshooting steps:
Note if the directory does not exist and you use the rmdir command you will get a "No such file or directory" error
code2care@mac Desktop % rmdir mydir1
rmdir: mydir1: No such file or directory
![No such file or directory No such file or directory](https://code2care.org/macos/delete-directory-using-mac-terminal-rm-rmdir-command/images/No such file or directory.png)
If the object you are trying to delete and it's not a directory - i.e. a file you will get an error like - Not a directory
code2care@mac Desktop % rmdir myfile.txt
rmdir: myfile.txt: Not a directory
Options that you can use with rm command
- -r :Recursive: remove directories and their contents recursively
- -f :Force: ignore nonexistent files, never prompt
- -i :Interactive: prompt before every removal
- -l :Interactive: only prompt before removing more than three files
- -r :Verbose: explain what is being done
![Directory is not empty Directory is not empty](https://code2care.org/macos/delete-directory-using-mac-terminal-rm-rmdir-command/images/Directory is not empty.png)
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Fix: grunt: command not found (Mac/Windows)
- AppleScript Example with TextEdit
- How to copy Password of Wifi on your Mac
- Install OpenJDK Java (8/11/17) versions using brew on Mac (Intel/M1/M2)
- How to check if a port is in use using terminal [Linux or macOS]
- Cut, Copy and Paste Keyboard Shortcuts on Mac Keyboard
- [macOS] NetBeans IDE cannot be installed. Java (JRE) found on your computer but JDK XX or newer is required.
- How to Remove Widgets from macOS Sonoma 14 Desktop
- Turn off Auto-Capitalization on macOS Ventura 13
- How to Go To /usr/local/bin on Mac Terminal?
- How to Open TextEdit on Mac
- Mac: How to Open Android Studio from Terminal
- How to change the default location of Mac Screenshot
- Display Seconds in Digital Clock on Mac Menu Bar [macOS]
- How to install homebrew (brew) on M1 Mac
- Must Know Homebrew Commands for Mac/Linux Users
- Steps to Uninstall Roblox on Mac (macOS)
- How to change the System Settings Sidebar icon size Mac Ventura 13
- How to set Out Of Office (OOO) on Outlook for Mac [Updated 2023]
- Trigger Notification Center Message using Mac Terminal Command
- [fix] macOS Ventura - Python3 xcrun: error: invalid active developer path missing xcrun at CommandLineTools
- How to Install Rust Language on Mac (macOS)
- How to minimize all Windows in Mac
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS]
- How to make TextEdit the default text Editor on Mac
- Fetch only content-type using cURL Command - cURL
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs - Python-Programs
- How to check your installed version of Git - Git
- Create Symbolic Link using Terminal Command - MacOS
- Xcode 13 - unknown error compiling for iOS 15.0 but module has a minimum deployment target of iOS 15.2 - iOS
- Accept Only 0-9 Numbers RegEx Example - HowTos
- How to Run PowerShell Script (.ps1) on Mac - MacOS
- Shortcut: How to delete a complete line in Nano Editor - Linux