Pass filename from Nautilus to shell script

1.6k views Asked by At

I am trying to execute a shell script that gets for an argument a file name.

I can do it if I go to the terminal and type the full PATH.

But I am trying to make this more user friendly so I what I want to do is to send the argument (complete path) from the file manager or nautilus.

Is it possible?

2

There are 2 answers

0
Agustin On BEST ANSWER

Ok I am going to share a way I found that works.

All I was trying to do was to pass a path as an argument, without typing it on the command line. to make it user friendly.

So I use a zenity tool, file selection, to browse through the file manager and select the file.

./script.sh $(zenity --file-selection)

See, now I am passing the path of the file I selected.

If you want to try this out, just type in the command line.

zenity --file-selection
2
Jamil Said On

It is possible. Here's a way to do it:

Copy your script to the folder ~/.local/share/nautilus/scripts/ -- please note that this path will expand to include the current user information, such as: /home/john/.local/share/nautilus/scripts/.

Now, on your script, capture the argument using $1. For example, let's say that your script is intended to delete the file, then your script could be like this:

#!/bin/bash
rm -f "$1"
exit

Now, restart Nautilus and navigate to the file you want to execute the action on. Right-click the file, and on the menu that appears, choose "scripts", and on the sumenu that appears, choose your script name -- that will execute your script with the file name as argument.

Note: don't forget to make your script executable first, for example: chmod +x /home/john/.local/share/nautilus/scripts/myScript