Why does the rm command not remove the file?

24,190

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

Share:
24,190
Admin
Author by

Admin

Updated on May 27, 2020

Comments

  • Admin
    Admin about 4 years

    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!

  • Jonathan Leffler
    Jonathan Leffler over 7 years
    Even root can't modify a read-only file system without remounting it with write options.
  • John Kugelman
    John Kugelman over 7 years
    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.
  • Jonathan Leffler
    Jonathan Leffler over 7 years
    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.
  • Jonathan Leffler
    Jonathan Leffler over 7 years
    @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?
  • EL TEGANI MOHAMED HAMAD GABIR
    EL TEGANI MOHAMED HAMAD GABIR over 6 years
    yes you are totally right the main problem is file name contains extra spaces...that is solution to my problem