How to copy retaining relative path


From a certain directory I wanted to copy a file a/b/c.txt to a destination. But I wanted the relative path a/b to be retained at the destination. A normal copy would just copy c.txt to the destination.


There is no way to do this using the cp command. One solution is to use rsync which supports maintaining the relative file path.

For example:

$ pwd
$ rsync -R a/b/c.txt /home/joe/destination

This copies c.txt to /home/joe/destination/a/b automatically creating a/b if they do not exist in the destination.


How to use xclip

xclip is a very useful tool to copy and paste text content between the shell and GUI (X11) programs. You can pipe text to xclip and that text will be copied to the X clipboard. You can invoke xclip to output and it will spit out the content in the X clipboard. Once you learn it, you will wonder how you ever worked without it at the shell, inside Vim and across SSH sessions.

  • Installing it is easy:
$ sudo apt install xclip
  • To copy output from shell to X11 clipboard:
$ pwd | xclip
  • To paste from X11 clipboard to shell:
$ xclip -o | ls
  • Note that xclip works with the X clipboard, not the clipboard maintained by your window manager. Yes, you have two clipboards in Linux! So, if this copy-paste is not working for you, then the selection copied to your X clipboard might not be appearing in your window manager clipboard. Typically, the window manager clipboard is configured by default for all of this to work transparently. If it is not working, then you can configure the window manager clipboard to get all the selections from X clipboard.

  • To copy from Vim to clipboard: Visually highlight the text or lines you want to copy and type :w !xclip. The :w command is used to write the entire contents of the buffer or the visually highlighted text to a specified output file. Instead of a file, we write that text to xclip here, so it will be copied to the clipboard.

  • To copy from clipboard to Vim: Just do the reverse of the above operation. Type this command :r !xclip -o. The :r command in Vim is used to read text into the current buffer. So, this command takes the output of xclip and inserts it into the currently buffer.

  • Copy or paste in SSH session: The above commands works without any change between your local computer and the remote computer you have SSHed into! Yes, works like magic all thanks to the X11 protocol! Note that the remote computer should have xclip installed and you should have created the SSH session with X11 forwarding.

Tried with: XClip 0.12 and Ubuntu 14.04

LibreOffice Calc: Copy and paste results of formulas

Paste Special to paste results of formulas


LibreOffice Calc is the spreadsheet application in the LibreOffice suite. In LibreOffice Calc, if the cells of a spreadsheet hold the results of formulas, then those results cannot be copied and pasted to a different cell. This is because the formula is copied instead of the result.


To copy the results of the formulas instead of the formulas themselves, do this:

  1. Copy the cells as usual.
  2. Right-click at the cell where you want to paste the results and choose Paste Special.
  3. In the Paste Special dialog, under the Selection section, uncheck all the different types of selection. Check only Numbers (assuming that the results are numbers). Choose OK.

The results of formulas should be pasted now instead of the formulas. This trick can also be used to do in-place replacement of cells with formulas with their results.

Tried with: LibreOffice

C++ STL: Copy vector to array

The array behaves like a vector and so can be used almost everywhere a vector is used. So, a vector can be copied into an array using std::copy algorithm. But, make sure that the array is big enough to hold the elements of the vector when you do this:

#include <vector>

std::vector<Foo> fooVec;
Foo fooArr[FOO_MAX];

std::copy( fooVec.begin(), fooVec.end(), fooArr );