Unix Command Summary: Tutorial
Unix Command Summary: Tutorial
See the Unix tutorial for a leisurely, self-paced introduction on how to use the commands
listed below. For more documentation on a command, consult a good book, or use the
man pages. For example, for more information on grep, use the command man grep.
Contents
cat --- for creating and displaying short files
chmod --- change permissions
cd --- change directory
cp --- for copying files
date --- display date
echo --- echo argument
ftp --- connect to a remote machine to download or upload files
grep --- search file
head --- display first part of file
ls --- see what files you have
lpr --- standard print command (see also print
more --- use to read files
mkdir --- create directory
mv --- for moving and renaming files
ncftp --- especially good for downloading files via anonymous ftp.
print --- custom print command (see also lpr
pwd --- find out what directory you are in
rm --- remove a file
rmdir --- remove directory
rsh --- remote shell
setenv --- set an environment variable
sort --- sort file
tail --- display last part of file
tar --- create an archive, add or extract files
telnet --- log in to another machine
wc --- count characters, words, lines
cat
!his is one of the most flexible Unix commands. "e can use to create, view and
concatenate files. For our first example we create a three-item #nglish-Spanish dictionary
in a file called $dict.$
% cat >dict
red rojo
green verde
blue azul
<control-D>
%
%control-&' stands for $hold the control key down, then tap (d($. !he symbol > tells the
computer that what is typed is to be put into the file dict. !o view a file we use cat in a
different way)
% cat dict
red rojo
green verde
blue azul
%
*f we wish to add text to an existing file we do this)
% cat >>dict
white blanco
black negro
<control-D>
%
+ow suppose that we have another file tmp that looks like this)
% cat tmp
cat gato
dog perro
%
!hen we can ,oin dict and tmp like this)
% cat dict tmp >dict2
"e could check the number of lines in the new file like this)
% wc -l dict2
8
!he command wc counts things --- the number of characters, words, and line in a file.
chmod
!his command is used to change the permissions of a file or directory. For example to
make a file essay.001 readable by everyone, we do this)
% chmod a+r essay.!
!o make a file, e.g., a shell script mycommand executable, we do this
% chmod +" mycommand
+ow we can run mycommand as a command.
!o check the permissions of a file, use ls -l . For more information on chmod, use man
chmod.
cd
Use cd to change directory. Use pwd to see what directory you are in.
% cd english
% pwd
% /u/ma/jeremy/english
% ls
novel poems
% cd novel
% pwd
% /u/ma/jeremy/english/novel
% ls
ch1 ch2 ch3 journal scrapboo
% cd ..
% pwd
% /u/ma/jeremy/english
% cd poems
% cd
% /u/ma/jeremy
-eremy began in his home directory, then went to his english subdirectory. .e listed this
directory using ls , found that it contained two entries, both of which happen to be
diretories. .e cd(d to the diretory novel, and found that he had gotten only as far as
chapter / in his writing. !hen he used cd .. to ,ump back one level. *f had wanted to ,ump
back one level, then go to poems he could have said cd ../poems. Finally he used cd with
no argument to ,ump back to his home directory.
cp
Use cp to copy files or directories.
% cp #oo #oo.2
!his makes a copy of the file foo.
% cp $%poems%jabber .
!his copies the file ,abber in the directory poems to the current directory. !he symbol $.$
stands for the current directory. !he symbol $0$ stands for the home directory.
date
Use this command to check the date and time.
% date
!ri "an # 08$%2$&2 '() 1**%
echo
!he echo command echoes its arguments. .ere are some examples)
% echo this
this
% echo &'D()*+
/usr/local/bin/emacs
% echo &,+(-)'+
b12*lab1
!hings like +,-.)/, are so-called environment variables. !his one stores the name of the
default printer --- the one that print ,obs will go to unless you take some action to change
things. !he dollar sign before an environment variable is needed to get the value in the
variable. !ry the following to verify this)
% echo ,+(-)'+
+,-.)/,
ftp
Use 0tp to connect to a remote machine, then upload or download files. See also) nc0tp
Example 1: "e(ll connect to the machine 0ubar.net, then change director to mystu00,
then download the file home1or11)
% #tp solitude
2onnected to 0ubar.net.
220 0ubar.net !)+ server 34ersion 1u52.&3116 'on 7pr 18 18$2#$33
'9) 1**&6 ready.
.ame 3solitude$carlson6$ jeremy
331 +ass1ord re:uired 0or jeremy.
+ass1ord$
230 ;ser jeremy logged in.
0tp> cd mystu##
2%0 2<9 command success0ul.
0tp> get homework!!
0tp> .uit
Example 2: "e(ll connect to the machine 0ubar.net, then change director to mystu00,
then upload the file collected5letters)
% #tp solitude
2onnected to 0ubar.net.
220 0ubar.net !)+ server 34ersion 1u52.&3116 'on 7pr 18 18$2#$33
'9) 1**&6 ready.
.ame 3solitude$carlson6$ jeremy
331 +ass1ord re:uired 0or jeremy.
+ass1ord$
230 ;ser jeremy logged in.
0tp> cd mystu##
2%0 2<9 command success0ul.
0tp> put collected-letters
0tp> .uit
!he ftp program sends files in ascii (text format unless you specify binary mode)
0tp> binary
0tp> put #oo
0tp> ascii
0tp> get bar
!he file 0oo was transferred in binary mode, the file bar was transferred in ascii mode.
grep
Use this command to search for information in a file or files. For example, suppose that
we have a file dict whose contents are
red rojo
green verde
blue azul
1hite blanco
blac negro
!hen we can look up items in our file like this1
% grep red dict
red rojo
% grep blanco dict
1hite blanco
% grep brown dict
%
+otice that no output was returned by grep bro1n. !his is because $brown$ is not in our
dictionary file.
2rep can also be combined with other commands. For example, if one had a file of phone
numbers named $ph$, one entry per line, then the following command would give an
alphabetical list of all persons whose name contains the string $Fred$.
% grep /red ph 0 sort
7lpha= !red$ 3335#%#%
>eta= !reddie$ #%#500**
!redericson= 'olly$ &&&50*81
?amma= !red5?eorge$ 11158#8#
@eta= !rederic$ &3150*88
!he symbol $3$ is called $pipe.$ *t pipes the output of the grep command into the input of
the sort command.
For more information on grep, consult
% man grep
head
Use this command to look at the head of a file. For example,
% head essay.!
displays the first 45 lines of the file essay.001 !o see a specific number of lines, do this)
% head -2 essay.!
!his displays the first 65 lines of the file.
ls
Use ls to see what files you have. 7our files are kept in something called a directory.
% ls
0oo letter2
0oobar letter3
letter1 maple5assignment1
%
+ote that you have six files. !here are some useful variants of the ls command)
% ls l1
letter1 letter2 letter3
%
+ote what happened) all the files whose name begins with $l$ are listed. !he asterisk (8
is the $ wildcard$ character. *t matches any string.
lpr
!his is the standard Unix command for printing a file. *t stands for the ancient $line
printer.$ See
% man lpr
for information on how it works. See print for information on our local intelligent print
command.
mkdir
Use this command to create a directory.
% mkdir essays
!o get $into$ this directory, do
% cd essays
!o see what files are in essays, do this)
% ls
!here shouldn(t be any files there yet, since you ,ust made it. !o create files, see cat or
emacs.
more
9ore is a command used to read text files. For example, we could do this)
% more poems
!he effect of this to let you read the file $poems $. *t probably will not fit in one screen,
so you need to know how to $turn pages$. .ere are the basic commands)
q --- :uit more
spacear --- read next page
return key --- read next line
--- go back one page
For still more information, use the command man more.
m!
Use this command to change the name of file and directories.
% mv #oo #oobar
!he file that was named 0oo is now named 0oobar
ncftp
Use nc0tp for anonymous ftp --- that means you don(t have to have a password.
% nc#tp #tp.#ubar.net
2onnected to 0tp.0ubar.net
> get jokes.t"t
!he file joes.tAt is downloaded from the machine 0tp.0ubar.net.
print
!his is a moderately intelligent print command.
% print #oo
% print notes.ps
% print manuscript.dvi
*n each case print does the right thing, regardless of whether the file is a text file (like
0oo , a postcript file (like notes.ps, or a dvi file (like manuscript.dvi. *n these
examples the file is printed on the default printer. !o see what this is, do
% print
and read the message displayed. !o print on a specific printer, do this)
% print #oo jwb22!
% print notes.ps jwb22!
% print manuscript.dvi jwb22!
!o change the default printer, do this)
% setenv ,+(-)'+ jwb22!
p"d
Use this command to find out what directory you are working in.
% pwd
/u/ma/jeremy
% cd homework
% pwd
/u/ma/jeremy/home1or
% ls
assign51 assign52 assign53
% cd
% pwd
/u/ma/jeremy
%
-eremy began by working in his $home$ directory. !hen he cd (d into his homework
subdirectory. ;d means $ change directory$. .e used pwd to check to make sure he was
in the right place, then used ls to see if all his homework files were there. (!hey were.
!hen he cd#d back to his home directory.
rm
Use rm to remove files from your directory.
% rm #oo
remove 0ooB y
% rm letter1
remove letter1B y
remove letter2B y
remove letter3B n
%
!he first command removed a single file. !he second command was intended to remove
all files beginning with the string $letter.$ .owever, our user (-eremy< decided not to
remove letter/.
rmdir
Use this command to remove a directory. For example, to remove a directory called
$essays$, do this)
% rmdir essays
= directory must be empty before it can be removed. !o empty a directory, use rm.
rsh
Use this command if you want to work on a computer different from the one you are
currently working on. >ne reason to do this is that the remote machine might be faster.
For example, the command
% rsh solitude
connects you to the machine solitude. !his is one of our public workstations and is
fairly fast.
See also) telnet
seten!
% echo &,+(-)'+
labprinter
% setenv ,+(-)'+ myprinter
% echo &,+(-)'+
myprinter
sort
Use this commmand to sort a file. For example, suppose we have a file dict with
contents
red rojo
green verde
blue azul
1hite blanco
blac negro
!hen we can do this)
% sort dict
blac negro
blue azul
green verde
red rojo
1hite blanco
.ere the output of sort went to the screen. !o store the output in file we do this)
% sort dict >dict.sorted
7ou can check the contents of the file dict.sorted using cat , more , or emacs .
tail
Use this command to look at the tail of a file. For example,
% head essay.!
displays the last 45 lines of the file essay.001 !o see a specific number of lines, do this)
% head -2 essay.!
!his displays the last 65 lines of the file.
tar
Use create compressed archives of directories and files, and also to extract directories and
files from an archive. #xample)
% tar -tvz# #oo.tar.gz
displays the file names in the compressed archive 0oo.tar.gz while
% tar -"vz# #oo.tar.gz
extracts the files.
telnet
Use this command to log in to another machine from the machine you are currently
working on. For example, to log in to the machine $solitude$, do this)
% telnet solitude
See also) rsh.
"c
Use this command to count the number of characters, words, and lines in a file. Suppose,
for example, that we have a file dict with contents
red rojo
green verde
blue azul
1hite blanco
blac negro
!hen we can do this
% wc dict
% 10 %# tmp
!his shows that dict has ? lines, 45 words, and ?@ characters.
!he word count command has several options, as illustrated below)
% wc -l dict
% tmp
% wc -w dict
10 tmp
% wc -c dict
%# tmp
dummy
;nder construction