CS155

CS155: Introduction to Unix

Fall 2017

Commands 1

See this page as a slide show

CS155 Commands1

Review

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

Notes

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

% pwd
/tmp
% ls
my_file1
% cat my_file1
Hi, everybody!
Hi, Dr. Nick!

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

Modifying the file system

% ls
my_file1
% cp my_file1 my_file2
% ls
my_file1  my_file2
% mv my_file1 file1
% ls
file1  my_file2
% rm file1
% ls
my_file2

Here are three commands for handling files:

Modifying the file system (cont.)

% ls
my_file2
% mkdir new_dir
% ls -l
total 8
-rw------- 1 boese fac 4 Jan 19 14:43 my_file2
drwx------ 2 boese fac 4096 Jan 19 14:44 new_dir
% rmdir new_dir
% ls
my_file2

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 hey children
hey children
%echo hey         children
hey children
% echo "hey         children"
hey         children
% echo "~"
~
% echo ~
/Users/davematt
%

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  my_file2
% cat chef
hello there, children
% echo "hey chef" >> chef
% cat chef
hello there, children
hey chef
% date
Fri Apr 19 12:23:12 MDT 2024
% date >chef
% cat chef
Fri Apr 19 12:23:12 MDT 2024

More output redirection

% ls
chef my_file2
% ls >file_list
% ls
chef  file_list  my_file2
% cat file_list
chef
file_list
my_file2
% pwd >> file_list
% cat file_list
chef
file_list
my_file2
/tmp

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

Input redirection

% ls -l >tempfile
% more <tempfile
...
% more 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: 2017-08-25T12:31

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