Shell scripting for Nautilus

It has been a while since I wrote my last post. Sorry for the delay, but I was a bit busy lately. In this post, I shall explain how to get the most of your nautilus file manager by using shell scripts.

Nautilus provides some facilities available from shell scripts. Combining them with a small utility called zenity can improve your daily tasks.

Nautilus has the ability of executing shell scripts applying them to the selected files. The executable scripts are those present in the following directory:


Every script present in that directory will be shown in the “Scripts” entry of the contextual menu that shows up by right-clicking on a file. If that entry is not shown, navigate into the directory mentioned above in Nautilus and you will see a message indicating that all the scripts in the folder will be available from now on.
Nautilus sets up four environment variables for the process executing the script which are:




NAUTILUS_SCRIPT_WINDOW_GEOMETRY: size and location of the current window

In addition, the names (without the full path) of all the selected items are passed to the script as input parameters.

These scripts will be executed silently (not in a terminal window) so it seems reasonable to be able to communicate with the user. Enter zenity.

Introducing zenity
zenity is a program for displaying GTK+ dialogs. It reads data from stdin and returns user input through stdou. It is an easy way of gathering and showing information from a shell script on a desktop environment. Here are some examples on how to use zenity:

Show an information window:

$ zenity --info --title=InfoWindow --text="This is an information text"


Get some user input:

$zenity --entry --title=EntryWindow --text="Type a text, please"


Hello World!

As you can see, zenity provides an easy way to interact with the user in a graphical environment.

With this little information, it is possible to write a small debugging script. I called it

#! /bin/bash
# Use /usr/local/bin/bash on FreeBSD

ZENITY=$(which zenity)

$ZENITY --info --text="$*"


This script shows the contents of the parameters and the Nautilus variables.

zenity is really easy to use, and with a little effort and some bash scripting knowledge (that I will not explain, because it is out of the scope of this post) you can write a convenience script like the one below (I named it

#! /bin/bash
# Use /usr/local/bin/bash on FreeBSD
# Small script intended to be used from Nautilus.
# It compresses the selected files and/or directories
# in several formats
# Requires: zenity, tar, gzip, bzip2, zip, mkisofs

FORMATS="tar tgz tbz2 zip iso9660"
ZENITY=$(which zenity)

# Check if we have selected any files...
        $ZENITY --error --text="No files selected"
        exit 0;

# Ask for output file name
output_filename=packit-$(date +%s)
output_filename=$($ZENITY --entry --title="Output file name" --text="Type file name" --entry-text="$output_filename")

# Ask the user to select a compressing format
selected_format=$($ZENITY --title="Select format" --list --column=Format $FORMATS)

if [ -z "$selected_format" ]; then
        $ZENITY --error --text="No format selected"
        exit 0;

# Select the compressing utility to use
case $selected_format in
                COMP_COMMAND="tar cvf"
                COMP_COMMAND="tar czvf"
                COMP_COMMAND="tar cjvf"
                COMP_COMMAND="zip -9"
                COMP_COMMAND="mkisofs -R -o"

# Execute the command while showing a progress bar.
(echo "0" ;
 echo "100") | $ZENITY --progress --pulsate --title=Working...

The script above works for me, but it assumes the existence of several commands. If you want to use it you might need modify some lines.


speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields