这样做的基本的Unix命令一些研究,并试图获得一个例子,当我想用下面的命令行选项--attributes-only的命令cp
这是来自cp手册页
--attributes-only
              don't copy the file data, just the attributes
Run Code Online (Sandbox Code Playgroud)
    Chr*_*own 15
假设您有一个文件 ,file1您知道它应该具有相同的属性file2(您知道它file2具有正确的属性)。
$ stat file{1,2}
  File: 'file1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 1fh/31d Inode: 2326956     Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/   chris)   Gid: ( 1000/   chris)
Access: 2013-12-24 09:53:20.248720441 +0800
Modify: 2013-12-24 09:53:20.248720441 +0800
Change: 2013-12-24 09:53:31.011984772 +0800
 Birth: -
  File: 'file2'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 1fh/31d Inode: 2326957     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   chris)   Gid: ( 1000/   chris)
Access: 2013-12-24 09:53:21.045382001 +0800
Modify: 2013-12-24 09:53:21.045382001 +0800
Change: 2013-12-24 09:53:21.045382001 +0800
 Birth: -
Run Code Online (Sandbox Code Playgroud)
确保它们匹配的一种方法是检查file2并手动应用属性:
$ chmod 644 file1
Run Code Online (Sandbox Code Playgroud)
然而,这对于自动化和编写脚本来说很麻烦。file2以file1编程方式从中获取属性并应用它们会更容易。
$ cp --attributes-only --preserve file2 file1
$ stat file1
  File: 'file1'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 1fh/31d Inode: 2326956     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   chris)   Gid: ( 1000/   chris)
Access: 2013-12-24 09:53:21.045382001 +0800
Modify: 2013-12-24 09:53:21.045382001 +0800
Change: 2013-12-24 09:57:06.320604649 +0800
 Birth: -
Run Code Online (Sandbox Code Playgroud)
--attributes-only自己什么都不做;它需要与其他属性保留标志结合使用。来自info cp:
--attributes-only
     Copy only the specified attributes of the source file to the
     destination.  If the destination already exists, do not alter its
     contents.  See the `--preserve' option for controlling which
     attributes to copy.
Run Code Online (Sandbox Code Playgroud)
--preserve上面使用,记录为等效于--preserve=mode,ownership,timestamps. 在内部,您可以将其视为“不复制数据”而不是“仅复制属性”,这就是您--preserve无论如何都必须通过的原因。