Can't remove a directory in Unix
Asked Answered
B

8

37

I've got a seemingly un-deletable directory in Unix that contains some hidden files with names that start with .panfs. I'm unable to delete it using either of these commands:

rm -R <dir>
rm -Rf <dir>

Does anyone have any suggestions?

Bess answered 5/6, 2013 at 18:48 Comment(5)
what error are you getting?Stronghold
There's no error - it just doesn't complete - even though the files are 0kb...Bess
try changing the permission 'chmod +w <dir>' and then try removingStronghold
Does it not complete, at all, ever, even if you wait? Or is it just slow for some reason, leading you to impatiently type ^C? You can try strace rm -R <dir> to watch what it is actually doing.Monies
The hidden .panfs files are a result of having a mounted directory. When they are present they are "open" and can't be removed. I have found that rebooting or unmounting removes these files, and then you can delete the files.Auricular
E
73

Try to delete it with root user or use sudo, if you are in trouble

Use rm -rf dir with root account and it will be deleted, since you should be facing a permissions issue.

Earthy answered 5/6, 2013 at 22:57 Comment(3)
@user3817989 are you sure you are issuing the command as root? What 'whoami' command says?Earthy
@KalenGi you need to have permission on 'dir' to remove it, otherwise you really need to be root (or the owner).Earthy
@PauloFidalgo Not working for me, says some kind of rm: cannot remove 'foldername' Input/output errorMotta
C
6

To those who prefers to separate the options for a full mastering of their linux command lines so

rm -r -f directory_name

rm → remove

-r → recursively

-f → force (includes chmod permissions)

Cistern answered 1/8, 2018 at 11:7 Comment(0)
P
5

Check with df dir and mount how is your directory mounted and to which file system it belongs to. Notice that if you use NFS, CIFS/SMB, or some other distributed file system, you could have issues... since distributed file systems are caching (both server side and client side) so don't have POSIX semantics. See filesystems(5).

Very probably you are using NFS (then your question should say that, and give much more details, notably mount and export options in /etc/fstab, see fstab(5), version of NFS protocol used, etc...). Then you need to give more details about how it is exactly mounted, if you have processes using that file system (use lsof(8)...), and how authentication works. Quite often, root access does not exactly work thru NFS as you want it to... (intuitively your local root is not a network-wide root).

In some cases, you need to remove files on the NFS server after having unmounted that remote NFS file system on all NFS clients. And details vary with version of NFS protocol used and configuration options.

See also nfsd(7), exports(5), chattr(1) etc and this question on Serverfault, and this Linux NFS overview.

Pitchy answered 23/12, 2017 at 11:55 Comment(0)
G
3

Sorry, but voted 20+ approved solution didn't work for me :) but I nailed the sucker.

In my case, under root, rm -rf (directory) leads to an infinite loop, and size of the folder is under a gig. Furthermore, the folder is non-listable that is using the dir command within the folder also leads to infinite loop.

Oh Hell no!!!

Enter recovery mode by holding on to left shift at boot. Provide your root password or press enter if there is none.

cd /

mount -o remount,rw /

rm -f (directory)   // Purpose is to fix loop bug

rm -r (directory)

See ya!

All hail Linux Lite.

Godfry answered 19/5, 2016 at 17:12 Comment(0)
L
3

Syntax :

rm -rf <Directory_Name>

It worked for me. It will remove the directory with all its content ...(forcibly)

Leman answered 22/6, 2018 at 14:39 Comment(0)
T
1

I recently could not delete a folder created by a crazy going backup job on a QNAP. It created sub/sub/sub/sub/sub/.... folders until it couldn't.

rm -rf sub
rm -rf sub/sub/sub/sub/sub/sub/sub

all rm commands failed with: can't remove 'sub/sub/sub/sub': Directory not empty

I tied to rename some folder to make the path shorter, but it didn't help. But after moving one of the subfolders, I was able to delete all.

mv sub/sub deleteme
rm -rf deleteme
rm -rf sub
Tamarisk answered 26/11, 2020 at 12:18 Comment(0)
S
1

In my case, I was not able to delete a folder, and If I tried to rename it a new copy was created.

Long story short, I realized that there was a Linux service pointing to that folder, so what worked for me was:

  1. Change the folder that this service was using
  2. Reboot my Linux server
sudo reboot
  1. Delete the folder with its content
rm -rf dir

Probably there is a chance that you are not aware of a process that is using that folder, so you can find and kill that process or just reboot the server as I did as a quick solution.

Sperm answered 20/10, 2021 at 10:19 Comment(0)
D
0

If you're encountering an issue while trying to remove a directory using rm , it might be due to permissions or the presence of files within the directory. Make sure you have the necessary permissions to delete the directory and all its contents. If the directory contains files or subdirectories, you'll need to use the -r flag with the rm command, like this: rm -r . Be cautious when using this command, as it will permanently delete the directory and its contents.

Also, the command rm -rf works because the -r flag is used to delete the directory and its contents recursively, and the -f flag forces the removal without asking for confirmation.

Remember to be careful when using the -rf combination, as it doesn't provide any confirmation before deletion.

Dodd answered 14/8, 2023 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.