Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 17 Apr 2001 01:05:18 -0600 (CST)
From:      Ryan Thompson <ryan@sasknow.com>
To:        Beech Rintoul <akbeech@anchoragerescue.org>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: Redirect output
Message-ID:  <Pine.BSF.4.21.0104170042380.43274-100000@ren.sasknow.com>
In-Reply-To: <01041620361200.85720@galaxy.anchoragerescue.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Beech Rintoul wrote to freebsd-questions@FreeBSD.ORG:

> Hi,
> I'm logged on one of my remote machines via telnet. How do I redirect
> the output of a command from the terminal to a file? I know this is a
> simple question, but I missed it along the way somewhere.

That IS a simple question. Just to be cheeky, I'm going to give you a
long answer ;-)

(If you want the gory details of how your shell handles redirection, read
manpages for sh(1), csh(1), etc)

But, to give you the simple answer, you can use the > operator in all user
shells to redirect standard output to a file. So, if you want to dump the
contents of a directory to a file, try this command:

	ls > ls.out

ls.out will then contain the output of the ls command--one file per line.

If you only log the output of simple commands, you can safely stop reading
here. If thou seeketh a more gory response, read on.

-------------------------------------------------------------------

The above command redirects standard output only. There is another
"output" buffer, called "standard error", to which error messages (or just
really important things) are written. If you attempt to redirect standard
output, but you still see output on your terminal screen from that
application, the output was most likely sent to standard error. To catch
standard error by itself, use 2> (filename). If you want to send stderr
and stdout to the same place, use >(filename) 2>&1 (order is important,
and the "why" is getting quite far removed from the scope of your
question, but if you're interested, I can certainly explain it better).
This leads to my next point:

If you want to get more complex than logging the output of a simple
"command" (i.e., you want to make a log of something interactive, like
running fsck(8) to fix filesystem errors), use script(1).  Type "script"
at your prompt first before you do your interactive tasks. When you are
finished, type "exit" at the prompt, and whatever you did (including
everything you typed--as well as all of your typing mistakes ;-) will be
contained in a file called "typescript", unless you specified a different
file. Read script(1) for more details.

There is also a very handy and unique construct in UNIX known as a pipe.
Pipes allow you to, well, "make a pipeline", between successive commands.
That is, all of the output from one command will be sent as input to the
next command. A simple example of a typical pipe would be the problem of
counting the number of files in a directory. The UNIX command wc(1) ("word
count") counts the number of lines, words, and characters in a file. The
pipe (note the "|" operator--this is the pipe symbol).

	ls | wc -l

ls lists the contents of the directory, and sends its output to wc. wc
takes that input, and (thanks to the -l) parameter), dutifully counts the
lines and writes them to the terminal.


> 
> TIA - Beech
> 
> 

Hope this helps,

- Ryan "that WAS the short version" Thompson

-- 
  Ryan Thompson <ryan@sasknow.com>
  Network Administrator, Accounts

  SaskNow Technologies - http://www.sasknow.com
  #106-380 3120 8th St E - Saskatoon, SK - S7H 0W2

        Tel: 306-664-3600   Fax: 306-664-1161   Saskatoon
  Toll-Free: 877-727-5669     (877-SASKNOW)     North America


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0104170042380.43274-100000>