14

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
pgmank
  • 734

1 Answers1

14

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
Germar
  • 6,377
  • 2
  • 26
  • 39
  • I tried this and when I click on run, I get this message. Also, is there a way to insert the icon property somewhere in the script or I should create an autorun.inf with contents [autorun] icon=icon.ico ? – pgmank Jun 30 '15 at 02:09
  • Your script must start with #!/bin/sh. Sorry if this was irritating. The line above was just to show the content of the file autorun. – Germar Jun 30 '15 at 19:13
  • The error message was displayed while the script contents where only the shell declaration and the command. I did not append the cat command on the top. I also tried to open gedit but the same error message appeared. Also, if autorun.inf is supported on Ubuntu, could it also open a script apart from setting the usb icon ? – pgmank Jun 30 '15 at 20:56
  • Please post your full script (edit your Question). No, autorun.inf can not run a script (open=... doesn't work). – Germar Jun 30 '15 at 21:05
  • Can you please post any other properties of the autorun.inf that are supported by Ubuntu if any ? – pgmank Jun 30 '15 at 21:29
  • Did you make the script executeable with chmod 755 autorun? Everything else looks good and does work in here without problems. – Germar Jun 30 '15 at 21:55
  • I have set the mode bits of autorun and autorun.inf to 755 and still the same error message appears – pgmank Jul 01 '15 at 01:30
  • Assuming that the value of Software is Run Software under System Settings > Details > Removable Media - would it still prompt and ask you whether you would like to run the software? That prompt should appear only when the value of Software is Ask what to do, doesn't it? In other words: I think that having the value of Run Software for Software should run the software without any questions or prompts. – Dor Sep 16 '16 at 11:21
  • 1
    That would be very insecure as it would start programms from every unknown device which could have bin prepared to attack you. I wouldn't recomment doing that. – Germar Sep 16 '16 at 11:28
  • @Germar Indeed, but in your case it is set to Run Software .. as can be seen in the image. So in your case it should execute the software w/o asking.. – Dor Oct 11 '16 at 08:57
  • More of a note to self: On my Mint Mate 18.1, that settings dialog can be reached under the name "File Management Preferences" – RobertG May 14 '17 at 18:46