Deleting files from a computer is a very common and easy task to perform. However, it’s not so common to delete a directory on your system. How to delete a directory in Terminal on Mac? This page will give you answers to the question. Besides, it tells you how to delete remove files and folders using CMD or how to force delete a directory in Linux.
Part 1: Delete Files on Mac
When you don’t need a file anymore, you must want to delete it for getting more free space on your disk. The things go the same with a folder; when all items saved in a folder are not useful for you anymore, you’ll be thinking of deleting the folder as a whole. Deleting files & folders is one of the most common actions you may perform while using a computer. How to delete files on Mac? How to delete a directory in Terminal? The following content tells all.
How To Check Storage On Mac: 5 Ways Available.
3 Common Ways to Delete Files & Folders on Mac
1st way: drag and drop the item to Trash.
2nd way: right click on the item and select Move to Trash.
3rd way: press Command + Delete or Command + Shift + Delete.
Are there other ways possible to delete the files, folders, or even directories you don’t want? Of course, yes. How to delete locked files on Mac?
- Open Finder.
- Navigate to the locked file.
- Control click the file or press Command + I.
- Select Get Info.
- Uncheck the Locked option under General.
How to delete hidden files on Mac?
- Open Finder.
- Look for Locations.
- Click on your hard drive.
- Open the Macintosh HD folder.
- Press “CMD + Shift + .” to show the hidden files.
- Repeat this process to show hidden files in other folders.
In addition, you can delete directory using Terminal, which is a command line tool built in every macOS. With this tool, you can perform almost all the system tasks that you can do with the help of the graphic interface.
Reasons for Choosing Delete Directory Terminal
After moving files and folders to Trash, you can easily recover them if you find they are still useful. However, if you delete files in Terminal or delete folder Terminal, the items won’t be put to Trash. That means you’re not able to get them back from Mac as you usually do. But why bother using this command line tool to delete things? There are some common reasons for explaining why people choose to delete a file in Terminal, remove folder Terminal, or delete a directory in Terminal.
- It helps you to delete multiple files, folders, and even apps quickly and efficiently by using wildcards.
- It allows you to delete items you can’t delete with common ways, bypassing any error messages.
- It’s able to delete files that are even hidden in the Finder on your Mac (system or root files are usually hidden by default).
- Terminal can also be very helpful if Finder becomes unresponsive or you lose access to it due to unknown reasons (for instance, your Mac is on the blink).
Can you fix it when Windows Explorer is not responding or stops working properly?
Part 2: How to Delete Files/Directory Using Terminal
How to Delete a File in Terminal
Some users are asking how to remove a file in Terminal or how to force delete a file Mac. It’s easy to remove files/folders or remove directory Terminal. Here are the exact steps you should finish.
Step 1: Open Terminal Mac
Method 1: open through Finder.
- Open Finder as you usually do.
- Browse the items and select Applications.
- Look for the Utilities folder and open it.
- Double click on Terminal to run it.
You can also press Option + Command + Space to quickly open Finder and go to the “Searching This Mac” window.
Method 2: search for Terminal.
- Navigate to Dock on your Mac.
- Look for the Launchpad icon and click on it.
- Type terminal into the search field you see.
- Click Terminal from the search result to open it.
Step 2: Delete a File
You can delete files you don’t need with the help of the “rm” command, but you need to know that files deleted in this way won’t be sent to Trash.
Firstly, you should navigate to the directory in which the target file is kept. For instance, please type cd ~/Desktop to go to the Desktop directory.
Secondly, type the command to delete your file.
- Please type rm filename to delete a single file.
- Type “file name.extension” to delete a file when its name contains one or more spaces.
Thirdly, press Return on your keyboard to execute the file deletion command. The file will be removed immediately, without asking you to confirm whether you want to delete it or not (the confirmation window will appear if you delete a file using the common ways mentioned in part 1).
Attention!!!
What if you have multiple files you want to delete? Do you need to delete them one by one with the “rm” command mentioned above? Can you delete them at the same time using Terminal?
Luckily, the Terminal utility on Mac even allows users to delete multiple files simultaneously by using one single command: rm filename filename filename (as said before, you can replace filename with “file name.extension” if the filename does contain spaces).
Adding a “-i” flag if you want.
Also, you may add the “-i” flag into the deletion command if you’re cautious enough. The “-i” flag acts as a pause button, so you’ll need to confirm your actions in Terminal after doing this.
- To delete a single file: rm -i filename.
- To delete multiple files: rm -i filename filename filename.
You should press Return to execute the command. But, you still need to type yes, or simply y to confirm the deletion action.
How to Delete a Directory in Terminal
Also, you need to open Terminal using the way you like. Then, you should use the rm -R (or -r) command to delete the entire folder. All the files, subfolders, and items contained in the subfolders will be removed from Mac.
4 Steps on How to Remove a Directory in Terminal
- Open Terminal.
- Type rm -R directoryname to delete a directory when the name doesn’t contain space.
- Type rm -R “directory name” to delete a directory if spaces are found in its name.
- Press Return and wait.
Likewise, you can add the -i flag for protection, typing rm -iR directoryname instead.
How to Delete a Folder in Terminal
How to delete an empty directory (also known as the folder)? The command for deleting directories, or folders is a little bit different; it’s rmdir.
Please open Terminal -> type rmdir foldername -> press Return -> wait for the action to complete.
Part 3: Recover Deleted Data on Mac
You can use Mac Terminal to recover files moved to Trash or deleted permanently. However, this command line tool doesn’t work for you to recover data deleted with the “rm” commands mentioned above. Therefore, you should turn to the powerful data recovery software that supports Mac.
Step 1: Choose a Professional Date Recovery Tool for Mac
Stellar Data Recovery for Mac is a good choice for users to recover deleted/lost/missing files.
- It supports MacBook Pro/Air, iMac, Mac mini & more.
- It’s able to recover permanently deleted or lost/missing data from macOS.
- It allows users to recover files from SSD, HDD, SD card, USB, or other drives connected to Mac.
4 Useful Methods For Getting Files Off Failing Hard Drive On Mac.
You can click on the button below to get Stellar Data Recovery for Mac directly or visit this webpage to know more about it.
Data Recovery for MacClick to Download100%Clean & Safe
Step 2: How to Recover Deleted Data on Mac
Please follow this guide to recover files and folders deleted by Terminal.
- Run the Stellar Data Recovery for Mac.
- Decide which file types you want to recover in the Select What To Recover You can turn ON your desired file types manually or turn ON Recover Everything to select them all.
- Click on the Next button to continue.
- Specify the drive where the lost data are saved in the Select Location For instance, you may select macintosh HD.
- Click on the Scan button to start searching for lost files. You can also enable the Deep Scan in the lower left corner for better recovery results.
- Browse the found files & folders and check the ones you need. Then, click Recover at the bottom right.
- Click Browse in the Recover window to select a safe location for keeping the recovered data.
- Click on the Save button to confirm. The recovery process will be started immediately.
- Wait for the recovery to finish and go to check the recovered items files and folders.
How to recover data from your MacBook if it can’t boot successfully?
Part 4: Remove Directory CMD on Windows
In Windows systems, there are also built-in command line tools to help users perform different tasks. Two of the most commonly used ones are Command Prompt (also known as CMD) and PowerShell. Now, I’ll mainly focus on the CMD delete directory.
How to recover permanently deleted files on Windows or Mac?
Step 1: Open Command Prompt Windows 10
Here’s the simplest way to open CMD on Windows 10 (and other systems):
- Press Win + S on the keyboard.
- Type cmd or command prompt into the search box.
- Right click on Command Prompt from the search result.
- Select Run as administrator from the context menu.
- Click Yes if you see a User Account Control window.
Attention:
If you don’t run the Command Prompt tool as an administrator, you won’t be able to delete some protected files.
Step 2: Delete Folder CMD Windows 10
In addition to deleting files/folders/directories for you, the CMD tool can also help you recover deleted files on Windows. Please read this page to know how to recover lost files with CMD:
CMD Delete Folder
You should use the “rd” or “rmdir” command to delete an entire folder or directory.
- Use cd to navigate to the location of the folder; type the correct path.
- Type rmdir foldername or rmdir “folder name” if the name contains any spaces.
- Press Enter and wait for the action to complete.
This method will fail if the directory is full or has other subdirectories.
How to delete a directory in this case? You should add the “/s” flag to the command.
- Type rmdir foldername /s to delete a folder/directory whose name has no space.
- Type rmdir /s “ folder name ” to delete a folder/directory containing spaces in the name.
Delete Files with CMD
Delete file command line with the “del” command:
- Navigate to the location of the file you want to delete.
- Delete a file by typing del filename.extension (change filename and extension to suit your case).
- Please type del “file name.extension” if the name contains any spaces.
- Press Enter and wait.
How to force delete a file (a read-only file, for instance)?
You should the “/f” flag to the command: del /f filename.extension or del /f “ file name.extension”.
How to delete all files in the current folder?
Please type del * and press Enter.
How to delete files with the same extension?
Please type del *.extension and press Enter.
How to delete files with the same prefix?
Please type del prefix* and press Enter.
How to delete multiple files simultaneously?
Please type del filename1 filename2 filename3 and press Enter.
Likewise, the files, folders, or directories deleted by using the CMD commands will not be sent to Recycle Bin (click here to know more about the Recycle Bin recovery). Can you recover CMD deleted files? Yes, you can recover from your backups or by using third-party recovery software like MiniTool Power Data Recovery.
MiniTool Power Data Recovery TrialClick to Download100%Clean & Safe
How to recover deleted or lost files on a Windows PC:
Part 5: Remove Directory Linux
How to Delete Files in Linux
You can delete file Linux Terminal by using the following commands.
How to delete a single file in Linux?
Type rm filename or unlink filename and then hit Enter.
How to force delete a file in Linux?
Please type rm -f filename and hit Enter.
How to delete files with the same extension?
Please type rm *.extension and hit Enter.
How to delete files with the same prefix?
Please type rm prefix* and hit Enter.
How to delete multiple files simultaneously?
Please type rm filename1 filename2 filename3 and press Enter.
How to Delete/Remove a Directory in Linux
How to delete an empty folder or directory in Linux?
Please type rmdir foldername or rmdir “folder name” (use this if the name contains spaces) and then press Enter.
How to delete a folder or directory with files and subfolders?
Please type rm -r foldername or rm -r “folder name” and press Enter.
That’s all about removing a directory Linux.
Conclusion
The Terminal is a useful command line tool on Mac to help users perform almost all tasks. In addition to Mac, Windows and Linux also have such tools embedded. This post shows you how to delete a file/folder/directory in Terminal on Mac, with CMD on Windows, or using the command line tool in Linux.
User Comments :