3

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 ‘./’
3
  • Try, rm -rf ./* Commented Mar 27, 2015 at 8:44
  • 6
    The error message suggests exactly the opposite of what you say it does. Commented Mar 27, 2015 at 8:45
  • 1
    Very relevant (duplicate?): Does 'rm .*' ever delete the parent directory? Commented Mar 27, 2015 at 11:52

2 Answers 2

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.

-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.

7
  • Appreciate your help, but even though the CWD may not be deleted, why aren't any of the directory files/dirs deleted? Commented Mar 27, 2015 at 8: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. Commented Mar 27, 2015 at 8:53
  • 1
    @celada -- I think howard asked for deleting the contents of the present directory not the present directory. Commented Mar 27, 2015 at 8: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. Commented Mar 27, 2015 at 8:56
  • 6
    You can delete the current directory. Just not with rm -rf . Commented Mar 27, 2015 at 9:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.