io redirection

14
8/11/2019 IO Redirection http://slidepdf.com/reader/full/io-redirection 1/14 IO Redirection 5.1. Simple redirections 5.1.1. What are standard input and standard output? 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 standard input (stdin device, and the screen or a particular terminal window is the standard output (stdout device. !owever, since Linux is a flexible system, these default settings don"t necessarily have to be applied. #he standard output, for example, on a heavily monitored server in a large environment may be a printer. 5.1.2. The redirection operators 5.1.2.1. Output redirection with > and | $ometimes 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. #his is known as redirecting output. %edirection is done using either the &'& (greaterthan symbol, or using the &)& (pipe operator which sends the standard output of one command to another command as standard input.  *s we saw before, the cat 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. nancy!> cat test1

Upload: nikhats10

Post on 02-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 1/14

IO Redirection

5.1. Simple redirections

5.1.1. What are standard input and standard output?

Most Linux commands read input, such as a file or anotherattribute for the command, and write output. By default, input isbeing given with the keyboard, and output is displayed on yourscreen. Your keyboard is your standard input (stdin device, andthe screen or a particular terminal window is the standard output(stdout device.

!owever, since Linux is a flexible system, these default settingsdon"t necessarily have to be applied. #he standard output, forexample, on a heavily monitored server in a large environmentmay be a printer.

5.1.2. The redirection operators

5.1.2.1. Output redirection with > and |

$ometimes you will want to put output of a command in a file, oryou may want to issue another command on the output of onecommand. #his is known as redirecting output. %edirection isdone using either the &'& (greaterthan symbol, or using the &)&(pipe operator which sends the standard output of one commandto another command as standard input.

 *s we saw before, the cat command concatenates files and putsthem all together to the standard output. By redirecting this outputto a file, this file name will be created or overwritten if it alreadyexists, so take care.

nancy!> cat test1

Page 2: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 2/14

some words

nancy!> cat test2some other words

nancy!> cat test1 test2 > test"

nancy!> cat test"some wordssome other words

#on$t o%erwrite&

Be careful not to overwrite existing (important files whenredirecting output. Many shells, including Bash, have a builtinfeature to protect you from that risk+ noclobber. $ee the nfopages for more information. n Bash, you would want to add theset o noclobber command to your .bashrc configuration file inorder to prevent accidental overwriting of files.

%edirecting &nothing& to an existing file is e-ual to emptying thefile+

nancy!> ls 'l list'rw'rw'r'' 1 nancy nancy 11( )pr 2 1*+, list

nancy!> > list

nancy!> ls 'l list'rw'rw'r'' 1 nancy nancy + )pr - 12+1 list

#his process is called truncating.

#he same redirection to an nonexistent file will create a newempty file with the given name+

Page 3: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 3/14

nancy!> ls 'l newlistls newlist o such /ile or directory

nancy!> > newlist

nancy!> ls 'l newlist'rw'rw'r'' 1 nancy nancy + )pr - 12+5 newlist

hapter / gives some more examples on the use of this sort ofredirection.

$ome examples using piping of commands+

#o find a word within some text, display all lines matching&pattern0&, and exclude lines also matching &pattern1& from beingdisplayed+

0rep pattern1 /ile | 0rep '% pattern2

#o display output of a directory listing one page at a time+

ls 'la | less

#o find a file in a directory+

ls 'l | 0rep parto//ilename

5.1.2.2. Input redirection

n another case, you may want a file to be the input for acommand that normally wouldn"t accept a file as an option. #hisredirecting of input is done using the &2& (lessthan symboloperator.

Below is an example of sending a file to somebody, using input

Page 4: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 4/14

redirection.

andy!> mail mie3somewhere.or0 4 todo

f the user mike exists on the system, you don"t need to type thefull address. f you want to reach somebody on the nternet, enterthe fully -ualified address as an argument to mail.

#his reads a bit more difficult than the beginner"s cat /ile | mailsomeone but it is of course a much more elegant way of usingthe available tools.

5.1.2.". 6om7inin0 redirections

#he following example combines input and output redirection. #hefile text.txt is first checked for spelling mistakes, and the output isredirected to an error log file+

spell 4 te8t.t8t > error.lo0

#he following command lists all commands that you can issue toexamine another file when using less+

mie!> less ''help | 0rep 'i e8amine  e 9/ile: ;8amine a new /ile.  n < ;8amine the ='th ne8t /ile /rom the commandline.  p < ;8amine the ='th pre%ious /ile /rom the

command line.  8 < ;8amine the /irst =or 'th /ile /rom the commandline.

#he i option is used for caseinsensitive searches rememberthat 345 systems are very casesensitive.

Page 5: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 5/14

f you want to save output of this command for future reference,redirect the output to a file+

mie!> less ''help | 0rep 'i e8amine > e8amine'/iles'in'less

mie!> cat e8amine'/iles'in'less  e 9/ile: ;8amine a new /ile.  n < ;8amine the ='th ne8t /ile /rom the commandline.  p < ;8amine the ='th pre%ious /ile /rom thecommand line.

  8 < ;8amine the /irst =or 'th /ile /rom the commandline.

6utput of one command can be piped into another commandvirtually as many times as you want, 7ust as long as thesecommands would normally read input from standard input andwrite output to the standard output. $ometimes they don"t, butthen there may be special options that instruct these commandsto behave according to the standard definitions8 so read thedocumentation (man and nfo pages of the commands you use ifyou should encounter errors.

 *gain, make sure you don"t use names of existing files that youstill need. %edirecting output to existing files will replace thecontent of those files.

5.1.2.-. The >> operator 

nstead of overwriting file data, you can also append text to anexisting file using two subse-uent greaterthan signs+

9xample+

Page 6: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 6/14

mie!> cat wishlistmore moneyless wor

mie!> date >> wishlist

mie!> cat wishlistmore moneyless worThu e7 2* 2+2"+( 6;T 2++2

#he date command would normally put the last line on the screen8

now it is appended to the file wishlist.

5.2. )d%anced redirection /eatures

5.2.1. @se o/ /ile descriptors

#here are three types of :6, which each have their own identifier,called a file descriptor+

  standard input+ ;

  standard output+ 0

  standard error+ 1

n the following descriptions, if the file descriptor number isomitted, and the first character of the redirection operator is 2, the

redirection refers to the standard input (file descriptor ;. f thefirst character of the redirection operator is ', the redirectionrefers to the standard output (file descriptor 0.

$ome practical examples will make this more clear+

Page 7: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 7/14

ls > dirlist 2>A1

will direct both standard output and standard error to the filedirlist, while the command

ls 2>A1 > dirlist

will only direct standard output to dirlist. #his can be a usefuloption for programmers.

#hings are getting -uite complicated here, don"t confuse the useof the ampersand here with the use of it in $ection <.0.1.0, where

the ampersand is used to run a process in the background. !ere,it merely serves as an indication that the number that follows isnot a file name, but rather a location that the data stream ispointed to. *lso note that the biggerthan sign should not beseparated by spaces from the number of the file descriptor. f itwould be separated, we would be pointing the output to a fileagain. #he example below demonstrates this+

9nancy3asus B%arBtmp:C ls 2> tmp

9nancy3asus B%arBtmp:C ls 'l tmp'rw'rw'r'' 1 nancy nancy + Sept ( 125* tmp

9nancy3asus B%arBtmp:C ls 2 > tmpls 2 o such /ile or directory

#he first command that nancy executes is correct (eventhough no

errors are generated and thus the file to which standard error isredirected is empty. #he second command expects that 1 is a filename, which does not exist in this case, so an error is displayed.

 *ll these features are explained in detail in the Bash nfo pages.

Page 8: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 8/14

5.2.2. ;8amples

5.2.2.1. )nalyDin0 errors

f your process generates a lot of errors, this is a way tothoroughly examine them+

command 2>A1 | less

#his is often used when creating new software using the makecommand, such as in+

andy!Bnewso/t> mae all 2>A1 | less''output ommitted''

5.2.2.2. Separatin0 standard output /rom standard error 

onstructs like these are often used by programmers, so thatoutput is displayed in one terminal window, and errors in another.=ind out which pseudo terminal you are using issuing the ttycommand first+

andy!Bnewso/t> mae all 2> Bde%BptsB(

5.2.2.". Writin0 to output and /iles simultaneously

You can use the tee command to copy input to standard outputand one or more output files in one move. 3sing the a option totee results in appending input to the file(s. #his command is

useful if you want to both see and save output. #he ' and ''operators do not allow to perform both actions simultaneously.

#his tool is usually called on through a pipe (), as demonstratedin the example below+

Page 9: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 9/14

mireille !Btest> date | tee /ile1 /ile2Thu Eun 1+ 111+"- 6;ST 2++-

mireille !Btest> cat /ile1

Thu Eun 1+ 111+"- 6;ST 2++-

mireille !Btest> cat /ile2Thu Eun 1+ 111+"- 6;ST 2++-

mireille !Btest> uptime | tee 'a /ile2 111+51 up 21 days 2121 5( users load a%era0e +.+-+.1F +.2F

mireille !Btest> cat /ile2Thu Eun 1+ 111+"- 6;ST 2++- 111+51 up 21 days 2121 5( users load a%era0e +.+-+.1F +.2F

5.". ilters

5.".1. Gore a7out 0rep

 *s we saw in $ection >.>.>.<, grep scans the output line per line,searching for matching patterns. *ll lines containing the patternwill be printed to standard output. #his behavior can be reversedusing the v option.

$ome examples+ suppose we want to know which files in a certaindirectory have been modified in =ebruary+

 Henny!> ls 'la | 0rep e7

#he 0rep command, like most commands, is case sensitive. 3sethe i option to make no difference between upper and lowercase. * lot of ?43 extensions are available as well, such as

Page 10: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 10/14

colour, which is helpful to highlight searchterms in long lines,and aftercontext, which prints the number of lines after the lastmatching line. You can issue a recursive 0rep that searches allsubdirectories of encountered directories using the r option. *s

usual, options can be combined.

%egular expressions can be used to further detail the exactcharacter matches you want to select out of all the input lines.#he best way to start with regular expressions is indeed to readthe 0rep documentation. *n excellent chapter is included in thegrep nfo page. $ince it would lead us too far discussing the insand outs of regular expressions, it is strongly advised to start here

if you want to know more about them.

@lay around a bit with 0rep, it will be worth the trouble puttingsome time in this most basic but very powerful filtering command.#he exercises at the end of this chapter will help you to getstarted, see $ection A.A.

5.".2. ilterin0 output

#he command sort arranges lines in alphabetical order by default+

thomas!> cat people'I'lie | sort)untie ;mmyoy/riend#adJrandmaGum

Gy 7oss

But there are many more things sort can do. Looking at the filesie, for instance. Cith this command, directory content is sortedsmallest files first, biggest files last+

Page 11: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 11/14

ls 'la | sort 'n 5

Old sort synta8 

You might obtain the same result with ls la ) sort D<n, but this isan old form which does not comply with the current standards.

#he sort command is also used in combination with the uni-program (or sort u to sort output and filter out double entries+

thomas!> cat itemlist

1-25"-5F(-"25F("-555

thomas!> sort itemlist | uniK12"---"2

55555F(

5.-. Summary

Page 12: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 12/14

n this chapter we learned how commands can be linked to eachother, and how input from one command can be used as outputfor another command.

nput:output redirection is a common task on 345 and Linuxmachines. #his powerful mechanism allows flexible use of thebuilding blocks 345 is made of.

#he most commonly used redirections are ' and ). %efer to *ppendix for an overview of redirection commands and othershell constructs.

Ta7le 5'1. ew commands in chapter 5 IBO redirection6ommand Geanin0date Eisplay time and date information.set onfigure shell options.sort $ort lines of text.uni- %emove duplicate lines from a sorted file.

5.5. ;8ercises

#hese exercises give more examples on how to combinecommands. #he main goal is to try and use the 9nter key as littleas possible.

 *ll exercises are done using a normal user E, so as to generatesome errors. Chile you"re at it, don"t forget to read those manpagesF

  < 3se the cut command on the output of a long directory listingin order to display only the file permissions. #hen pipe this outputto sort and uni- to filter out any double lines. #hen use the wc tocount the different permission types in this directory.

  < @ut the output of date in a file. *ppend the output of ls to this

Page 13: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 13/14

file. $end this file to your local mailbox (don"t specify anything2Gdomain', 7ust the user name will do. Chen using Bash, youwill see a new mail notice upon success.

  < List the devices in :dev which are currently used by your 3E.@ipe through less to view them properly.

  < ssue the following commands as a nonprivileged user.Eetermine standard input, output and error for each command.

cat none8istent/ile

/ile Bs7inBi/con/i0

0rep root BetcBpasswd BetcBno/iles > 0represults

 BetcBinit.dBsshd start > B%arBtmpBoutput

 BetcBinit.dBcrond start > B%arBtmpBoutput 2>A1

4ow check your results by issuing the commands again, nowredirecting standardoutput to the file :var:tmp:output and standarderror to the file :var:tmp:error.

Low many processes are you currently runnin0?

Low many in%isi7le /iles are in your home directory?

@se locate to /ind documentation a7out the ernel.

ind out which /ile contains the /ollowin0 entry

root8++rootBrootB7inB7ash

Page 14: IO Redirection

8/11/2019 IO Redirection

http://slidepdf.com/reader/full/io-redirection 14/14

 *nd this one+

system root

$ee what happens upon issuing this command+

> timeM date >> timeM cat 4 time

Chat command would you use to check which script in :etc:init.dstarts a given processH