JandD
JandD

Reputation: 13

I cannot use rmdir to remove a directory as there seems to be a file in the directory with the same name that cannot be deleted

Hi guys I am a complete beginner to programming and have tried to google this answer but with no joy.

I am following Zed Shaw's "Command Line Crash Course " and have hit a wall with rmdir.

I use a mac and know to delete hidden files like .DS_Store and have done so for my directory. However, it still says the directory is not empty and ls -la seems to show a file that shares the directory name:

See below for directory joe

drwxr-xr-x  3 MyLaptop staff  102  1 Apr 16:52 .

drwxr-xr-x  4 MyLaptop  staff  136 31 Mar 22:32 ..

drwxr-xr-x  3 MyLaptop  staff  102  1 Apr 16:51 joe

I have tried to remove this file but it will not allow me to and I cannot remove the directory, can anyone's suggest a solution?

Upvotes: 0

Views: 11657

Answers (2)

user6124839
user6124839

Reputation: 1

You can also use

$ rm -r joe

The -r option to the rm command will recursively delete the contents of the directory, and the directory itself.

From the rm man page:

     -r, -R, --recursive
     remove directories and their contents recursively

Upvotes: 0

Bakuriu
Bakuriu

Reputation: 101909

That is not a file. The d at the beginning of a line tells you that it is a directory. In fact it is the exact directory you are trying to delete.

The rmdir command requires that the joe directory be empty. To check if joe is empty use:

$ ls -la joe

Or move into that directory first:

$ cd joe
$ ls -la

You are instead using ls -la in the parent directory of joe, and hence joe itself is showing up in the contents.

So check which files are inside joe and delete them using rm. The . and .. entries don't really count as these are references to the current and parent directory.


Note that rm can also delete directories, so you could simply do rm -r joe to delete all the files inside joe and the joe directory itself.

Upvotes: 3

Related Questions