Linux:如何同时使用文件作为输入和输出?

我刚刚在bash中运行以下代码:
uniq .bash_history > .bash_history

我的历史档案最终完全空了.

我想在写入之前我需要一种方法来读取整个文件.
怎么做的?

PS:我显然想过使用临时文件,但我正在寻找更优雅的解决方案.

解决方法

我建议使用 moreutils的海绵.从联机帮助页:
DESCRIPTION
  sponge  reads  standard  input  and writes it out to the specified file. Unlike
  a shell redirect,sponge soaks up all its input before opening the output file.
  This allows for constructing pipelines that read from and write to the same 
  file.

要将此问题应用于您的问题,请尝试:

uniq .bash_history | sponge .bash_history

相关文章

/etc/sysctl.conf这个目录主要是配置一些系统信息,/etc/sys...
1.作用 useradd或adduser命令用来建立用户帐号和创建用户的起...
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅...
不管是我们在安装软件还是监测软件的使用性能,我们都要随时...
装好Tomcat7后,发现除了本机能访问外界访问不了,岂有此理。...
修改防火墙配置需要修改 /etc/sysconfig/iptables 这个文件,...