Tuesday, April 17, 2018

Remove Folder/File by Using Terminal

Normally, you can remove files/folders from Finder just by selecting files/folders first and then hitting "delete" key. However, I encountered a special case that I could not remove a folder in Finder. I got a prompt message saying that the folder is in use. Actually this is a misleading message. The folder is not in use and I could not delete it even after I tried to restart my mac.

Further investigation, I found that the folder is in kind of nested loop endless. It is in my Trash. I don't know why it is like that.



Full Path to the Folder


The first thing I need is to find the path to the folder. The file/folder full path is not obviously available in Finder.

There is a way to obtain the full path from folder's Info. Just locate the folder in Finder, and hit Command+i combination keys. This will bring up an info window.

For example, the following is the info window for the folder in my Trash.




Highlight Where section, kit Command+c keys to make a copy. This will copy the full path of the folder to clipboard, ready for use.

Terminal to Rescue

Use spotlight to open Terminal. This can be easy done by pressing Command+space keys, and type in terminal in spotlight search area. Press enter key to open Terminal.

I remember that there is a command tool to remove folders. It is the command of rm.

First I have change current location to the path where the folder is. Use the following command cd to change the current location or directory or path:

  1. cd /Users/dchu/.Trash

Before removing the folder, I have to make sure the folder is there. Use ls command to show the content of the current directory, with -F switch to show folder:

  1. ls -F
  2. iPhoneDev copy 6.00.02 PM/

Now it is time to remove the folder. Use rm command with -R switch. This switch will force to remove the folder as well as any contents, such as files and folders, within the folder.

  1. rm -F iPhoneDev\ copy\ 6.00.02\ PM/
Tip: when you type in folder's name, you can just type in a few characters like "iPho" first, and press tab key to get the complete name filled out.

Kit enter key and the folder is gone!

Note: there may be spaces within file or folder names. The spaces in a file or folder name can be specified by escape char \, just like above example.

References





No comments: