Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to copy retaining relative path

📅 2019-Jan-21 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ copy, rsync ⬩ 📚 Archive

Problem

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.

Solution

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
/home/joe/workspace/foobar
$ 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.


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧