Working With Nautilus Scripts

Jarret B

Well-Known Member
Staff member
May 22, 2017
Reaction score
Sometimes, you may use Nautilus and wish you could add your own script to the right-click ability on a file or folder.

In this article, we'll look at that.

Scripting Languages

To use scripts in Nautilus, the language can be BASH, Python, Perl or Ruby.

You can use any of these to write a script, but I will concentrate on BASH for some examples.

Whichever language you are most comfortable with is the one you will prefer and most likely use. No matter the language, just be aware of any steps to make things work that you need to do in Linux.

Preparing Nautilus

The first thing is to create the scripts folder with the command:

mkdir -p ~/.local/share/nautilus/scripts

All scripts will go into this folder. Once a script is in the folder, it will not show up in the right-click context menu until you make at least one script executable with the command:

chmod +x <script_name>

Once you have at least one executable script, then it will appear under 'scripts' in the context menu, as shown in Figure 1.

Figure 1.JPG


You can see in Figure 1 that the name of the script is ''. This is an example, but since the file is a BASH Script and starts with the shebang (#! /bin/bash), we do not need the '.sh' file extension. Just like with any file in Linux, the extension is usually optional. For example, if you place a JPG file in a folder, you should see the thumbnail of the image. If you rename the file and remove the extension, it still shows the thumbnail. If you double-click on the file, it will open the default program associated with a JPG file.


Another program I'll use is Zenity. We use Zenity for scripting to allow you to have a Graphical User Interface (GUI) for interacting with the user. For some Linux distros, Zenity is automatically installed. To check, just enter the command 'zenity --version' in a terminal. If the system does not find the command, then install 'zenity' using your command specific to your distro to install 'zenity'.

Zenity is a command-line program that will generate a GUI. The command has a few parameters to be aware of, but we are only covering a few.

--warning – displays a warning symbol in a box with caption and text
--error – displays an error symbol in a box along with text and caption
--info – displays an information symbol in a box with text and caption
--question – displays two boxes labeled 'yes' and 'no'
--text – text to display in the box
--title – text to display as the title for the pop-up box

There are more parameters and abilities for Zenity, but in the examples, these are the ones we will use.

The syntax for Zenity is:

zenity <box_type> --title=<window_title> --text=<window_text>

The '<box_type>' is one of 'warning', 'error', 'info' or 'question'.


There are two variables that you need to be aware for your scripts. They are:


The variables will contain the selected folder or file names. If there are multiple items selected, they are separated by a newline.

The 'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS' is the full path to a file. For example, if you select the 'Downloads' folder in your HOME folder, the path would be '/home/<username>/Downloads'.

If you use the variable name 'NAUTILUS_SCRIPT_SELECTED_URIS', the path is for a remote system, such as 'smb://dlna-server.local/dlna/File.ext'.

If you use the variable 'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS' and the user selects a file on a remote system, the file list will be empty. If you use 'NAUTILUS_SCRIPT_SELECTED_URIS', then you get a remote file name.


Let's look at an example of the variable contents and start with the 'file_paths' variable.

#! /bin/bash
set -e
set -u
set -o pipefail
zenity --info --title="Selected Files" –text="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"

I removed all comments to get the script to its basics so its easier to see what is going on in it.

Let's look at this line by line.

The first line is, of course, the shebang. This lets the system know this is a bash script.

The next three lines all configure different scenarios in the script to handle errors. The second line, 'set -e', will cause the script to exit immediately if there are any errors. Since the script is running in the background from Nautilus, we don't need it to be stuck and taking up memory. The next line, 'set -u', will cause the script to exit if we do not define a variable when it is expanded. The fourth line, '-o pipefail', prevents an error in a pipeline to cause errors to be raised. All we really want to do is to exit the script on any errors that may occur.

The next line is a zenity command in a statement, 'zenity --info --title="Selected Files" --text="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"'. We are creating an information box, by the '--info', that has a title of 'Selected Files'. The text in the window is a list of all the selected files by 'FILE_PATHS'. So, if the files are local, we get a list. I will select three folders in my HOME folder and run the script, as shown in Figure 2.

Figure 2.JPG


I selected the folders 'Documents', 'Downloads' and 'Music'. Since newlines delimit the file list, each name is on a separate line. If I select remote files on my network, the list is empty.

Now, let's try the 'URIS' variable with the following example:

#! /bin/bash
set -e
set -u
set -o pipefail
zenity --info --title="Selected Files" –text="$NAUTILUS_SCRIPT_SELECTED_URIS"

Here, the script is identical except for finding the 'URIS' variable. By selecting a remote file on the network, I can get the results, as shown in Figure 3.

Figure 3.JPG


Figure 4 shows the output of the using the 'URIS' script on local files. Here, the file paths start with 'file:///' followed by the path and file/folder name.

Figure 4.JPG


But what if we want to do something more creative and useful? Let's create a file in the scripts folder called 'Change Owner'. Make the file executable and place the following code into it:

#! /bin/bash
set -e
set -u
set -o pipefail
if zenity --question --title="Confirm Change" --text="Should the Owner be $USER ?"; then

This all looks familiar except for the three lines:

if zenity --question --title="Confirm Change" --text="Should the Owner be $USER ?"; then

The first line is an if statement. The zenity command is displaying a '--question' box that will give the user the choice of selecting 'Yes' or 'No'. If the user clicks on 'Yes', then it executes the second line. The third line ends the 'if statement'.

Let's look more closely at the first line. Zenity is displaying a box that issues a question 'Should the Owner be $USER ?'. Here, it replaces the variable '$USER' with the current username. The 'title' of the displayed box will be 'Confirm Change'. If the user clicks on 'No', then the script exits.

If the user should click on 'Yes', then we go to the second line. The second line starts with 'pkexec'. The 'pkexec' command will issue the following command as an administrative user. So, it prompts you to enter your password to elevate the privileges for the command following it. The command it is elevating the privileges for is 'sudo chown $USER:$USER $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'. In this case, we are running the 'chown' command to change the ownership of the selected files/folders to match the current user, $USER, and the user’s main group.

To test this, open a terminal and go to your HOME folder. Execute the command 'sudo mkdir T1'. Change the filename if you have one that already exists. Close the terminal. In Nautilus, go to your HOME folder and you should see the new folder that has an icon inside the folder icon. The icon inside the folder should be a little red lock to show that you do not have privileges to the folder. Right-click on the folder, select 'scripts', then select the 'Change Owner' script. It should prompt you to verify that you want to change the ownership to the current user. Select 'Yes' and enter your password when prompted. The lock icon on the folder should disappear. You can check the Properties on the folder and click on the Permissions tab. The system changes the user and group ownership to your user account.

With scripting having the limitations of your imagination, you can create scripts to manage a lot of various items from within Nautilus.


You can write scripts to help manage repetitive tasks from within Nautilus. Looking online, there are a lot of scripts available to download.

Make sure you look over any scripts before running them. Look into writing your own scripts so that you understand the script and can modify it as you need to improve it.

Learning Zenity can also help improve the looks of scripts by adding a GUI element. This can make your script look more professional instead of simply being a command-line interface.
Last edited:


New Member
Aug 17, 2022
Reaction score
Very nice introduction, thank you.

Staff online

Members online