Introductory Note 201 /linux Shell Commands: Cardiff University Cardiff School of Computer Science
Introductory Note 201 /linux Shell Commands: Cardiff University Cardiff School of Computer Science
Cardiff University
Cardiff School of Computer Science
Prifysgol Caerdydd
Ysgol Cyfrifiadureg Caerdydd
Robert Evans
Copyright
2003,
c 2004 R.Evans.
Email: Robert.Evans@cs.cardiff.ac.uk
Abstract
The Shell is the command interpreter on UNIX and Linux systems. This Note in-
toduces some of the basic features of the Shell and lists many of the commands or
programs available on the UNIX and Linux computers in Cardiff School of Computer
Science.
Contents
1 The Shell 1
2 Command Summary 6
2.1 Logging out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 Commands for accessing floppy disks . . . . . . . . . . . . . . . 6
2.3 File Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Manipulating data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Manipulating data (cont’d) . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Compressed files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.7 Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.8 Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.9 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.10 Messages between Users . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.11 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.12 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.12.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Computer Science: Introductory Note 201 (2004/05)
2.12.2 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.12.3 C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.12.4 JAVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.12.5 FORTRAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.12.6 Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.12.7 Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.13 Text Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.13.1 General Commands . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.13.2 Troff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.13.3 TeX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.14 Word Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.15 Database Management . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1 The Shell
The UNIX and Linux command interpreter or shell is the program users interact with
in a terminal emulation window. The terminal emulation window can be one in the
workstation’s Graphical User Interface - gnome-terminal on Linux or dtterm on
Solaris. Alternatively, it can be an application such as telnet, secure shell client or
PuTty on a Windows PC that’s logged into Linux or UNIX over the network.
The shell used in the School of Computer Science is bash - Bourne Again Shell.
There are other shells available such as the Bourne Shell, the C-Shell and the TC-
Shell, and you can choose to use a different shell if you prefer. They all have similar
characteristics but each has its own particular features. This Note assumes you are
using bash.
Bash has the following features:
• a command prompt which may be configured by the user. The default prompt is
bash-2.05$
- a dollar symbol preceded by “bash” and the bash program’s version number.
• the shell, like other programs on UNIX and Linux has an associated current di-
rectory. Programs running on UNIX and Linux use the current directory as the
starting point when locating files. The shell command
bash-2.05$ cd
is used to change the current directory to a different location in the UNIX or Linux
file system.
• commands are invoked by naming them. Most UNIX and Linux commands are
simply programs which are executed by the shell. For example,
bash-2.05$ ls
runs the program ls which reads the current directory and lists the names of its
files.
• When you type a command name, the shell will check to see if the command is
built-in and will otherwise search a set of directories until it finds the program.
This set is known as the search path.
Information for Users: Introductory Note 201 (2004/05) 3
The search path includes the current directory, your home directory and its sub-
directory “linuxbin” or “solarisbin” (depending on whether you are logged in to
Linux or Solaris). You can write your own programs and invoke them simply by
typing their names. If you store such a program in the directory “linuxbin” or
“solarisbin” as appropriate, it will be found and run no matter what your current
directory is.
• commands often have argument strings which may, for instance, represent file-
names. E.g.
bash-2.05b$ cd ˜/linuxbin
change the current directory to “linuxbin” in your home directory. (The tilde char-
acter ˜ means your home directory to the shell.
Some commands need more than one argument. E.g.
is the copy command cp with two filename arguments; “fileA” is copied to a new
file, “fileB”.
Some commands have flag or option argument strings usually beginning with “-”
or “–”. The flags modify the behaviour of the program being invoked:
bash-2.05b$ ls -lt
• the shell will expand wildcards to match filenames in the current directory. For
example,
bash-2.05b$ ls -l *.c
will give a directory listing of the files with names “anything.c” (conventionally C
program source files).
• most UNIX and Linux commands and programs adhere to a concept of standard
input and standard output. The standard input is a stream of data which the
program reads and the standard output is a stream of output written by the pro-
gram. Often these are both attached to the terminal so that input comes from
your keyboard and output goes to your screen. The shell lets you redirect the
standard input and output.
The symbol “<” is used to redirect standard input from a file and the symbol “>”
is used to redirect standard output to a file. For example:
makes cat reads from file “fileA”. It outputs the file’s contents to the terminal or
screen.
• the Shell has the facility to pipe the output of one program to the input of another.
The pipe symbol is “|”. For example:
bash-2.05b$ ls | wc -w
pipes the output of ls (viz., your filenames) into the word count program wc.
The “-w” flag tells wc to count the number of words in its input. So the above
command counts the number of files in your directory.
4 Computer Science: Introductory Note 201 (2004/05)
fg %2
bring job number 2 into the foreground while
kill %1
terminates job number 1.
The command
bash-2.05b$ jobs
lists the status of all stopped or background jobs along with the job number
(1,2,3...).
If a backgound job needs terminal input, it will stop until you bring it into the
foreground.
• the shell has a history mechanism - it remembers the last few commands.
bash-2.05b$ history
lists the remembered commands along with a reference number, e.g:
515 cd .Intro
516 ls -ltd 302*
517 latex 302
518 latex 302
519 dvips -Ppdf -G0 -ta4 302
520 ps2pdf 302.ps
521 acoread 302.pdf
In a workstation’s terminal emulation windows, you can cut and paste from the
history to rerun a command. You can also use the symbol “!” to rerun any
command from the history:
bash-2.05b$ !518
reruns command number “518” from the history.
bash-2.05b$ !ps
reruns the last command starting “ps...”.
bash-2.05b$ !!
See the manual page on bash for more details (type man bash).
Bash has an additional mechanism which allows you to recall and edit previous com-
mands using the keyboard up-arrow key ↑ . If you press up-arrow, the last command
re-appears on the terminal. Press up-arrow again to get earlier commands. To rerun
the command, press RETURN . To amend the command before rerunning it, use the
delete key to remove characters from the end or use the back-arrow ← to reposition
the cursor to delete or insert characters within the command.
6 Computer Science: Introductory Note 201 (2004/05)
join - join files on some com- These commands list or alter information
mon field about the system.
look - find lines in sorted data chfn - change your finger entry
perl - data manipulation lan- date - print the date
guage determin - automatically find termi-
paste - merge file data nal type
sed - stream text editor du - print amount of disk us-
sort - sort file data age
split - split file into smaller files finger - print information about
tr - translate characters logged-in users
uniq - report repeated lines in a groups - show group memberships
file homequota- show quota and file usage
wc - count words, lines, and iostat - report I/O statistics
characters kill - send a signal to a process
last - show last logins of users
lun - list user names or login ID
netstat - show network status
2.6 Compressed files passwd - change your login pass-
word
printenv - display value of a shell
Files may be compressed to save space. variable
Compressed files can be created and ex- ps - print process status
amined. statistics
compress - compress files quota -v - display disk usage and
uncompress - uncompress files limits
zcat - cat a compressed file reset - reset terminal mode
zcmp, - compare compressed script - keep script of terminal
zdiff files session
zmore - file perusal filter for crt stty - set terminal options
viewing of compressed text time - time a command
gzip - GNU alternative com- tset - set terminal mode
pression method tty - print current terminal
gunzip - uncompress gzipped files name
uptime - display system status
users - print names of logged in
users
2.7 Information vmstat - report virtual memory
statistics
Manuals and documentation are available w - show what logged in
on-line. Go to our web site www.cs.cf.ac.uk/systems users are doing
for web-based documentation. The fol- who - list logged in users
lowing Shell commands give information.
answerbook2 - invoke netscape to
view for Sun documen-
tation
apropos - locate commands by
keyword lookup
dthelpview - Sun CDE Worksta-
tion help viewer
man - displays manual
pages online
info - displays command in-
formation pages online
yelp - GNOME help viewer
8 Computer Science: Introductory Note 201 (2004/05)
CC - C++ compiler for Suns Troff is the standard UNIX text format-
SPARC systems ter. TeX is also available for documents
workshop - SPARCStation develop- intended for a LaserPrinter.
ment environment
g++ GNU C++ Compiler
2.13.2 Troff
2.13.3 TeX