• Linkedin
  • Reddit

Summary :

How to delete a directory in Terminal

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.

Quick Navigation :

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.

Tip: MiniTool Solution provides several tools for you to recover deleted/lost files from different platforms. You should choose the correct one when your important data are lost but you need them back.

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.

Move to Trash

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?

  1. Open Finder.
  2. Navigate to the locked file.
  3. Control click the file or press Command + I.
  4. Select Get Info.
  5. Uncheck the Locked option under General.
Note: You can’t find some files in Finder since they are hidden somehow. For example, the files containing settings for certain apps or parts of macOS, or containing a dot (.) before their names will be hidden on Mac.

How to delete hidden files on Mac?

  1. Open Finder.
  2. Look for Locations.
  3. Click on your hard drive.
  4. Open the Macintosh HD folder.
  5. Press “CMD + Shift + .” to show the hidden files.
  6. Repeat this process to show hidden files in other folders.
mac file recovery thumbnail
[SOLVED] How To Recover Deleted Files On Mac | Complete Guide

Believe it or not, we can make it easy to recover deleted data on Mac even if you are totally a novice.

Read More

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?

windows explorer needs restarted thumbnail
[SOLVED] Windows Explorer Needs To Be Restarted: Problem Fixed

It’s easy to encounter Windows Explorer needs to be restarted issue on Windows; this post tells you how to fix it properly.

Read More

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.

  1. Open Finder as you usually do.
  2. Browse the items and select Applications.
  3. Look for the Utilities folder and open it.
  4. 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.

Open Terminal from Finder

Method 2: search for Terminal.

  1. Navigate to Dock on your Mac.
  2. Look for the Launchpad icon and click on it.
  3. Type terminal into the search field you see.
  4. Click Terminal from the search result to open it.

Find Terminal through Launchpad

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

  1. Open Terminal.
  2. Type rm -R directoryname to delete a directory when the name doesn’t contain space.
  3. Type rm -R “directory name” to delete a directory if spaces are found in its name.
  4. 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.

Note: Another thing you should notice is that you can’t use the -i flag in the rmdir command when deleting a folder.

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.

Click to tweet

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.

Free Download

Stellar Data Recovery for Mac

Step 2: How to Recover Deleted Data on Mac

Please follow this guide to recover files and folders deleted by Terminal.

  1. Run the Stellar Data Recovery for Mac.
  2. 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.
  3. Click on the Next button to continue.
  4. Specify the drive where the lost data are saved in the Select Location For instance, you may select macintosh HD.
  5. 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.
  6. Browse the found files & folders and check the ones you need. Then, click Recover at the bottom right.
  7. Click Browse in the Recover window to select a safe location for keeping the recovered data.
  8. Click on the Save button to confirm. The recovery process will be started immediately.
  9. 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?

Select What To Recover

Select Location

Recover lost files

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):

  1. Press Win + S on the keyboard.
  2. Type cmd or command prompt into the search box.
  3. Right click on Command Prompt from the search result.
  4. Select Run as administrator from the context menu.
  5. Click Yes if you see a User Account Control window.

Open CMD

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:

recover files using cmd thumbnail
How To Recover Files Using CMD: Ultimate User Guide

This page will show you how to recover files using CMD easily. You can recover data from USB pen drive, hard disk or other storage devices by command line.

Read More

CMD Delete Folder

You should use the “rd” or “rmdir” command to delete an entire folder or directory.

  1. Use cd to navigate to the location of the folder; type the correct path.
  2. Type rmdir foldername or rmdir “folder name” if the name contains any spaces.
  3. 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.
Note: If the Command Prompt tool asks you whether you want to delete the folder or directory, please press “y” to confirm.

Delete Files with CMD

Delete file command line with the “del” command:

  1. Navigate to the location of the file you want to delete.
  2. Delete a file by typing del filename.extension (change filename and extension to suit your case).
  3. Please type del “file name.extension” if the name contains any spaces.
  4. 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.

Note: If the Command Prompt tool asks you whether you want to delete a file (or files), please press “y” to confirm.

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.

Free Download

How to recover deleted or lost files on a Windows PC:

retrieve deleted files on pc thumbnail
How To Easily Recover Deleted/Lost Files On PC In Seconds - Guide

Don’t know how to retrieve deleted files on PC? The following content will show you how to get them back from devices in different situations.

Read More

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.

Note: If the terminal tool asks you whether you want to delete a file (or files), please press “y” to confirm.

How to Delete/Remove a Directory in Linux

How to delete an empty folder or directory in Linux?

Please type rmdir foldername or rmdirfolder 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.

Click to tweet

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.

  • Linkedin
  • Reddit

User Comments :