old-www/LDP/intro-linux/html/sect_05_01.html

693 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Simple redirections</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Introduction to Linux"
HREF="index.html"><LINK
REL="UP"
TITLE="I/O redirection"
HREF="chap_05.html"><LINK
REL="PREVIOUS"
TITLE="I/O redirection"
HREF="chap_05.html"><LINK
REL="NEXT"
TITLE="Advanced redirection features"
HREF="sect_05_02.html"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Introduction to Linux: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="chap_05.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. I/O redirection</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sect_05_02.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="sect_05_01"
></A
>5.1. Simple redirections</H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_05_01_01"
></A
>5.1.1. What are standard input and standard output?</H2
><P
>&#13;Most Linux commands read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your <EM
>standard input</EM
> (stdin) device, and the screen or a particular terminal window is the <EM
>standard output</EM
> (stdout) device.
</P
><P
>However, since Linux is a flexible system, these default settings don't necessarily have to be applied. The standard output, for example, on a heavily monitored server in a large environment may be a printer.</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_05_01_02"
></A
>5.1.2. The redirection operators</H2
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_05_01_02_01"
></A
>5.1.2.1. Output redirection with &#62; and |</H3
><P
>Sometimes you will want to put output of a command in a file, or you may want to issue another command on the output of one command. This is known as redirecting output. Redirection is done using either the <SPAN
CLASS="QUOTE"
>"&#62;"</SPAN
> (greater-than symbol), or using the <SPAN
CLASS="QUOTE"
>"|"</SPAN
> (pipe) operator which sends the standard output of one command to another command as standard input.</P
><P
>As we saw before, the <B
CLASS="command"
>cat</B
> command concatenates files and puts them all together to the standard output. By redirecting this output to a file, this file name will be created - or overwritten if it already exists, so take care.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>cat test1</B
>
some words
<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>cat test2</B
>
some other words
<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>cat test1 test2 &#62; test3</B
>
<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>cat test3</B
>
some words
some other words
</PRE
></FONT
></TD
></TR
></TABLE
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/warning.gif"
HSPACE="5"
ALT="Warning"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Don't overwrite!</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Be careful not to overwrite existing (important) files when redirecting output. Many shells, including <SPAN
CLASS="application"
>Bash</SPAN
>, have a built-in feature to protect you from that risk: <B
CLASS="command"
>noclobber</B
>. See the <SPAN
CLASS="application"
>Info</SPAN
> pages for more information. In <SPAN
CLASS="application"
>Bash</SPAN
>, you would want to add the <B
CLASS="command"
>set <TT
CLASS="option"
>-o</TT
> <TT
CLASS="parameter"
><I
>noclobber</I
></TT
></B
> command to your <TT
CLASS="filename"
>.bashrc</TT
> configuration file in order to prevent accidental overwriting of files.</P
></TD
></TR
></TABLE
></DIV
><P
>Redirecting <SPAN
CLASS="QUOTE"
>"nothing"</SPAN
> to an existing file is equal to emptying the file:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>ls -l list</B
>
-rw-rw-r-- 1 nancy nancy 117 Apr 2 18:09 list
<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>&#62; list</B
>
<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>ls -l list</B
>
-rw-rw-r-- 1 nancy nancy 0 Apr 4 12:01 list
</PRE
></FONT
></TD
></TR
></TABLE
><P
>This process is called <EM
>truncating</EM
>.</P
><P
>The same redirection to an nonexistent file will create a new empty file with the given name:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>ls -l newlist</B
>
ls: newlist: No such file or directory
<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>&#62; newlist</B
>
<TT
CLASS="prompt"
>nancy:~&#62;</TT
> <B
CLASS="command"
>ls -l newlist</B
>
-rw-rw-r-- 1 nancy nancy 0 Apr 4 12:05 newlist
</PRE
></FONT
></TD
></TR
></TABLE
><P
><A
HREF="chap_07.html"
>Chapter 7</A
> gives some more examples on the use of this sort of redirection.</P
><P
>Some examples using piping of commands:</P
><P
>To find a word within some text, display all lines matching <SPAN
CLASS="QUOTE"
>"pattern1"</SPAN
>, and exclude lines also matching <SPAN
CLASS="QUOTE"
>"pattern2"</SPAN
> from being displayed:</P
><P
><B
CLASS="command"
>grep <TT
CLASS="parameter"
><I
>pattern1</I
></TT
> <TT
CLASS="filename"
>file</TT
> | grep <TT
CLASS="option"
>-v</TT
> <TT
CLASS="parameter"
><I
>pattern2</I
></TT
></B
> </P
><P
>To display output of a directory listing one page at a time:</P
><P
><B
CLASS="command"
>ls <TT
CLASS="option"
>-la</TT
> | less</B
> </P
><P
>To find a file in a directory:</P
><P
><B
CLASS="command"
>ls <TT
CLASS="option"
>-l</TT
> | grep <TT
CLASS="parameter"
><I
>part_of_file_name</I
></TT
></B
> </P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_05_01_02_02"
></A
>5.1.2.2. Input redirection</H3
><P
>In another case, you may want a file to be the input for a command that normally wouldn't accept a file as an option. This redirecting of input is done using the <SPAN
CLASS="QUOTE"
>"&#60;"</SPAN
> (less-than symbol) operator.</P
><P
>Below is an example of sending a file to somebody, using input redirection.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>andy:~&#62;</TT
> <B
CLASS="command"
>mail mike@somewhere.org &#60; to_do</B
>
</PRE
></FONT
></TD
></TR
></TABLE
><P
>If the user <EM
>mike</EM
> exists on the system, you don't need to type the full address. If you want to reach somebody on the Internet, enter the fully qualified address as an argument to <B
CLASS="command"
>mail</B
>.</P
><P
>This reads a bit more difficult than the beginner's <B
CLASS="command"
>cat <TT
CLASS="filename"
>file</TT
> | mail <TT
CLASS="parameter"
><I
>someone</I
></TT
></B
>, but it is of course a much more elegant way of using the available tools.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_05_01_02_03"
></A
>5.1.2.3. Combining redirections</H3
><P
>The following example combines input and output redirection. The file <TT
CLASS="filename"
>text.txt</TT
> is first checked for spelling mistakes, and the output is redirected to an error log file:</P
><P
><B
CLASS="command"
>spell &#60; <TT
CLASS="filename"
>text.txt</TT
> &#62; <TT
CLASS="filename"
>error.log</TT
></B
></P
><P
>The following command lists all commands that you can issue to examine another file when using <B
CLASS="command"
>less</B
>:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>mike:~&#62;</TT
> <B
CLASS="command"
>less --help | grep -i examine</B
>
:e [file] Examine a new file.
:n * Examine the (N-th) next file from the command line.
:p * Examine the (N-th) previous file from the command line.
:x * Examine the first (or N-th) file from the command line.
</PRE
></FONT
></TD
></TR
></TABLE
><P
>The <TT
CLASS="option"
>-i</TT
> option is used for case-insensitive searches - remember that UNIX systems are very case-sensitive.</P
><P
>If you want to save output of this command for future reference, redirect the output to a file:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>mike:~&#62;</TT
> <B
CLASS="command"
>less --help | grep -i examine &#62; examine-files-in-less</B
>
<TT
CLASS="prompt"
>mike:~&#62;</TT
> <B
CLASS="command"
>cat examine-files-in-less</B
>
:e [file] Examine a new file.
:n * Examine the (N-th) next file from the command line.
:p * Examine the (N-th) previous file from the command line.
:x * Examine the first (or N-th) file from the command line.
</PRE
></FONT
></TD
></TR
></TABLE
><P
>Output of one command can be piped into another command virtually as many times as you want, just as long as these commands would normally read input from standard input and write output to the standard output. Sometimes they don't, but then there may be special options that instruct these commands to behave according to the standard definitions; so read the documentation (man and <SPAN
CLASS="application"
>Info</SPAN
> pages) of the commands you use if you should encounter errors.</P
><P
>Again, make sure you don't use names of existing files that you still need. Redirecting output to existing files will replace the content of those files.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_05_01_02_04"
></A
>5.1.2.4. The &#62;&#62; operator</H3
><P
>Instead of overwriting file data, you can also append text to an existing file using two subsequent greater-than signs:</P
><P
>Example:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>mike:~&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>wishlist</TT
></B
>
more money
less work
<TT
CLASS="prompt"
>mike:~&#62;</TT
> <B
CLASS="command"
>date &#62;&#62; <TT
CLASS="filename"
>wishlist</TT
></B
>
<TT
CLASS="prompt"
>mike:~&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>wishlist</TT
></B
>
more money
less work
Thu Feb 28 20:23:07 CET 2002
</PRE
></FONT
></TD
></TR
></TABLE
><P
>The <B
CLASS="command"
>date</B
> command would normally put the last line on the screen; now it is appended to the file <TT
CLASS="filename"
>wishlist</TT
>.
</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="chap_05.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sect_05_02.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>I/O redirection</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="chap_05.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Advanced redirection features</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>