Escribir en el archivo, pero sobrescribirlo si existe


echo "text" >> 'Users/Name/Desktop/TheAccount.txt'

Cómo lo hago para que cree el archivo si no existe, pero lo sobrescribe si ya existe. Ahora mismo este script solo se añade.

 160
Author: switz, 2011-01-13

6 answers

Un poco de comprensión de cómo funcionan las tuberías *nix ayudaría.

En resumen, el operador de redirección >> añadirá líneas al final del archivo especificado, donde-como el único mayor que > vaciará y sobrescribirá el archivo.

echo "text" > 'Users/Name/Desktop/TheAccount.txt'
 289
Author: Nylon Smile,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-07 19:23:21

En Bash, si ha establecido noclobber a la set -o noclobber, entonces utiliza la sintaxis >|

Por ejemplo:

echo "some text" >| existing_file

Esto también funciona si el archivo aún no existe


  • Compruebe si noclobber está configurado con: set -o | grep noclobber

  • Para una explicación más detallada sobre este tipo especial de operador, consulte este post

  • Para una lista más exhaustiva de operadores de redirección, consulte este post

 52
Author: BrDaHa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-20 09:10:12

A pesar de NylonSmile's respuesta , que es "más o menos" correcta.. No pudesobrescribir los archivos de esta manera..

echo "i know about Pipes, girlfriend" > thatAnswer

zsh: file exists: thatAnswer

Para resolver mis problemas.. Tuve que usar... >!, á la ..

[[ $FORCE_IT == 'YES' ]] && echo "$@" >! "$X" || echo "$@" > "$X"

Obviamente, ten cuidado con esto...

 33
Author: Alex Gray,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:03:03
#!/bin/bash

cat <<EOF > SampleFile

Put Some text here 
Put some text here
Put some text here

EOF
 2
Author: Balaji Boggaram Ramanarayan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-05 19:55:12

Si su entorno no permite sobrescribir con >, use pipe | y tee en su lugar de la siguiente manera:

echo "text" | tee 'Users/Name/Desktop/TheAccount.txt'

Tenga en cuenta que esto también se imprimirá en la salida estándar. En caso de que esto no sea deseado, puede redirigir la salida a /dev/null de la siguiente manera:

echo "text" | tee 'Users/Name/Desktop/TheAccount.txt' > /dev/null
 2
Author: computerist,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-14 23:34:13

Si tiene una salida que puede tener errores, es posible que desee usar un ampersand y mayor que, de la siguiente manera:

my_task &> 'Users/Name/Desktop/task_output.log' esto redirigirá tanto stderr como stdout al archivo de registro (en lugar de solo stdout).

 -1
Author: aaron-coding,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-21 17:11:34