Copy-Paste – How to Copy Files from Command Line to Gnome Window

copy/paste

There are plenty of examples how to copy the "contents" of a file from command line with the help of xclip or xsel. What I am looking for is to copy a file from the command line and paste into the file explorer. So I will use a command to copy a file, not only the contents, and use Ctrlv to paste it in UI.

updated
xclip-copyfile and xclip-pastefile work close to what I expect, but when I run xclip-copyfile, I can only paste with xclip-pastefile command, not GUI paste.

Best Answer

I use Nautilus, in Ubuntu, as my File Browser, so I can't speak for any other browser.
The way in which Nautilus handles pasting a file via Ctrl-v is quite local to Nautilus, ie. Nautilus only recognizes Ctrl-v as paste a file in response to a copy file command which was issued while in nautilus itself. This means that you can't use Ctrl-v to paste a file whose path you copied in another application.

However, if it suits you, Ctrl+Shift+v can be bound to a script which runs under nautilus-scripts-manager. With this script, you can do pretty much whatever you like.

nautilus-scripts-manager gives you access to some fundamental information about the current directory and which files/directories are selected. One thing I particularly like about "nautilus-scripts" is that it adds its scripts to the File menu item (as well as to the context-menu). This allows you to set a key-binding which is local to Nautilus 3 or Nautilus 2.

Here is a rough example, using your already mentioned xclip-pastefiles example. Note, that as it stands now, it requires that you set up the xclip-copyfiles before running this Ctrl-Shift-v paste script.
Once you have run the setup xclip-copyfiles, you need only navigate to a Nautilus window, and selcet the target directory (or a file in that directory) and press your script's keybinding: Ctrl-Shift-v

#!/bin/bash   
# Note: `leafpad' is a simple text editor which doubles as a quick-and-easy testing messsage-box

dir=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" |sed -n 1p)
if [[ -n "$dir" ]] ;then
    # target dir from `Nautilus Scripts' (1st priority)    
    if [[ -e "$dir" ]] ;then
       [[ -d "$dir" ]] || dir="${dir%/*}" 
       [[ -d "$dir" ]] || dir=
    fi
else
  # target dir from $1 (2nd priority)
    dir="$1"
    if [[ -e "$dir" ]] ;then
       [[ -d "$dir" ]] || dir="${dir%/*}" 
       [[ -d "$dir" ]] || dir=
    fi
fi

if [[ -z "$dir" ]] ;then
    # target dir from clipboard (if not in $NAUTILUS_SCRIPT.. or  $1)
   dir="$(xclip -o -sel c)"
   if [[ -e "$dir" ]] ;then
      [[ -d "$dir" ]] || dir="${dir%/*}" 
      [[ -d "$dir" ]] || dir= 
   else
      dir=
   fi
fi

[[ -d "$dir" ]] || { echo "$(basename "$0"): target directory not found in clipboard" |leafpad; exit 1; } 

{ cd   "$dir"
  echo "Current  Directory:" 
  echo "  $dir"
  echo "======="
  echo  xclip-pastefile # run your command  (`echo' is for testing)
} 2>&1 |leafpad