1 / 8

CIS 240 Introduction to UNIX Instructor: Sue Sampson

CIS 240 Introduction to UNIX Instructor: Sue Sampson. CIS240 – UNIX File Redirection. Standard Files Standard files are opened by the kernel for every command that gets executed There are three: stdin Used for input stdout Used for output stderr Used for error messages.

salaam
Download Presentation

CIS 240 Introduction to UNIX Instructor: Sue Sampson

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 240 Introduction to UNIX Instructor: Sue Sampson

  2. CIS240 – UNIX File Redirection Standard Files • Standard files are opened by the kernel for every command that gets executed • There are three: • stdin Used for input • stdout Used for output • stderr Used for error messages

  3. CIS240 – UNIX File Redirection Default Associations • If another file is not specified in the command line, the kernel assigns these files as follows: • stdin keyboard • stdout display screen • stderr display screen

  4. CIS240 – UNIX File Redirection Remember -- the UNIX file system supports: • - Simple/ordinary files • - Directories • - Symbolic (soft) links • - Special files (devices) • - Named pipes (FIFO) • - Hard links

  5. CIS240 – UNIX File Redirection When Executing Commands • We can re-direct the input to a command from any Unix file • We can re-direct the output from a command to any Unix file • We can re-direct error messages from a command to any Unix file

  6. CIS240 – UNIX File Redirection Redirection (0=input, 1=output, 2=error out) • Input Syntax: • command < input-file sort < tempfile.dat (sort will get its input from tempfile.dat instead of the keyboard) • Output Syntax • command > output-file ls –al > directory.txt (The output from the ls command would go to directory.txt instead of the display screen) • Error Syntax • command 2> error-file ifup eth0 2> error.log (The error messages generated by the ifup command would be sent to a file called error.log)

  7. CIS240 – UNIX File Redirection Piping • Piping takes the stdout from one command and makes it the stdin for another – filtering as you proceed. • Syntax: command1|command2|…|commandn grep Sampson class.txt | sort +3 (Look for lines that contain Sampson in the class.txt file – that becomes input for the sort command, which sorts the results on the 4th column, and outputs to the standard out.) • Add redirection… grep Sampson class.txt | sort +3 >sortedclass.txt (Look for lines that contain Sampson in the class.txt file – that becomes input for the sort command, which sorts the results on the 4th column, and outputs to the sortedclass.txt)

  8. CIS240 – UNIX File Redirection tee Utility • Combines redirection and piping – when you want the input of subsequent commands to be the output files of previous commands. • Syntax: command1|tee file1…filen|command2 cat names | grep “Sampson” | tee file1.txt wc -1 (Look for lines that contain Sampson in the names file – send the results to file1.txt, which becomes input for the wc command, the end result sends the resulting line count to the monitor.)

More Related