CS155: Introduction to Unix

Spring 2018

Redirection

See this page as a slide show

CS155 Redirection

Redirection

What if we don’t want the output of a command to go to the screen but to a file? Or get the input to a command from a file instead of the keyboard?

Redirection allows us to change where a command gets input or sends output.

Output redirection

% echo "hello there, children"
hello there, children
% echo "hello there, children" > chef
% ls
chef
% cat chef
hello there, children
% echo "hey chef" >> chef
% cat chef
hello there, children
hey chef
% date
Fri Apr 26 14:58:16 MDT 2024
% date >chef
% cat chef
Fri Apr 26 14:58:16 MDT 2024

More output redirection

% cp ~/monster .
% ls
monster
% ls >file_list
% ls
file_list  monster
% cat file_list
file_list  monster
% pwd >> file_list
% cat file_list
file_list  monster
/tmp/PmWiki.tmp

echo isn’t the only command whose output we can redirect.

Input redirection

% cp ~/monster .
% ls -l >tempfile
% cat <tempfile
total 4
-rw------- 1 cs155 class 401 Apr 26 14:58 monster
-rw------- 1 cs155 class   0 Apr 26 14:58 tempfile
% cat tempfile
total 4
-rw------- 1 cs155 class 401 Apr 26 14:58 monster
-rw------- 1 cs155 class   0 Apr 26 14:58 tempfile

Many commands accept input from a file or a command line argument.

Output redirection to a program

% ls -l > tempfile
% more tempfile
...
% rm tempfile
% ls -l | more
...
%

This is useful for combining operations without making temporary files

Summary

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-02-07T14:28

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building