Why does the rm command not remove the file?

Stephan picture Stephan · Nov 13, 2016 · Viewed 18.6k times · Source

When i today accessed my Ubuntu 16.04 server and wanted to remove the file "test2" it was simply not deleted!

I have used

rm test2

as well as

rm -f test2

but it still did not delete it as you can read here:

root@icinga:~# ls
basket  desd.save  packages  scripts  src  test2  test5  unused
root@icinga:~# rm test2
root@icinga:~# ls
basket  desd.save  packages  scripts  src  test2  test5  unused
root@icinga:~# rm -f test2
root@icinga:~# ls
basket  desd.save  packages  scripts  src  test2  test5  unused

I have also tried to remove other files, didn't work!

I am the owner of "test2" and using ls -la test2 you can see that I have the rights to read and write this file!

root@icinga:~# ls -la test2
-rw-r--r-- 1 root root 9 Nov 11 20:33 test2

Using which rm it says /bin/rm.

root@icinga:~# which rm
/bin/rm

And also \rm test2 does not delete the file!

I have also checked for the name, there are no spaces at the end etc. because when I use cat test2 the correct content is shown!

I also can create a new file but can't delete this as well.

rm is also not an alias, I used unalias rm but it said "rm: not found".

Reboot did also not help.

I had the problem that I accidently deleted a file instead of moving it, so I created a script that simply moves the file to a certain directory.
Then I used nano /etc/environment and added ":/root/scripts" where this script was located!
After that i created the alias rms by using alias rms='./rm'. I know it might be dumb naming a file like a system command, I already changed it to remove!

But after doing all this there was the Error that rm can't be found and can be found in the following packages: coreutils. So i tried apt-get install coreutils but it said it is already installed.
So I first used touch /bin/rm and then chmod +x /bin/rm.

After that this problem occured!

EDIT: the problem was the /bin/rm file was empty so I set up an virtual machine and copied the required file to the server!

Answer

Jonathan Leffler picture Jonathan Leffler · Nov 13, 2016

To remove a file, you need to be able to modify (write) in the directory that contains the file. If the file isn't deleted, then you probably don't have permission to write on the directory. This could be because the file is on a read-only file system, but it is more likely that you do not have write permission on the directory. Using rm -f suppresses error messages (and prompts).

One other possibility (probably not the case here), is that the file name has a space or other invisible character at the end, and the name you specify as a file doesn't actually exist (the file is "test2" and not "test2"; or maybe it is "test1<bs>2" where the <bs> represents a backspace, or … there are endless ways to run into problems).

Rerun rm test2; respond to the prompt; look at the error messages.

Or run ls -ld . in the directory containing the file and look at the permissions, but remember that ACLs (access control lists) and extended attributes can make it harder to work out what your permissions are (though again, they're unlikely to be a factor in the problem).