tee
—轻松解决无权保存vim
已修改文件的尴尬!
一、背景
在类Linux
环境下工作的小伙伴们,肯定都有过这种尴尬:就是在修改一个高权限的文件(比如系统配置文件)的时候,最后提交的时候,发现无权修改此文件!然后只能灰溜溜的保存一份,再次用管理员权限打开,再复制一遍!!!是不是感觉非常不爽。。。
但是今天呢,介绍一种优雅搞定此问题的方法!如下:
二、解决方法
那就是 tee
命令。
其实tee
命令是可以用来提高写入文件的权限等级的。
具体的操作就是在文件保存的时候输入:w !sudo tee %
,这时候vim
会提醒你输入sudo
密码,输入之后,文件就保存了。是不是很方便呢。
三、命令详解
下面是 Mac
环境下的 man
手册信息!
# man tee
TEE(1) BSD General Commands Manual
NAME
tee -- pipe fitting
SYNOPSIS
tee [-ai] [file ...]
DESCRIPTION
The tee utility copies standard input to standard output, making a copy in zero or more files. The output is unbuffered.
The following options are available:
-a Append the output to the files rather than overwriting them.
-i Ignore the SIGINT signal.
The following operands are available:
file A pathname of an output file.
The tee utility takes the default action for all signals, except in the event of the -i option.
The tee utility exits 0 on success, and >0 if an error occurs.
STANDARDS
The tee function is expected to be POSIX IEEE Std 1003.2 (``POSIX.2'') compatible.
BSD June 6, 1993
参数:
-a 附加到既有文件的后面,而非覆盖它。
-i 忽略中断信号。
示例:
// 将用户输入的数据同时保存到文件"file1"和"file2"中
# tee file1 file2
// 追加写入"file"中
# tee -a file
——2020-04-16——