Unix Shell脚本,用于复制文件和创建目录 [英] Unix Shell scripting for copying files and creating directory

查看:269
本文介绍了Unix Shell脚本,用于复制文件和创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个源目录(例如 / my / source / directory / )和一个目标目录(例如 / my / dest / directory / ),我想与之进行一些镜像

I have a source directory eg /my/source/directory/ and a destination directory eg /my/dest/directory/, which I want to mirror with some constraints.


  • 我要复制符合 find 命令某些条件的文件,例如- ctime -2 (不到2天)到dest目录进行镜像

  • 我想包括一些前缀,所以我知道它来自哪里,例如/源/目录

  • 我想使用绝对路径执行所有这些操作,因此它不取决于我从哪个目录运行

  • 猜测没有 cd 命令也是一种好习惯。

  • 如果子目录不存在,我希望创建子目录

  • I want to copy files which meet certain criteria of the find command, eg -ctime -2 (less than 2 days old) to the dest directory to mirror it
  • I want to include some of the prefix so I know where it came from, eg /source/directory
  • I'd like to do all this with absolute paths so it doesn't depend which directory I run from
  • I'd guess not having cd commands is good practice too.
  • I want the subdirectories created if they don't exist

所以

/my/source/directory/1/foo.txt -> /my/dest/directory/source/directory/1/foo.txt
/my/source/directory/2/3/bar.txt -> /my/dest/directory/source/directory/2/3/bar.txt

我已经一起入侵了以下命令行,但看上去有点丑陋,有人能做得更好吗?

I've hacked together the following command line but it seems a bit ugly, can anyone do better?

find /my/source/directory -ctime -2 -type f -printf "%P\n" | xargs -IFILE rsync -avR /my/./source/directory/FILE /my/dest/directory/

如果您认为我应该自己添加此命令行作为答案,请发表评论,我不想对信誉感到贪婪。

Please comment if you think I should add this command line as an answer myself, I didn't want to be greedy for reputation.

推荐答案

这与(已关闭的)问题非常相似: Bash脚本复制文件而不会覆盖。我给出的答案引用了 查找| cpio 解决方案在其他答案中提到(减去时间标准,但这是相似和相同之间的区别),还概述了使用GNU'tar'的解决方案。

This is remarkably similar to a (closed) question: Bash scripting copying files without overwriting. The answer I gave cites the 'find | cpio' solution mentioned in other answers (minus the time criteria, but that's the difference between 'similar' and 'same'), and also outlines a solution using GNU 'tar'.

当我在Solaris上进行测试时,GNU tar和(Solaris)cpio都无法保留ctime设置。确实,我不确定是否有任何方法可以做到这一点。例如, touch 命令可以设置atime或mtime或同时设置两者-但不能设置ctime。 utime()系统调用也只接受mtime或atime值;它不处理ctime。因此,我相信,如果您找到保留ctime的解决方案,那么该解决方案可能是特定于平台的。 (奇怪的例子:入侵磁盘设备并在inode中编辑数据-不可移植,需要提升的特权。)不过,重读这个问题后,我发现保留ctime不是要求的一部分(phe);

When I tested on Solaris, neither GNU tar nor (Solaris) cpio was able to preserve the ctime setting; indeed, I'm not sure that there is any way to do that. For example, the touch command can set the atime or the mtime or both - but not the ctime. The utime() system call also only takes the mtime or atime values; it does not handle ctime. So, I believe that if you find a solution that preserves ctime, that solution is likely to be platform-specific. (Weird example: hack the disk device and edit the data in the inode - not portable, requires elevated privileges.) Rereading the question, though, I see that 'preserving ctime' is not part of the requirements (phew); it is simply the criterion for whether the file is copied or not.

我认为 cd '操作是必需的-但它们可以完全本地化为脚本或命令行,如所引用的问题和下面的命令行所示,其中第二个

I think that the 'cd' operations are necessary - but they can be wholly localized to the script or command line, though, as illustrated in the question cited and the command lines below, the second of which assumes GNU tar.

(cd /my; find source/directory -ctime -2 | cpio -pvdm /my/dest/directory)

(cd /my; find source/directory -ctime -2 | tar -cf - -F - ) |
    (cd /my/dest/directory; tar -xf -)

不使用 chdir()(又名 cd ),您需要专门的工具或选项来即时处理路径名的操作。

Without using chdir() (aka cd), you need specialized tools or options to handle the manipulation of the pathnames on the fly.

特定于GNU的'查找-正如Adam Hawes指出的,print0 '和' xargs -0 '非常强大和有效。有趣的是,GNU cpio 可以选择处理' find -print0 '的输出,即'-null 或简称 -0 。因此,使用GNU find 和GNU cpio ,安全的命令是:

The GNU-specific 'find -print0' and 'xargs -0' are very powerful and effective, as noted by Adam Hawes. Funnily enough, GNU cpio has an option to handle the output from 'find -print0', and that is '--null' or its short form '-0'. So, using GNU find and GNU cpio, the safe command is:

(cd /my; find source/directory -ctime -2 -print0 |
    cpio -pvdm0 /my/dest/directory)

注意:这不会覆盖备份目录下的现有文件。为此,在 cpio 命令中添加 -u

Note:This does not overwrite pre-existing files under the backup directory. Add -u to the cpio command for that.

类似地,GNU tar 支持-null (显然没有 -0 缩写),也可以使用:

Similarly, GNU tar supports --null (apparently with no -0 short-form), and could also be used:

(cd /my; find source/directory -ctime -2 -print0 | tar -cf - -F - --null ) |
    (cd /my/dest/directory; tar -xf -)

GNU处理具有空终止符的文件名非常聪明并且是一项有价值的创新(尽管我只是在最近才意识到这一点,这要归功于SO;它已经在GNU tar中使用了至少十年了。)

The GNU handling of file names with the null terminator is extremely clever and a valuable innovation (though I only became aware of it fairly recently, courtesy of SO; it has been in GNU tar for at least a decade).

这篇关于Unix Shell脚本,用于复制文件和创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆