0

This question already has an answer here:

I am running a command say nmap www.somesite.com>file.txt so that i can get the output to file.txt. But if I do so, I am not able to see the output of that command on terminal. Is it possible to make it visble on terminal also.

I know the usage of tee, but I wish to do this specifically in this way.

marked as duplicate by muru, Eric Carvalho, Radu Rădeanu command-line Dec 12 '14 at 19:03

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.

  • I am not looking for one another command. I wish to know whether it is possible to do this by that command itself. I tried verbose but not working. – Anandu M Das Dec 11 '14 at 12:06
  • 1
    In general, no. – muru Dec 11 '14 at 12:08
  • I don't understand why everyone is saying it is not possible? Is he not asking to redirect stderr to stdout? I have tested my answer and it seems to do what he asks. – cremefraiche Dec 11 '14 at 12:10
  • @cremefraiche No. Say I do echo A>some.file.txt. OP Wants A to appear on the screen and in the file as well. – muru Dec 11 '14 at 12:11
  • 3
    What's wrong with tee, which was designed exactly for this purpose? – mivk Dec 11 '14 at 12:23
-1

It is not possible to have command line view and redirect process at the same time

But you can use the following command to use both the process in sequence

nmap www.somesite.com && nmap www.somesite.com > file.txt

First it will execute the command in terminal and then it will save the output as a file

Hope this helps!

  • 1
    This is cool. But It doubles the overhead rite? – Anandu M Das Dec 11 '14 at 12:08
  • 2
    It is a bit incorrect to say "redirect process", as the shell will only redirect output, not the process, and hence it is very well possible to dump the same text at many places. – Registered User Dec 11 '14 at 12:10
  • 1
    This runs the command twice. A very inefficient solution. Particularly with nmap, which may be very slow depending on options used and size of target network. – mivk Dec 11 '14 at 12:22
3

tee is designed to split STDIN into a file and back out to STDOUT.

In simple terms, just pipe it through, like so:

nmap www.somesite.com | tee file.txt

The current accepted alternative involves running nmap twice which is a horrible idea.
You'd be better off running it once to file and then outputting the file.

nmap www.somesite.com > file.txt; cat file.txt
  • Please read the question completely. I was looking for an answer other than tee. Anyway thanks ;) – Anandu M Das Dec 11 '14 at 12:16
  • 1
    If you don't want tee, please explain why or what it is you're actually trying to do. Otherwise this is Ask Ubuntu, Ubuntu ships tee, therefore tee is always an option here. – Oli Dec 11 '14 at 12:41
1

That is exactly what tee is for. Why do you not want to use that?

An alternative might be to capture the output, and echo it twice:

output=$(nmap localhost)
echo "$output"
echo "$output" > somefile.txt

However, in the special case of nmap, you can take advantage of it's output option -ox :

      OUTPUT:
         -oN/-oX/-oS/-oG <file>: Output scan in normal, XML, s|<rIpt kIddi3,
            and Grepable format, respectively, to the given filename.

For example:

nmap -oN somefile.txt localhost

Seems to do exactly what you want: output to the terminal and also write to file.

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