0

I've created a script to empty my trash. I want to be able to execute it without having to use the terminal. Is there anyway I could do this?

draoi
  • 33

1 Answers1

2

A nice way to execute scripts by double clicking them is to make a .desktop file, which is essentially a shortcut. Aside from scripts, it can execute pretty much any executable, including .jar. Here's how to make one:

  1. Create a file and name it <program name>.desktop
  2. Open it with GEdit and paste this text in:

    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=<whatever you want the file name to be>
    Comment=<description of program>
    Exec=</path/to/script>
    Icon=</path/to/optional/icon.png>
    
  3. Edit the Name, Comment, Exec, and Icon fields to what you want. Having an Icon is optional and you can simply remove that line if you don't want to have one. Make sure to delete the surrounding <>.

  4. Save the file and then go into its properties.

  5. Go to Permissions and mark it as executable.

You should now be able to double click this file and it will execute your script. If you don't want a terminal window to be shown, add Terminal=false to the file. You can now add this file to the launcher for easy access, if you like. If you want to move your script, you'll have to update the Exec path in the .desktop file.

Any problems, just comment.

---Update---
Thanks to @kos, I now know the solution to your problem. Instead of having sudo rm -rf ~/.local/share/Trash/*, you want just rm -rf ~/.local/share/Trash/*. Since ~/.local/share/Trash is owned by you, you can do anything from your user account to edit it without sudo. After testing the non-sudo command out, I can confirm that it works.

TheWanderer
  • 19,395
  • 12
  • 50
  • 65
  • It says there was an error launching the application.[Desktop Entry] Version=1.0 Type=Application Name= Comment= Exec=<./empty_trash.sh> Icon=</path/to/optional/icon.png> – draoi Nov 17 '15 at 00:02
  • What was the error? – TheWanderer Nov 17 '15 at 00:03
  • A window comes up and says "There was an error launching the application." – draoi Nov 17 '15 at 00:04
  • The script works on its own, when run from the terminal. – draoi Nov 17 '15 at 00:04
  • Did you set it as executable in the properties? – TheWanderer Nov 17 '15 at 00:04
  • Yes, under permissions, execution is allowed. – draoi Nov 17 '15 at 00:06
  • You took away the <> from the paths and such right? – TheWanderer Nov 17 '15 at 00:06
  • Here is the script for the .desktop file: – draoi Nov 17 '15 at 00:07
  • [Desktop Entry] Version=1.0 Type=Application Name= Comment= Exec=<./empty_trash.sh> Icon=</path/to/optional/icon.png> – draoi Nov 17 '15 at 00:07
  • Yeah, take away the <> from around the paths, name and comment. Those were to denote that what was inside should be changed. – TheWanderer Nov 17 '15 at 00:08
  • Added that to my answer for clarity. – TheWanderer Nov 17 '15 at 00:08
  • lol, whoops. Still getting the error though... – draoi Nov 17 '15 at 00:10
  • Under Exec you can't have ./ since that's the working directory. If the script is in your home directory, change that to ~/, although a definite path might be better. – TheWanderer Nov 17 '15 at 00:11
  • I guess I could in theory just add the command to the .desktop file. – draoi Nov 17 '15 at 00:11
  • If it's just one command, yeah, but try that suggestion first. – TheWanderer Nov 17 '15 at 00:12
  • Well I didn't get an error message this time, but nothing happened. – draoi Nov 17 '15 at 00:17
  • Add Terminal=true to it and see what happens. What's the command that you're running? – TheWanderer Nov 17 '15 at 00:18
  • Still nothing. The terminal doesn't open. the command is: – draoi Nov 17 '15 at 00:19
  • sudo rm -rf ~/.local/share/Trash/* – draoi Nov 17 '15 at 00:20
  • So you double-clicked the desktop file but nothing happened (the trash didn't empty)? I would say just add the command to the Exec field. You'll have to enter your password when you double click it. – TheWanderer Nov 17 '15 at 00:20
  • The trash doesn't empty, and the terminal doesn't open for me to enter my password. Clicking on the script itself works, but naturally it would be nicer to get it working with a .desktop icon. – draoi Nov 17 '15 at 00:22
  • And this is with the command in the Exec field now. – draoi Nov 17 '15 at 00:23
  • [Desktop Entry] Version=1.0 Type=Application Name=trashx Exec=sudo rm -rf ~/.local/share/Trash/* Terminal=True – draoi Nov 17 '15 at 00:23
  • Put the command in single quotes: ' ' – TheWanderer Nov 17 '15 at 00:24
  • Now that causes the error message to pop back up. I've got to go to sleep now, but this has been interesting. I'm going to look into this further when I have more energy. I like the idea I've being able to make a desktop icon for a little script, also so that I could run other programs that I usually have to start with the terminal. – draoi Nov 17 '15 at 00:30
  • It is a great feature. I'll reboot into Ubuntu in a bit and update my answer if I'm able to come up with anything. – TheWanderer Nov 17 '15 at 00:35
  • Thanks to @kos, I now know that sudo is unnecessary for this command. Putting rm -rf ~/.local/share/Trashinstead fixed it. – TheWanderer Nov 17 '15 at 00:49
  • As a .desktop file, it's still not working. As an sh file, it works without the sudo command. However I'm afraid that if I delete a locked file, it will only work with the sudo command. – draoi Nov 18 '15 at 09:34
  • The original problem was that the normal method of emptying the trash did not work after I had deleted a large folder, possibly because it was locked. http://askubuntu.com/questions/698920/ubuntus-terrible-trash/698922?noredirect=1#comment1023841_698922 – draoi Nov 18 '15 at 09:37
  • It's kind of dangerous, but what if you put a chmod 777 on the stuff inside the Trash folder before you run the delete command. – TheWanderer Nov 18 '15 at 11:01