how to redirect stdout to a file?

for example, I enter:

touch test.txt // и тут, как я понял, должно быть 0< log.txt 

but does not work.


from comments:

for example, I want this "ls -laRf" command to write to a file. Then I will need another command to write, for example, "grep" include "text.txt"

  • touch only creates a file / changes the modification date, input redirection will not do anything, because This command does not read standard input - Mike
  • @Mike Is the syntax correct? - Volodymyr Samoilenko
  • @Mike I write to the file that should be in the output, but I would like to get a record of the commands - Volodymyr Samoilenko
  • it is not at all clear what it means to redirect stdin to a file. I would understand if "take stdin from file" or "redirect stdout to file" - Mike
  • record which teams? coming in to what? shell are logs of commands, each shell is in its file, for example bash in .bash_history - Mike

1 answer 1

write the standard output (stdout) of the command to the file:

 $ команда > файл 

add standard output (stdout) command to file contents:

 $ команда >> файл 

write both standard output (stdout) and error output (stderr) commands to a file:

 $ команда &> файл 

Add both standard output (stdout) and error output (stderr) commands to the contents of the file:

 $ команда &>> файл 

You can write to the file not only the output of the command, but in general everything that you enter, and see in the terminal. for this you can use the script program.

it launches a new instance of the shell, and everything that you enter, and everything you receive in response, saves it to the file specified by the parameter. if this file is not specified, the default will be the file named typescript in the current directory. the shell can be terminated (and, accordingly, the script program can be script ), as usual, with the exit (often the shells are configured to execute the same command when receiving the ctrl + d keyboard shortcut ).

example:

 $ script logfile Script started, file is logfile $ какие-нибудь команды $ exit 

Now everything that you saw when executing какие-нибудь команды can be viewed in the logfile file. for example, using the less program. in order for the special characters used for coloring to be correctly displayed, the less need to pass the -R option to the less program:

 $ less -R logfile