从具有相同权限和所有权的 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
Create a new empty file from linux command line with same permissions and ownership?
提问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