夏期劇場
夏期劇場

Reputation: 18327

Linux: How to write the outputs of TWO terminal commands altogether into a file?

Let's say i want to save (append) the outputs of commands:

.. into a file. With an expectation, like:

Thu Jan 14 04:38:08 GMT 2016
                 total       used       free     shared    buffers     cached
Mem:              3655       1087       2567          0         50        117
-/+ buffers/cache:            919        735
Swap:                0          0          0

I know the way to save with individual commands, like:

# date >> memlogs.txt
# free -m >> memlogs.txt

But how do i properly make them into one clean command please?

Upvotes: 1

Views: 92

Answers (1)

John Kugelman
John Kugelman

Reputation: 361645

{ date; free -m; } > memlogs.txt

(Note the trailing semi-colon.)

or

{
    date
    free -m
} > memlogs.txt

or

exec > memlogs.txt
date
free -m

Surround the last one with parentheses to put the commands in a sub-shell if you want the redirection to be temporary.

(
    exec > memlogs.txt
    date
    free -m
)

Upvotes: 4

Related Questions