简体   繁体   English

将文件复制到备份目录,保留文件夹结构

[英]Copy file to backup directory preserving folder structure

I have to copy a file in a directory, to its backup directory, preserving the folder structure. 我必须将目录中的文件复制到其备份目录,保留文件夹结构。 Eg the file aaa in MyFolder/Test/aaa to .MyFolder.bck/Test/aaa 例如, MyFolder/Test/aaa的文件aaa.MyFolder.bck/Test/aaa

I tried to use 我试着用

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

But the result is .MyFolder.bck/MyFolder/Test/aaa and not .MyFolder.bck/Test/aaa (which is the one I want). 但结果是.MyFolder.bck/MyFolder/Test/aaa而不是.MyFolder.bck/Test/aaa (这是我想要的那个)。

You need to cd to the directory and then copy the file: 您需要cd到目录然后复制文件:

(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. 命令周围的括号使其在子shell中运行,而不是在当前shell中运行。 The advantage of this is that it saves you from having to cd back to the original directory afterwards. 这样做的好处是可以节省您不必cd回到原来的目录之后。

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, eg rsync, rdiff-backup, synkron... Not to mention the advantages of a local VCS... You should not reinvent the wheel. Linux也有几个同步实用程序,甚至用于备份目的,例如rsync,rdiff-backup,synkron ......更不用说本地VCS的优点......你不应该重新发明轮子。

I like to use "rsync" for this. 我喜欢用“rsync”来做这件事。

rsync -av MyFolder/ .MyFolder.bck/ 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). 确保使用尾部斜杠,因为它们告诉rsync这两个参数都是“顶级”目录(也就是说,不要在.MyFolder.bck中创建另一个MyFolder)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM