Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is it possible even as root one cannot remove directory? What could be the scenarios for directory not getting deleted?

Well nothing wrong with the file system.

There are applications running on the system perfectly.

Trying to delete directory with files and is not a mount/readonly.

Is that something to do with someone has reference to it or is in use not sure?

I am trying this from a shell script and cannot see the error message and need badly to get rid of this directory.

share|improve this question
    
Do you get an error message when you try to delete a directory, and if so, what is that error message? –  Pourquoi Litytestdata Apr 15 '09 at 21:13

9 Answers 9

Various possibilities:

  • File system mounted read-only
  • Immutable bit set (system specific)
  • From chaos pessimism: file system damage
  • From MarkR's good thinking: if it isn't empty, you can't delete it. Use ls -A to check for hidden files...
share|improve this answer

NFS normally excludes all write access for root (can be enabled with the no_root_squash mount option (but do not do this unless you absolutely trust absolutely all machines on your network; NFS is sometimes referred to as No File Security, and there is a reason for root being banned by default)).

share|improve this answer

Linux has regular permissions (read/write/execute) as well as extended attributes, which are less commonly known.

One of these is the immutable attribute which prevents even root from modifying the file.

The immutable bit is set with chattr +i file and unset with chattr -i file

Another one of these is the append attribute which only allows you to append to the file (not sure how this works with directories)

The append bit is set with chattr +a file and unset with chattr -i file

share|improve this answer

File system damage.

share|improve this answer

A non-empty directory cannot be removed with rmdir even if you're root. Also any other general problem such as readonly file system, io error etc.

share|improve this answer
    
Sure. Notice the obvious case. Hmph. If your going to be that way...+1 –  dmckee Apr 15 '09 at 21:13

To rm a directory that contains something inside of it, you have to do a recursive remove. On my ArchLinux machine it would be $ rm -rf /path/to/dir.

Be careful, very careful.

$ rm -rf / your system and bye bye hd without any warnings.

share|improve this answer
    
That's why I alias rm with rm -I. –  Bastien Léonard Apr 15 '09 at 21:22

Hum... OK, a few basic cases, some of them having been mentioned (hence the community wiki flag):

  • you are using rm without the -r arg

    rm -r directory
  • you are trying to delete from a readonly filesystem

    mount -o remount,rw /mount/point 
  • you are trying to delete from an NFS partition with root-squash

    (nothing you can do here, unless you are allowed to change the settings on the NFS server)

  • you are trying to delete from a read-only media (e.g. disk with the read-only pin set, mmc with lock on, etc...)

    Change the physical protection (may need a reboot)

share|improve this answer

Removing the directory . does not work:

$ sudo rm -rf .
rm: cannot remove directory `.'

Click here to see how to delete the current working directory

share|improve this answer
    
sorry ;-), fixed. Didn't paste the url but the links label as URL ;-) –  Johannes Weiß Apr 15 '09 at 21:30
    
Happens all the time :) –  avakar Apr 15 '09 at 21:32

Here are a couple things that come to mind:

  1. The directory or files within it are in use - you can use lsof (or something similar) to see if anyone has an open handle to something you're trying to delete
  2. The directory or files within it has special characters that need to be escaped to delete
  3. The directory or files within it has spaces that need to be escaped to delete
  4. The script is currently running in the directory you're trying to delete
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.