Copy-pasting files in the Linux terminal

The clipboard cut, copy and paste functions can be a great timesaver when organizing files.

But there didn't seem to be an obvious way to use those functions with the command line in Linux while targeting the main clipboard of the desktop environment such that files can be transferred between the terminal and file manager applications.

After a lot of searching, the closest I could find was a couple of scripts written by the user woodenshoe-wi in this thread named g-cut, g-copy and g-paste. I took the clipboard interfacing code from those scripts as the starting point for the clipboard-files script described here.

Get it on Github:

Download and install clipboard-files using the guide on Github.

If you find issues or ways to extend the script to unsupported desktop environments, please share the improvements on Github!

More about clipboard-files

Installing the script provides the following commands:

ccut FILE [FILE]...   Cut files or folders to clipboard
ccopy FILE [FILE]...  Copy files or folders to clipboard
cpaste                Paste from clipboard to current dir
cshow                 Show files on clipboard
cclear                Clear clipboard without any file operations
. ccd                 cd to the dir of the 1st file on clipboard

I think it's useful to be able to check the state of the clipboard with cshow and to be able to clear it with cclear. The ccd command will simply change directory to that of the first file on clipboard in a way suitable to be sourced. If ccd was not sourced with . the change of directory would have no effect.

cpaste will of course move the files if it's a cut operation and in that case it will also clear the clipboard. This mimics the typical behaviour of file managers.

The individual commands are actually symlinks to the main clipboard-files script which will check it's zero argument $0 and determine if it was invoked as ccut, ccopy and so on. With this technique all the coding can be contained in one file which reduces code repetition. The single binary command shell: busybox uses a similar technique.

Files on the clipboard in Gnome-like desktops

When files are put on the clipboard on Gnome-like desktops it's done with a string that starts with either cut or copy depending on the operation. After that follows the filenames separated by line breaks in a format like: file:///<full-file-path>. On some platforms URL encoding is done on the filenames which will turn for example spaces into %20.

The string has to be on the correct clipboard target in order for the file manager to see it as copy-paste-able files. It turns out that Gnome, Unity and XFCE all use the target: x-special/gnome-copied-files, but on Mate the target has to be x-special/mate-copied-files. The script uses the environment variable: $XDG_CURRENT_DESKTOP to determine what desktop environment is running and attempts to do the right thing based on that.

Note that IDE's like Eclipse will work with clipboard-files as well.

The script uses xclip to interface the clipboard. It should be available in the package management systems of all common Linux distributions.

But xclip already has functions for this, no?

There are commands in the xclip package named: xclip-copyfile, xclip-pastefile and xclip-cutfile. On the surface they seem to solve the same issue that clipboard-files is solving, but no. The commands actually do something quite different and interesting, when files are put on the "clipboard" they are tar-gz'ed and put on an xclip buffer as a binary stream. Additionally, when cutting files, they are deleted after being put on the buffer! So, if the buffer is lost or reused for something else, the cut files will be lost forever. Use xclip-cutfile at your own peril!

The obvious downside of the tar-gz approach is that the operations will be a lot slower and more memory demanding than simple file system copying and moving. On the other hand, the pasting will work regardless of what happens to the copied file sources.


Comments powered by Talkyard