3

How do I force Nautilus to demand yes/no confirmation after hitting the delete key?

I am aware of the "Files"->"Preferences"->"Behaviour" option: "Ask before emptying the rubbish bin or deleting files" but it is checked and only applies on emptying Trash.

Is there a way to get a confirmation window before files are moved to Trash?

Background
The major problem I have is that I sometimes accidentally hit the delete key on my keyboard without even noticing. I'm sure it has happened and I can't live with the idea of looking through my trash folder to check if I am not going to throw away something I didn't mean to.

12
  • Just thinking over options a bit. There are options, but they would need to be created. Would you like the feature for all block devices (if you have more than one) or is the "main" directory (your home directory) enough? – Jacob Vlijm Feb 23 '15 at 11:03
  • Block devices? Do you mean a file? Anywhere the user is able to delete stuff, I would find it much safer to have their action subject to confirmation. – Adam Feb 23 '15 at 14:25
  • 1
    block devices = drives/partitions, but never mind, I'll post ana answer within half an hour or so. – Jacob Vlijm Feb 23 '15 at 16:18
  • 1
    You mean "Nautilus" not "Unity" @adam – Rinzwind Feb 23 '15 at 17:18
  • 1
    @Xweque Note that OP mentioned he does not always notice he is pressing Delete. He needs to be notified somehow. – Jacob Vlijm Feb 23 '15 at 19:38
2

No, Unity does not have a confirm delete box like windows. Thats the reason the bin exist, so if you did make a mistake you can undo it by hitting restore.

2
  • 1
    Argh! OK. Thanks. The major problem I have with that though is that I sometimes randomly hit the delete key on my keyboard without even noticing. I'm sure it's happened and I can't live with the idea of looking through my trash folder to check I am not going to throw out something I didn't mean to. – Adam Feb 22 '15 at 22:41
  • That is pretty nasty reasoning. Bin exists so you can go and "undelete" files you've purposedly deleted not for accidental deletions you may not even notice since linux is so fast.... -_- – jave.web Feb 6 at 23:56
1

Nautilus does not provide a confirmation option: solution?

The fact that nautilus does not provide an option, does not mean that you have to take the risk to accidentally lose files if you unintentionally (and unnoticed) hit the Enter key.

You can simply run a script in the background, keeping an eye on delete actions, using the trash-cli -tools. If you run the script below in the background, it does two things that will prevent unnoticed deletion

  • It notifies you whenever you move a file to trash:

    enter image description here

    If you click "OK", nothing happens. If you click "Cancel", the trash will be opened and you can restore your item, since you know the name and the original path to the file.

  • It keeps track of the last 50 trash actions (one per line)
    In your Home directory, the script creates a log file, trashlog.txt, with the last 50 trash actions. This makes sure that you can track what happened, even if you accidentally hit the "OK" button.

    enter image description here

    It logs all trash events with a time stamp, like:

    Tue Feb 24 07:30:55 2015: '/home/jacob/Naamloze map/Naamloos document (11e kopie)'
    Tue Feb 24 07:31:09 2015: '/home/jacob/Naamloze map/Naamloze map'
    

Explanation

In a loop, the script checks the list of deleted files with the help of trash-cli, with the command:

trash-list

The command lists all delete actions, also from e.g. external drives.

Whenever new items appear in the list, the script calls a Zenity window, listing the (newly) deleted files. At the same time, these files are listed into a log file: ~/trashlog.txt. To prevent the file to become to large, it only "remembers" (a little more then) the last 50 delete events.

The Zenity window returns a "non-zero" when the Cancel button is pressed, and the script opens Trash (actually trash:///) to enable the user to restore the file.

How to use

  1. The script uses trash-cli

    sudo apt-get install trash-cli
    
  2. Copy the script below into an empty file, save it as trash_secure (no extension) in ~/bin. Make the directory if it does not exist yet.

  3. Make the script executable.
  4. Log out and back in, test-run the script from a terminal eindow with the command:

    trash_secure
    
  5. If all works fine, add it to your Startup Applications: Dash > Startup Applications > Add.

The script

#!/usr/bin/env python3
import subprocess
import os
import time

home = os.environ["HOME"]
trashlog = home+"/trashlog.txt"

def trashlist():
    return subprocess.check_output(["trash-list"]).decode("utf-8")

def log_trash(last_trashed):
    if not os.path.exists(trashlog):
        with open(trashlog, "wt") as log:
            log.write(last_trashed)
    else:
        with open(trashlog, "r") as log:
            log = log.readlines()
            log = log[50:]+[last_trashed] if len(log) > 50 else log+[last_trashed]
        with open(trashlog, "wt") as out:
            for it in log:
                out.write(it)
            out.write("\n")

trash1 = trashlist()
while True:
    time.sleep (1)
    trash2 = trashlist()
    if trash2 != trash1:
        diff = (" ").join(["'"+l[l.find("/"):]+"'" for l in trash2.splitlines() if not l in trash1])
        log_trash(time.ctime()+": "+diff)
        zenity_message = "zenity --list --width=600 --height=200 --title='Items were trashed' --text='' --column='Path' "+diff
        try:
            check = subprocess.check_output(["/bin/bash", "-c", zenity_message]).decode("utf-8")
            if check != "":
                subprocess.Popen(["nautilus", "trash:///"])
        except subprocess.CalledProcessError:
            subprocess.Popen(["nautilus", "trash:///"])
    trash1 = trash2

Notes

  • The script also tracks trash actions from possibly attached external drives. If one of these drives has a space in its name, the script will break. I had to rename my external drive, named "My Passport", to give an example. That is not caused by the script itself, but by the trash-cli tools the script uses.
  • The most elegant way would be (of course) to directly "untrash" the files if you pressed "Cancel". The trash-cli tools do not provide an option to do that from the command line however, other than an interactive one.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.