2

The command rm -rf ./ does not do anything in a directory full of sub directories and files. Why so? Isn't -r supposed to go recursive?

To add more confusion, it even prints an error message suggesting that it is traversing the directory:

rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘./’
Howard
  • 5,209

2 Answers2

6

The rm command refuses to delete the directory by the '.' name. If you instead use the full path name it should delete the directory recursively, i.e. rm -rf `pwd`‌.

It is also possible to delete the directory if it is the current directory.

[testuser@testhost] /tmp$ mkdir ff

[testuser@testhost] /tmp$ cd ff

[testuser@testhost] /tmp/ff$ touch a b c

[testuser@testhost] /tmp/ff$ rm -rf ./ rm: cannot remove directory: ‘./’

[testuser@testhost] /tmp/ff$ ls a b c

[testuser@testhost] /tmp/ff$ rm -rf /tmp/ff

[testuser@testhost] /tmp/ff$ ls

[testuser@testhost] /tmp/ff$ ls ../ff ls: cannot access ../ff: No such file or directory

[testuser@testhost] /tmp/ff$ cd ..

[testuser@testhost] /tmp$ ls ff ls: cannot access ff: No such file or directory

From info rm:

Any attempt to remove a file whose last file name component is .' or ..' is rejected without any prompting.

malthe
  • 115
Lambert
  • 12,680
-1

You cannot remove the current directory because then the current directory would become invalid.

First, change out of the directory you want to remove (e.g. cd ..) and then remove the desired directory using its full or relative pathname from outside.

Celada
  • 44,132
  • Appreciate your help, but even though the CWD may not be deleted, why aren't any of the directory files/dirs deleted? – Howard Mar 27 '15 at 08:47
  • 2
    It's pretty simple. You asked rm to do something it cannot do. Not only is rm not doing it, but it's telling you why not with an informative error message. To delete a directory (recursively or not), your current working directory should be outside of that directory. – Celada Mar 27 '15 at 08:53
  • 1
    @celada -- I think howard asked for deleting the contents of the present directory not the present directory. – vipin kumar Mar 27 '15 at 08:54
  • 1
    @vipinkumar: No. Howard asked for deleting the present directory. You suggested an alternative in your comment that deletes the contents of the present directory. – Celada Mar 27 '15 at 08:56
  • The message says skipping, so, yes, exactly like it says, it skips it and moves on to the next argument (but there was only one argument given, so instead it exits). – Celada Mar 27 '15 at 09:01
  • 6
    You can delete the current directory. Just not with rm -rf . – Chris Davies Mar 27 '15 at 09:39
  • @roaima yeah, you're right, that's true. I advised exiting that directory and setting the cwd to something else instead because deleting the current directory leaves you in a weird state (in a directory that does not exist anymore) which could be unintuitive and confusing. – Celada Mar 27 '15 at 12:54