Pregunta:
Acabo de ejecutar lo siguiente en bash:
uniq .bash_history > .bash_history
y mi archivo de historial terminó completamente vacío.
Supongo que necesito una forma de leer todo el archivo antes de escribir en él. ¿Cómo se hace eso?
PD: obviamente pensé en usar un archivo temporal, pero estoy buscando una solución más elegante.
Respuesta:
Recomiendo usar sponge
de moreutils . Desde la página de manual:
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.
Para aplicar esto a su problema, intente:
uniq .bash_history | sponge .bash_history