1

This question already has an answer here:

For example whoami and date. I can do this that way

whoami>/home/user/folder/file
date>>/home/user/folder/file

But i'm sure it can be done in one line without typing path two times. I have tried using

|

but always first command is ignored.

marked as duplicate by steeldriver, muru, Christopher, mosvy, Jeff Schaller May 24 at 1:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

Use a subshell

(whoami; date) > ~user/directory/file
3

You could accomplish this with a single redirection using echo/printf along with command substitution:

printf '%s\n' "$(whoami)" "$(date)" > /home/user/folder/file

IMO this is not a good solution, it is not very readable/manageable...especially if more commands are added.

In order to prevent having to type the path multiple times you could just save it in a variable:

file=/home/user/folder/file
whoami>"$file"
date>>"$file"

But this requires multiple redirections as well.

If you wanted to write a long script or subset of commands and wanted to perform a single redirection for them you could use command grouping:

{
whoami
date
} > /home/user/folder/file

Or on one line:

{whoami;date;} > /home/user/folder/file

Not the answer you're looking for? Browse other questions tagged or ask your own question.