0

I want to double click a file on my desktop, and have two files be opened in gedit as sudo.

Whenever I'm making a new website, I need to open

/etc/hosts /etc/apache2/sites-enabled/000-default.conf

as sudo.

At the moment this means opening the terminal, runing sudo gedit then opening each file manually. I want to streamline this part of my workflow. On windows I had wrote a little bash script which worked nicely. How can I do the same in ubuntu?

So far in my searches I've come across ways of adding a shortcut to the file browser, and similar things, but not exactly what I want.

I have tried creating a desktop launcher, but can't see how to make it run as sudo.

1 Answer 1

1

To enter your password via GUI, you need gksu to be installed:

sudo apt-get install gksu

Then:

  1. Make a new text document in Gedit, then copy paste this into it:

    #!/bin/bash
    
    gedit /etc/hosts
    gedit /etc/apache2/sites-enabled/000-default.conf
    

    Save the file as filename.sh

    This is a shell script to run your commands in a sequence (the second file will open once you closed the first). If you prefer to open both files at once, the command should be as below:

    gedit /etc/hosts&gedit /etc/apache2/sites-enabled/000-default.conf
    
  2. Make the file executable: right-click the file, choose properties > permissions and tick allow executing file as program. Alternatively. you can run:

    chmod +x /path/to/yourscript.sh

  3. Open a new gedit window and paste the following:

    [Desktop Entry]
    Type=Application
    Name=Name you like
    Exec=gksu /path/to/your/script.sh
    Terminal=false
    Icon=/path/to/some/iconfile
    

    Save this as filename.desktop, make it executable as well (if you run it from your desktop). When you double click the desktop file, it will run the .sh script, asking for your password and opening the files for editing.

For some other .desktop features please go here LINK

6
  • Thanks, that's one step closer. But still I need to open the terminal, navigate to the files location, and run the file. How can I just double click it and have it run?
    – S..
    Aug 24, 2014 at 10:14
  • @Sam create filename.sh with code mentioned above and save it on desktop (give it execution permission) now you have to only double-click on filename.sh on Desktop!
    – Pandya
    Aug 24, 2014 at 10:20
  • Thanks @Pandya but when I double click it the file opens in gedit as text. I have given it permission to execute through right clicking then properties. Any ideas?
    – S..
    Aug 24, 2014 at 10:57
  • As Jacob pointed out, file must be executable first. Open file manager, right click the file and edit the preferences. Then go to Edit drop down menu, and click preferences, then go to Behavior, and check " Run executable files when opened " Aug 24, 2014 at 12:04
  • Thanks all. @JacobVlijm your comments edited into the answer answered the question.
    – S..
    Aug 24, 2014 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .