从具有相同权限和所有权的 linux 命令行创建一个新的空文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8135598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 21:41:23  来源:igfitidea点击:

Create a new empty file from linux command line with same permissions and ownership?

linuxfilecommand-line

提问by siliconpi

I need to create a new empty file with the same permissions and ownership (group and user) as the source, kind of like how cp -p FILE1 FILE1.bak works but without actually copying the contents.

我需要创建一个与源具有相同权限和所有权(组和用户)的新空文件,有点像 cp -p FILE1 FILE1.bak 的工作原理,但实际上并不复制内容。

I know I can empty out the contents later on, but that seems wasteful.

我知道我可以稍后清空内容,但这似乎很浪费。

I cant use a script - the solution must run from the command line directly.

我不能使用脚本 - 解决方案必须直接从命令行运行。

采纳答案by Māris Kise?ovs

touch newfile
chmod `stat -c %a originalfile` newfile
chown `stat -c %U originalfile`:`stat -c %G originalfile` newfile

回答by DaveRead

回答by Tudor

Use

touch newfile
chmod --reference=oldfile newfile
chown --reference=oldfile newfile

回答by linuts

cp --attributes-only --preserve=ownership