CS155

CS155: Introduction to Unix

Spring 2018

Commands 1

See this page as a slide show

CS155 Commands1

Review

Command structurecommand [option]... [argument]...
Commandsman, pwd, ls, cd
Pathnames/, ~, ., ..

Learning More with man and info

Many Unix command have options that modify their behavior.

To learn more about a specific command try:

Consider them travel guides. man and info can answer many of your questions.

Viewing Files

% cat ~/monster
			/|  /|
			||__||
		       /  o o \__
		      /		 \
		     /	    \	  \
		    /	_    \	   \
		   /	|\____\	    \
		  /	| | | |\____/
		 /	 \| | | |/ |	 __
		/  /  \	  -------  |_____||__
	       /   |   |	   |	   --|
	       |   |   |	   |_____  --|
	       |<–|_|_|_|––<<<	   |	 \----
	       /\		   |
	      / /\	  |	   /
	     / /  |	  |	  |
	 ___/ /	  |	  |	  |
	|____/	  c_c_c_C/ \C_c_c_c

The simplest way to view the contents of a file is to use:

Modifying the file system

% ls
% cp ~/monster .
% ls
monster
% cp monster foo
% ls
foo  monster
% mv foo bar
% ls
bar  monster
% rm monster
% ls
bar

Here are three commands for handling files:

Modifying the file system (cont.)

% cp ~/monster .
% ls -l
total 4
-rw------- 1 cs155 class 401 Jan 20 04:12 monster
% mkdir new_dir
% ls -l
total 4
-rw------- 1 cs155 class 401 Jan 20 04:12 monster
drwx------ 2 cs155 class  40 Jan 20 04:12 new_dir
% rmdir new_dir
% ls -l
total 4
-rw------- 1 cs155 class 401 Jan 20 04:12 monster

The previous commands allowed us to move files around in the file system, but what about modifying the structure itself?

echo, echo, echo, echo, echo, echo, echo, echo, echo, echo, echo, echo, echo, echo, echo,

% echo hey children
hey children
% echo hey         children
hey children
% echo "hey         children"
hey         children
% echo "~"
~
% echo ~
/s/bach/a/class/cs155

The echo command prints some text to the screen.

Is this useful for anything? Sure!

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
Sat Jan 20 04:12:22 MST 2018
% date >chef
% cat chef
Sat Jan 20 04:12:22 MST 2018

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 Jan 20 04:12 monster
-rw------- 1 cs155 class   0 Jan 20 04:12 tempfile
% cat tempfile
total 4
-rw------- 1 cs155 class 401 Jan 20 04:12 monster
-rw------- 1 cs155 class   0 Jan 20 04:12 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

Modified: 2018-01-06T13:06

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building