Copy file to backup directory preserving folder structure

耗尽温柔 提交于 2019-12-05 18:24:42

You need to cd to the directory and then copy the file:

(cd MyFolder && cp --parents Test/aaa ../.MyFolder.bck)

The brackets around the command make it run in a subshell, rather than in your current shell. The advantage of this is that it saves you from having to cd back to the original directory afterwards.

Why don't you use two commands?

cd MyFolder && cp --parents /Test/aaa .MyFolder.bck;

And there are several synchronization utilities for Linux too, even for backup purposes, e.g. rsync, rdiff-backup, synkron... Not to mention the advantages of a local VCS... You should not reinvent the wheel.

I like to use "rsync" for this.

rsync -av MyFolder/ .MyFolder.bck/

Make sure to use the trailing slashes, since they tell rsync that both of these arguments are the "top level" directories (that is, don't create another MyFolder inside of .MyFolder.bck).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!