Deleting files as root in Nautilus

I’ve just seen the title of this post in my blog stats as search-term hit to my blog, and while they guy searching didn’t find his answer here (He got directed to my Nautilus Garbage Bin post), I decided to make sure the next one searching for that will find a decent answer.

I must admit that I firmly believe that root’s work should be done from the command-line, however, if a guy insists on doing it this way, well so be it, this is, after all, Linux, the OS of the free…

So basically I can see 2 ways of going about deleting files as root with Nautilus:

  1. Run the entire Nautilus with gksudo.
  2. Using a Nautilus script to run the rm command with root permissions (again, using gksudo)

While the first method seems to be fairly straight-forward, I’d recommend against it, Nautilus is a huge hunk of code, exactly the wrong thing to be running as root, you don’t want to hose your system because of some nautilus bug, do you? this can also have all kinds of other unexpected side-effects such as having image thumbnails created in the user’s home directory with root file permissions, thereby disabling the user from removing them easily.

In order to accomplish deleting files with the 2nd method, one needs to write a Nautilus script and place it in the “.gnome2/nautilus-scripts” directory in his home directory, I’ve provided an example for such a script here.

To install and use my script you don’t need to go to the command line, just follow the following simple steps:

  1. In Nautilus, type ctrl+L to present the location bar.
  2. In the location bar, type “~/.gnome2/nautilus-scripts” and press the “Enter” key, this will take you to the Nautilus scripts directory.
  3. In the directory, right-click on a blank space and select “Empty File” from the “Create Document” sub menu.
  4. Give a meaningful name to the file you’ve just created, for example, “delete_as_root.bash”, the name will later appear as an entry in Nautilus` right-click menu.
  5. Right click the file and select “Open with Text Editor”, this will open the file for editing in your preferred text editor, which would probably be Gedit, unless you configured your system otherwise.
  6. Copy and paste the script from my site into the Editor, take care to copy all and only the numbered script lines.
  7. Delete the line numbers, I’ve put then there to make it easy to discuss the scrip, but it won’t run with them inside, make sure you don’t leave spaces before the beginning of the actual text in the lines.
  8. Save the file and exit the editor.
  9. Right click the file, select “Properties” and check the “Allow executing file as program” check box in the Permissions tab.
  10. Thats it! in a few minute you’ll find a new “Scripts” sub menu, when right-clicking a file in Nautilus, that menu should contain the name of the file you’ve just created, clicking it will run the script and delete the file or files you currently have selected in the Nautilus window.
  11. If you’re impatient, an can’t wait until the script will appear on it own, you can log out back into you user account.

If this seems to you like a rather long and tedious process, I must agree, this could be done a whole lot faster from the command line, but hey, if we wanted to use the command line here we would’ve just “sudo rm”-ed the files in the first place and be done with…


5 thoughts on “Deleting files as root in Nautilus

  1. rm -f without sudo would have done the trick. I believe for a long time that this rm -f behaviour ought to be added to gui file managers.


  2. Running “rm -f” with the permissions of a regular user would work only in the special case in which the root-owned files are within a directory which is writable by that user. That is because in order to delete a file you need write permissions for the directory in which it resides and not the file itself.
    The fact that you need “-f” in this case, is just a safety measure added to the GNU version of “rm” and not something enforced by the permission system.

  3. This post is the first hit on google when searching on “nautilus delete as root script”. I visit here everytime I install Ubuntu (quite often). Please don’t ever delete this post!

    thanks so much for writing it

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s