Clipboard for copying and pasting files in command line?

Using Bash, I would just visit the directories:

$ cd /path/to/source/directory
$ cd /path/to/destination/directory

Then, I would use the shortcut ~-, which points to the previous directory:

$ cp -v ~-/file1.txt .
$ cp -v ~-/file2.txt .
$ cp -v ~-/file3.txt .

If one wants to visit directories in reverse order, then:

$ cp -v fileA.txt ~-
$ cp -v fileB.txt ~-
$ cp -v fileC.txt ~-

If I saw that situation coming as a one-off, I might:

a=`pwd`
cd /somewhere/else
cp "$a/myfile" .

If there were directories that I found myself copying files out of semi-regularly, I would probably define some mnemonic variables for them in my .profile.

Edited to add:

After sleeping on it, I wondered how closely I could get to other GUI / OS behaviors where you select some number of files, "cut" or "copy" them, then "paste" them somewhere else. The best selection mechanism I could come up with was your brain/preferences plus the shell's globbing feature. I'm not very creative with naming, but this is the basic idea (in Bash syntax):

function copyfiles {
  _copypastefiles=("$@")
  _copypastesrc="$PWD"
  _copypastemode=copy
}

function cutfiles {
  _copypastefiles=("$@")
  _copypastesrc="$PWD"
  _copypastemode=cut
}

function pastefiles {
  for f in "${_copypastefiles[@]}"
  do
    cp "${_copypastesrc}/$f" .
    if [[ ${_copypastemode} = "cut" ]]
    then
      rm "${_copypastesrc}/$f"
    fi
  done
}

To use it, put the code into ~/.bash_profile, then cd to the source directory and run either copyfiles glob*here or cutfiles glob*here. All that happens is that your shell expands the globs and puts those filenames into an array. You then cd to the destination directory and run pastefiles, which executes a cp command for each source file. If you had previously "cut" the files, then pastefiles also removes the source file (or, tries to). This doesn't do any error-checking (of existing files, before potentially clobbering them with the cp; or that you have permissions to remove the files during a "cut", or that you can re-access the source directory after you move out of it).


I think the ~- is the right answer, but note that bash has a built-in line editor that can copy/paste text.

If you are in emacs mode you can recall your cd command from the history, and use Control-u to kill the line into the bash "clipboard" called the kill-ring (there are other ways too). You can then yank this string into a new command at any time with Control-y. Obviously, in your example this depends on you having used an absolute directory name in your cd command.

You can also use the interesting default key-binding of Meta-.. This copies the last word from the previous command into your current line. If repeated, each time it goes back one command in the history. So if you do a cd /x, then cd /y followed by cdMeta-.Meta-. you will have /x in your input.