How to save command output to a file in windows and show output on terminal also? - windows

I want to save output of a command in a file as well as display output on terminal.
For saving output to a file I am using following command:
>> ps -ef > D:\temp.txt
But this saves output into files but do not display on terminal. Which command should I use to perform both requirements?

Use MinGW, and than use the following command:
ps -ef 2>&1 | tee D:\temp.txt
This will work.

Related

Cannot log terminal output using complex cat command, error "input file is output file"

I'm trying to write a shell script having the below command and then log the output to a file:
cat "$(ls -rt | tail -n1)" >> Run.log
but getting an error,
Run.log : input file is the output file
This command works fine directly in the terminal and prints the latest file output in the current directory.
What am I doing wrong in the shell script?

Read and write to file before task is complete using cmd

Consider, I am using the command
C:\>ping www.google.com 1>a.txt 2>&1 | type a.txt
It works well as by default windows sends 4 packets, the task ends and then the file content is displayed.
But when I use
C:\>ping www.google.com -t 1>a.txt 2>&1 | type a.txt
Here the task isn't complete as I have used the -t switch. How can I display the file contents as it is being written in the file.
I don't want to use tee from GnuWin32 CoreUtils
You don't want to use tee from the GnuWin32 CoreUtils?
Why don't you try the PowerShell version of the tee command?
Here is a reading
If you are insistent in using only CMD, I think it would be difficult as there is no way (AFAIK) to immediately flush the log buffer to disk.

Can't redirect command output to file

I've been writing a bash script to install PBIS Open when I execute the following command domainjoin-cli join $domain $join_account $password I can see the output on the terminal. However, if I try to capture the terminal output and save the output to a file, the file is empty.
I've tried adding <cmd> > output.txt
I've tried using
script output.txt
<cmd>
exit
I've searched for a day now but I can't seem to find a working solution.
There are two types of output stream stdout and stderr. It is probably coming out on the stderr stream. The > by itself will only capture the stdout.
Try executing with
<cmd> &> filename

Write command output to file plus console from another console

I am developing a script where I have to run it from the command line (say T1). The script has to open another terminal (T2) and the output of this terminal (T2) has to be redirected to a file, so that I can parse the file from the main script (T1). I know how to open a new terminal (T2) from the main terminal (T1).
gnome-terminal -e "ant" 2>&1
I also know how to throw command output to file plus console by using tee
ls | tee /home/xyz.txt
So I try to run T2 from T1 and redirect T2's output to xyz.txt by doing this:
gnome-terminal -e "ant" 2>&1 | tee /home/xyz.txt
However xyz.txt doesn't get output from T2.
So how to get the output of T2 to xyz.txt from T1?
While this sounds very convoluted and looks like an XY-Problem, here's one way to do it (tested with xterm instead of gnome-terminal).
gnome-terminal -e "ant | tee $(tty) xyz.txt; read dummy"
The tty names the terminal device where you start the terminal, not the new terminal. The read is optional and waits for ENTER so you see what's on the terminal display.

Bash Redirect to a file

I am trying to redirect output of a command to a file. The command I am using (zypper) downloads packages from the internet. The command I am using is
zypper -x -n in geany >> log.txt
The command gradually prints output to the console. The problem I am facing is that the above command writes the command output all at once after the command finishes executing. How do I redirect the bash output as I get it onto the terminal, rather than writing all the command output at the end.
Not with bash itself, but via the tee command:
zipper -x -n in geany | tee log.txt
&>>FILE COMMAND
will append the output of COMMAND to FILE
In your case
&>>log.txt zypper -x -n in geany
If you want to pipe a command through a filter, you must assure that the command outputs to standard output (file descriptor 1) -- if it outputs to standard error (file descriptor 2), you have to redirect the 2 to 1 before the pipe. Take into account that only stdout passed through a pipe.
So you have to do so:
2>&1 COMMAND | FILTER
If you want to grep the output and in the same keep it into a log file, you have to duplicate it with tee, and use a filter like ... | tee log-file | grep options

Resources