80 likes | 191 Views
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.
E N D
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
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
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
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
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)
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)
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.)