Cómo crear un archivo en Linux desde la ventana de terminal?


¿Cuál es la forma más fácil de crear un archivo en Linux?

Author: Tshepang, 2012-02-21

15 answers

Dependiendo de lo que desee que contenga el archivo:

  • touch /path/to/file para un archivo vacío
  • somecommand > /path/to/file para un archivo que contiene la salida de algún comando.

      eg: grep --help > randomtext.txt
          echo "This is some text" > randomtext.txt
    
  • nano /path/to/file o vi /path/to/file (o any other editor emacs,gedit etc)
    Abre el archivo existente para editarlo o crea y abre el archivo vacío para ingresar, si no existe

 443
Author: Eugen Rieck,
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
2013-07-30 11:49:39

Use toque

touch filename
 96
Author: Mariusz Jamro,
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
2012-02-21 16:45:50

Crear el archivo usando cat

$ cat > myfile.txt

Ahora, simplemente escribe lo que quieras en el archivo:

Hello World!

CTRL-D para guardar y salir

 72
Author: raffian,
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-01-27 14:32:57

Hay varias soluciones posibles:

Crear un archivo vacío

touch file

>file

echo -n > file

printf '' > file

La versión echo solo funcionará si su versión de echo admite el interruptor -n para suprimir nuevas líneas. Esta es una adición no estándar. Los otros ejemplos funcionarán en un shell POSIX.

Crear un archivo que contenga una nueva línea y nada más

echo '' > file

printf '\n' > file

Este es un "archivo de texto" válido porque termina en una nueva línea.

Escribir texto en un archivo

"$EDITOR" file

echo 'text' > file

cat > file <<END \
text
END

printf 'text\n' > file

Estos son equivalente. El comando $EDITOR asume que tiene un editor de texto interactivo definido en la variable de entorno EDITOR y que introduce texto equivalente de forma interactiva. La versión cat supone una nueva línea literal después de la \ y después de cada otra línea. Aparte de eso, todos estos funcionarán en un shell POSIX.

Por supuesto, también hay muchos otros métodos de escritura y creación de archivos.

 34
Author: Sorpigal,
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
2012-02-21 17:35:57

También, cree un archivo vacío:

touch myfile.txt
 22
Author: andreapier,
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
2012-02-21 16:44:44

Jaja! es fácil! prueba esto:

$ touch filename
 12
Author: Roman Newaza,
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
2012-02-21 16:44:48

Cómo crear un archivo de texto en Linux:

  • Usando touch para crear un archivo de texto: $ touch NewFile.txt
  • Usando cat para crear un nuevo archivo: $ cat NewFile.txt
    El archivo se crea, pero está vacío y todavía esperando la entrada del usuario. Puede escribir cualquier texto en el terminal, y una vez hecho CTRL-D lo cerrará, o CTRL-C se le escapará.
  • Simplemente usando > para crear un archivo de texto: $ > NewFile.txt
  • Por último, podemos usar cualquier nombre de editor de texto y luego crear el archivo, como as:
    nano MyNewFile vi MyNewFile NameOfTheEditor NewFileName
 11
Author: DATA SCIENCE,
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-31 21:10:00

1er método

echo -n > filename.txt

2º método

> filename.txt

3er método

touch filename.txt

Para ver el contenido del archivo

vi filename.txt
 11
Author: shashwat gupta,
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
2016-08-29 08:58:52

Puedes usar el comando touch, como los otros dijeron:

touch filename

Para escribir en el archivo en la línea de comandos, puede usar echo o printf:

echo "Foo" > filename
printf "Foo" > filename

Tal vez pueda tener problemas con los permisos. Si recibe el siguiente error: bash: filename: Permission denied, debe usar sudo bash -c 'echo "Foo" > filename', como se describe aquí: https://askubuntu.com/questions/103643/cannot-echo-hello-x-txt-even-with-sudo

 10
Author: user3753202,
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-04-13 12:22:45

Tan simple como eso:

> filename

 7
Author: IvanM,
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-03-28 10:35:51

Puede usar el comando touch para crear un nuevo archivo vacío.

Http://linux.about.com/library/cmd/blcmdl_touch.htm

 6
Author: Andy Arismendi,
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
2012-02-21 16:46:27

Esto creará un archivo vacío con la marca de tiempo actual

touch filename
 6
Author: Christy George,
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-11 06:28:17

Me gusta el editor de línea de comandos nano:

nano filename
 5
Author: Sam Perry,
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-11-03 09:58:59

En caso de que ustedes estén tratando de crear un nuevo archivo, pero dice: 'File does not exist', es simplemente porque también están accediendo a un directorio, que aún no existe. Primero tiene que crear todos los directorios inexistentes, usando el comando mkdir /path/to/dir.

 3
Author: JustBasti,
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-02-19 09:55:04
touch filename

Para permission denied error use sudo comando como:

sudo touch filename
 1
Author: Pradeep Kumar Prabaharan,
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-03 10:50:20