Why does the rm command not remove the file? [closed]
Asked Answered
P

1

8

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!

Piccalilli answered 13/11, 2016 at 17:41 Comment(15)
please show a screenshot of ls -l test2Shrum
What does type rm print? rm might be a broken alias or shell function.Reiser
or which rm too?Shrum
maybe your filename ends with whitespaces and the console doesn't highlight whitespaces... you could try writing rm -f test2 and before hitting enter, hit the tab key (for filename autocomplete); otherwise, rm -f test2* would for sure take care of it =)Montero
@Markus would you not expect a No such file or directory error in that case?Shrum
Would lack of execute permission on the directory stop deletion of the files but still allowing listing of files within it?Shrum
There was no error, it's a success operation; the file that was provided as argument doesn't exist so it's definitely deleted, why would there be an error message for that?Montero
@Markus if the issue was whitespace then running rm on a file that doesn't have the whitespace (when it should) would fail and therefore output the No such file or directory. This to me suggests that it is not a whitespace issue.Shrum
@JonTaylor: lack of execute permission on the directory would prevent you from accessing (reading the contents of) the files in the directory. I don't think it would prevent you removing the file; the lack of write permission on the directory definitely would prevent you removing a file.Madelynmademoiselle
The OP is root, though.Reiser
@JohnKugelman just because the user is named root doesn't mean it has any special privileges. Granted that it probably does, but should definitely not be assumed.Shrum
touch /bin/rm -- you created an empty script named rm. You're calling that script, a script which does nothing. touch is not the answer to a missing executable!Reiser
@StephanSchrenk: What do you get when you say type rm ?Puppetry
@JohnKugelman this is the problem, yes.. I looked up this file and it's empty! I could fix this by simply setting up an virtual machine and copying the "rm" file to the server! Thanks for your help guys!Piccalilli
@JonTaylor, ...we spend so much time here trying to get people to post text, not screenshots; please don't undo that work. (Screenshots are behind links that bitrot; screenshots can't be copied-and-pasted; screenshots can't be indexed for search; screenshots can't be inspected to distinguish between characters that render identically but behave differently; etc). See for example meta.#304312Wilinski
M
3

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

Madelynmademoiselle answered 13/11, 2016 at 17:51 Comment(5)
Even root can't modify a read-only file system without remounting it with write options.Madelynmademoiselle
The OP tries rm before rm -f. If the problem is rm is aliased to rm -f, they'll need to try \rm to avoid the alias.Reiser
I have unprintable views on people who alias rm, especially for root. This is, however, meant to remain a SFW (as opposed to NSFW) venue.Madelynmademoiselle
@StephanSchrenk: If you used to be able to remove files and can't now remove files, something changed. What? Track that down. Have you tried a reboot? It sounds corny — it is corny — but sometimes it works. Have you analyzed the file system holding these files? Have you checked the mount options that were used on it? Can you create new files? Can you remove the new files you create? Have you looked at ACLs on the directory or its parent directories?Madelynmademoiselle
yes you are totally right the main problem is file name contains extra spaces...that is solution to my problemCalcite

© 2022 - 2024 — McMap. All rights reserved.