Wednesday, May 5, 2010

Adding Right-Click Menu Options in Nautilus (Linux)

Nautilus is a file manager for the Gnome desktop. To enhance its capability, you can add functionality by using an addon package called nautilus-actions. If this package is not installed on your system, please install it. RedHat based systems (including Fedora and CentOS) can use: yum install nautilus-actions.

In this example, I'll add the capability to securely delete (using the shred command) files and folders. This example uses a script (which is listed below).

NOTE: On many modern file systems, the shred command may not be completely and securely delete your files. In particular, shred may not be effective on journaling filesystems such as xfs and ext3, as well as filesystems that write redundant data (RAID), write to cache or create snapshots.

Once the package is installed, type "nautilus-actions-config" and the menu editor will appear. Select the Add button and a dialog box will appear. I entered the following values:
  • Label: Shred
  • Tooltip: Securely delete files and folders
  • Icon: gtk-delete
  • Path: /usr/local/bin/shredFiles
  • Parameters: %M
The %M adds the full pathnames of all files and folders that are selected.

On the Conditions tab, I selected the "Both" option and checked "Appears if selection has multiple files or folders".

On the Advanced Conditions tab, I selected the "file" and "smb" options.

Close out of the dialog box, and from a command line type: "nautilus -q" to quit the nautilus file manager. This forces it to read its configuration files again. Start by typing "nautilus" in a command line.

Select one or more files and folders and right-click. Your newly created Shred command should appear. Simpy select the Shred command while the files/folders are selected and they will be securely deleted using the shredFiles script.

The listing of shredFiles is below. Don't forget to add execute permission on the /usr/local/bin/shredFile script!

#!/bin/bash

function _shredFilesOrFolders() {
    if [ $# -ne 1 ]; then
        echo usage: _shredFiles foldername|filename
        exit 1
    else
        arg=$1
        if [ -f $arg ]; then
            chmod 0700 $arg 2> /dev/null
            /usr/bin/shred -fzu $arg
        else
            chmod -R 0700 $arg 2> /dev/null
            find $arg -type f -execdir /usr/bin/shred -fzu {} \;
            /bin/rm -rf $arg
        fi
    fi
}

####################################################

if [ $# -lt 1 ]; then
    echo "usage: shredFiles foldername|filename"
    exit 1
fi

for i in $(seq 1 $#); do
    f=$(eval echo \$$i)
    echo -n "Shredding $f..."
    _shredFilesOrFolders $f
    echo "done"
done