8

I'm sure I'll get my ass handed to me for this question, but I just can't seem to figure it out. I'm new to bash scripting, so go easy on me...

I want to be able to drag and drop a file onto a desktop application, which then calls a bash script that will move that file to a location designated within the bash script. Here is what a have so far.

Bash script

#! /bin/bash

mv $file /path/to/new/directory

However, I can't figure out how to input the $file as a variable when I drag and drop it onto a desktop application. How can I do this?

Reason: I have papers that I need to read in one folder, and once I've read them I put them into another folder. I basically just want to make it easy to do this.

Thanks, Paul

4
  • 2
    I don't understand, why don't you just drag and drop them if that's what you want? You seem to be asking how to drag into folder1 which will cause the dragged item to be moved into folder 2. Why don't you just drag it directly into folder2 in the first place?
    – terdon
    Apr 4, 2016 at 10:57
  • 2
    I don't know of any file manager which allows you to drag and drop a file onto an application, and then pass that file to the application to do something with. Every file-manager I've used only supports dragging files into folders. Do you have any reason to think this might be possible? Have you seen an application that works this way? You could of course do this with a web application (which then provides the browser's drag & drop API), or another more complex type of application, but that may be too complex to explain in an AskUbuntu answer. Apr 4, 2016 at 10:59
  • 1
    Don't most terminals and file managers do this? In XFCE, dragging a file from Thunar onto a terminal pastes the full filename. And dragging a file to something like LibreOffice or Gedit will open it (at least try to). This sounds like it's completely up to the application to do whatever it wants with the filename.
    – Xen2050
    Apr 4, 2016 at 13:26
  • @terdon. Yes, I do just want to drag and drop into another folder, but I wanted a quick way to do this without having to back out of the directory I was in and move all the way into a new directory and then dump the file there. That just seems to inefficient for me. To all the others, see the answer provided by Slug45 below.
    – PaulFrater
    Apr 5, 2016 at 14:37

2 Answers 2

24
  1. Create a bash scriptfile with the following contents:

    #!/bin/bash
    mv "$1" "PATH_TO_NEW_DIRECTORY"
    
  2. Create a .desktop file with the following contents:

    [Desktop Entry]
    Name=Document Mover
    Exec=PATH_TO_SCRIPT_FILE %U
    Type=Application
    
  3. Change PATH_TO_NEW_DIRECTORY and PATH_TO_SCRIPT_FILE to your liking.

  4. Do chmod +x script_name

  5. Drag files to the .desktop file.

  6. Done!.

2
  • 1
    It should be emphasised that you always need to quote the variables, as shown. Using $file instead of "$file" (or "${file}") can get you in a heap of trouble. Apr 5, 2016 at 14:34
  • Also, no need to use bash here; #!/bin/sh is sufficient of doing the same (although it may be linked to bash anyway on many systems).
    – ckujau
    Apr 5, 2016 at 14:42
14

No need for a script. Create a link to PATH_TO_NEW_DIRECTORY where you need it. Then drag the file to the link

2
  • 2
    This would have been an easier solution (but then I wouldn't have learned something about bash scripting :)
    – PaulFrater
    Apr 5, 2016 at 14:34
  • This is the "obvious" way to do it and answers the question. But, if you use a script, that script can be arbitrarily complex and do different things depending on what file it is passed - like putting music files one place and documents in another place - or just changing the destination without having to replace the symlink.
    – Joe
    Apr 7, 2016 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.