1

I am using:

command > ~/Desktop/file.txt

My problem is that I need to execute this command multiple times, and each time It will give me different outputs, in this moment each time I run it It erases everything and saves the new output in the file, obviously this doesn't work for me since I need to add the new output along the current output.

I will really appreciate any help.

muru
  • 197,895
  • 55
  • 485
  • 740
Castiblanco
  • 141
  • 15

4 Answers4

3

Use >> instead of >:

command >> ~/Desktop/file.txt

While you were using command > ~/Desktop/file.txt, the file ~/Desktop/file.txt was opened by shell for writing the content of STDOUT (file descriptor 1). So the file's content will get overwritten every time you run the command.

On the other hand, shell will open the file for appending if you use >> operator. As a result the outputs of the command will get appended every time you run it.

I would suggest you to go through this manual on bash redirections.

heemayl
  • 91,753
3

Use '>>'. command >> ~/Desktop/file.txt

kanatti
  • 353
2

If you're running it in a loop, you can redirect the output of the entire loop instead.

for (( i = 0; i < n; i++ )); do
    somecommand
done > ~/Desktop/file.txt

If it's a bit more complex (e.g. other things outputting as well), you can open it on a separate file descriptor.

exec 3> ~/Desktop/file.txt
...
somecommand >&3
...
somecommand >&3
...
exec 3>&- # closes it
geirha
  • 46,101
1

You want >>, not >, like this:

command >> /path/to/file.txt
Tim
  • 32,861
  • 27
  • 118
  • 178