Bash Script to Drag and Drop a File to New Location

  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!.


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


What @Slug45 said is completely correct. However, it lacks an explanation.

When you drag and drop a file onto an executable file (or a link to one), that executable is run with the path to the dragged file as an argument. This is exactly the same on Windows (not that it really matters here).

Bash has a simple way of dealing with arguments. Use "$@" (with the quotes) to get an array of all arguments (useful for example in a for..in loop). $@,"$*", and $* do similar things, but you almost always want "$@". See here for more information about the specifics.

In addition, you can directly access specific args as $X where X is the arg number. For example:

$ cat ./args.sh
echo $1
echo $2
echo $3
$ ./args.sh foo bar baz
foo
bar
baz
$

In large scripts, it is better to assign those to named variables:

$ cat music.sh
#!/bin/bash
# Usage: ./music.sh Artist Album Song
ARTIST=$1
ALBUM=$2
SONG=$3

if [ $ARTIST -eq "Nickelback" ]; then exit; fi

mplayer ~/Music/$ARTIST/$ALBUM/$SONG.mp3