How to autorun files and scripts in Ubuntu when inserting a USB stick like autorun.inf in Windows?

Is there a way to execute a specific script or open a file with a particular program when inserting a USB stick on Ubuntu like the autorun.inf file in Windows ? If not, is there another way ?

EDIT: According to the answer I got I have created a file in the USB root folder named autorun with the above contents:

#!/bin/sh
xdg-open myText.txt

I have also created a file named autorun.inf with the above contents:

[autorun]
icon=icon.ico

The autorun.inf file specifies the USB icon. From the above autorun files only the autorun.inf seems to work. The autorun script file, when executed it displays the above message.

autorun error message

Am I doing something wrong or there are some more parameters to be configured in order to run the script ?

P.S: The files myText.txt and icon.ico are placed in the USB root folder.

Moreover, the autorun and autorun.inf file mode bits have been set to 755, using :

cd /path/to/usbFolder
chmod 755 autorun
chmod 755 autorun.inf

Solution 1:

Open System Settings > Details > Removable Media and set Software to Run Software

Removable Media

Your USB Stick must be formatted with a native Linux filesystem like EXT4.

Create a shell script with the name autorun (or autorun.sh, doesn't matter) on your USB Stick and make it executable with chmod 755 autorun.

Next time you insert your USB Stick, Ubuntu will notify you about the autorun and ask if you would like to execute this.

Ask before autorun

To automatically open a document you can use xdg-open

#!/bin/sh
xdg-open myDocument.odt

This will open myDocument.odt with the default application for this mime-type. So it will work the same with all other files, too.

To have a custom icon for your USB Stick you can create a autorun.inf file with following content

[autorun]
icon=icon.png

and place your icon as icon.png on the Stick.


autorun.inf on Ubuntu support this commands (take a look at autorun.inf on Wikipedia):

[autorun]
icon=iconfilename[,index]
label=text

[Content]
MusicFiles=yes|no
PictureFiles=yes|no
VideoFiles=yes|no

[ExclusiveContentPaths]
/pictures
/music
more music/special

[IgnoreContentPaths]
/pictures
/music
more music/special