Chapter 2 : UNIX Commands
1
What is Unix?
A multi-user networked operating system
» “Operating System”
– Handles files, running other programs, input/output
– Looks like DOS…but more powerful
– The internet was designed on it, thus networking is an
intrinsic part of the system
» “Multi-user”
– Every user has different settings and permissions
– Multiple users can be logged in simultaneously
2
Kernel (OS)
Interacts directly with the hardware
through device drivers
Provides sets of services to programs,
insulating these programs from the
underlying hardware
Manages memory, controls access, maintains
file system, handles interrupts, allocates
resources of the computer
Programs interact with the kernel
through system calls
3
Structure of the UNIX system
Applications
There are many
Shell standard applications:
Kernel\ (OS) • file system
commands
Hardware • text editors
• compilers
• text processing
4
Unix and Users
Most flavors of Unix (there are many)
provide the same set of applications
to support humans (commands and
shells).
Although these user interface programs
are not part of the OS directly, they are
standardized enough that learning your
way around one flavor of Unix is enough.
5
Flavors of Unix
There are many versions of Unix that
are used by lots of people:
SysV (from AT&T)
BSD (from Berkeley)
Solaris (Sun)
IRIX (SGI)
AIX (IBM)
LINUX (free software)
6
Unix vs. Linux
Age
» Unix: born in 1970 at AT&T/Bell Labs
» Linux: born in 1992 in Helsinki, Finland
Sun, IBM, HP are the 3 largest vendors of Unix
» These Unix flavors all run on custom hardware
Linux is FREE!
» Linux was written for Intel/x86, but runs on many platforms
7
UNIX Shell
A system program that allows a user to execute:
» shell functions (internal commands)
» other programs (external commands)
» shell scripts
Linux/UNIX has a bunch of them, the most common
are
» tcsh, an expanded version of csh (Bill Joy, Berkley, Sun)
» bash, one of the most popular and rich in functionality
shells, an expansion of sh (AT&T Bell Labs)
» ksh, Korn Shell
» zhs
» ...
8
Command Format
Format: command name and 0 or more arguments:
% commandname [arg1] ... [argN]
% sign – represents prompt.
Arguments can be
» options (switches to the command to indicate a mode of
operation) ; usually prefixed with a hyphen (-) or two (--) in GNU
style
» non-options, or operands, basically the data to work with (actual
data, or a file name)
9
Shell I/O
Shell is a “power-user” interface, so the user interacts
with the shell by typing in the commands.
The shell interprets the commands, that may produce
some results, they go back to the user and the control is
given back to the user when a command completes
(in general).
In the case of external commands, shell executes actual
programs that may call functions of the OS kernel.
These system commands are often wrapped around a
so-called system calls, to ask the kernel to perform an
operation (usually privileged) on your behalf.
10
Command I/O
Input to shell:
» Command name and arguments typed by the user
Input to a command:
» Keyboard, file, or other commands
Standard input: keyboard.
Standard output: screen.
These STDIN and STDOUT are often together referred to as a
terminal.
Both standard input and standard output can be redirected
from/to a file or other command.
File redirection:
» < input
» > output
» >> output append
11
Common Commands
12
man
Manual Pages
The first command to remember
Contains info about almost everything :-
» other commands
» system calls
» c/library functions
» other utils, applications, configuration files
To read about man itself type:
% man man
NOTE: unfortunately there’s no
% man woman ... 13
which
Displays a path name of a command.
Searches a path environmental variable for
the command and displays the absolute path.
To find which tcsh and bash are actually in
use, type:
% which tcsh
% which bash
% man which for more details
14
chsh
Change Login Shell
Login shell is the shell that interprets commands
after you logged in by default.
You can change it with chsh (provided that your
system admin allowed you to do so).
To list all possible shells, depending on
implementation:
% chsh -l
% cat /etc/shells
% chsh with no arguments will prompt you for the
shell.
15
whereis
Display all locations of a command (or some other
binary, man page, or a source file).
Searchers all directories to find commands that
match whereis’ argument
% whereis tcsh
16
passwd
Change your login password.
A very good idea after you got a new one.
It’s usually a paranoid program asking your
password to have at least 6 chars in the
password, at least two alphabetical and one
numerical characters. Some other restrictions (e.g.
dictionary words or previous password similarity) may
apply.
Depending on a privilege, one can change user’s
and group passwords as well as real name, login
shell, etc.
% man passwd 17
date
Displays dates in various formats
% date
% date -u
» in GMT
% man date
18
cal
Calendar % cal current month
% cal 2 2000 Feb 2000, leap
» for month year
» entire year % cal 2 2100 not a leap year
Years range: 1 - % cal 2 2400 leap year
% cal 0 error
9999
% cal 2002 whole year
No year 0
19
clear
Clears the screen
There’s an alias for it: Ctrl+L
Example sequence:
» % cal
» % clear
» % cal
» Ctrl+L
20
sleep
“Sleeping” is doing nothing for some time.
Usually used for delays in shell scripts.
% sleep 2 2 seconds pause
21
Command Grouping
Semicolon: “;”
Often grouping acts as if it were a single
command, so an output of different commands
can be redirected to a file:
% (date; cal; date) > out.txt
22
alias
Defined a new name for a command
% alias
» with no arguments lists currently active aliases
% alias newcommand oldcommand
» defines a newcommand
% alias cl cal 2003
% cl
23
unalias
Removes alias
Requires an argument.
% unalias cl
24
history
Display a history of % !n
recently used » repeat command n in the
commands history
% history % !-1
» all commands in the » repeat last command =
history !!
% history 10 % !-2
» last 10 » repeat second last
command
% history -r 10
% !ca
» reverse order
» repeat last command that
% !! begins with ‘ca’
» repeat last command
25
apropos
Search man pages % apropos date
for a substring. % man -k date
% apropos word % apropos password
Equivalent:
% man -k word
26
exit / logout
Exit from your login session.
% exit
% logout
27
shutdown
Causes system to shutdown or reboot cleanly.
May require superuser privileges
% shutdown -h now - stop
% shutdown -r now - reboot
28
Directories and Files
29
Directories
Files are grouped together in other files called directories -
analogous to folders in Windows
Directory paths are separated by a forward slash: /
» Example: /homes/iws/robh/classes/cse326
The hierarchical structure of directories (the directory tree)
begins at a special directory called the root, or /
» Absolute paths start at / (root directory)
– Example: /homes/iws/robh/classes/cse326
» Relative paths start in the current directory
– Example: classes/cse326
Your home directory “~” is where your personal files are
located, and where you start when you log in.
» Example: /homes/iws/robh
30
Directories (cont’d)
Handy directories to know: ~ Your home directory, .. The parent
directory, . The current directory
ls
» LiSts the contents of a specified files or directories (or the current
directory if no files are specified)
» Syntax: ls [<args> … ]
» Example: ls backups/
» Has whole bunch of options, e.g
– % ls -a : all files except those starting with a “.”
pwd
» personal working directory - displays Working Directory
find
» Looks up a file in a directory tree.
» Syntax: find . -name name
» Example: find . \(-name ‘w*’ -or -name ‘W*’ \) 31
Directories (cont’d further)
cd
» Change current Directory to a new one – absolute or relative
» Syntax: cd <directory>
» Examples:
– cd backups/unix-tutorial
– cd ../class-notes
– cd returns to home directory
mkdir
» MaKe DIRectory - Creates a directory
» Syntax: mkdir <directories>
» Example: mkdir backups class-notes
rmdir
» ReMove DIRectory, which must be empty first
» Syntax: rmdir <directories>
» Example: rmdir backups class-notes 32
Files
Unix file types (e.g. “executable files, ” “data files,” “text
files”) are not determined by file extension (e.g.
“foo.exe”, “foo.dat”, “foo.txt”) – unlike in Windows
Thus, the file-manipulation commands are few and
simple …
Many use only 2 letters
rm
» ReMoves a file, without a possibility of “undelete!”
» Syntax: rm <file(s)>
» Example: rm tutorial.txt backups/old.txt
33
Files (cont’d)
cp
» CoPies a file / directory, preserving the original
» Syntax: cp [options] <sources> <destination>
» Example: cp tutorial.txt tutorial.txt.bak
» Useful option: -i to prevent overwriting existing files and
prompt the user to confirm.
mv
» MoVes or renames a file / directo, destroying the original
» Syntax: mv <sources> <destination>
» Examples:
– mv tutorial.txt tutorial.txt.bak
– mv tutorial.txt tutorial-slides.ppt backups/ 34
cat
Display and concatenate files.
% cat
» Will read from STDIN and print to STDOT every line you
enter.
% cat file1 [file2] ...
» Will concatenate all files in one and print them to STDOUT
% cat > filename
» Will take whatever you type from STDIN and will put it into
the file filename
To exit cat or cat > filename type Ctrl+D to
indicate EOF (End of File).
35
more / less
Pagers to display contents of large files page by
page or scroll line by line up and down.
Have a lot of viewing options and search capability.
Interactive. To exit: ‘q’
36
less
less ("less is more") a bit more smart than the more
command
to display contents of a file:
» % less filename
To display line numbers:
» % less -N filename
To display a prompt:
» % less -P"Press 'q' to quit" filename
Combine the two:
» % less -NP"Blah-blah-blah" filename
For more information:
» % man less
37
touch
By touching a file you either create it if it did not
exists (with 0 length).
Or you update it’s last modification and access
times (update the time stamps of the files listed on
the command line to the current time)
There are options to override the default behavior.
% touch file
% man touch
38
script
Writes a log (a typescript) of whatever
happened in the terminal to a file.
% script [file]
% script
» all log is saved into a file named typescript
% script file
» all log is saved into a file named file
To exit logging, type:
» % exit
39
File attributes
Every file has some attributes:
Access Times:
when the file was created
when the file was last changed
when the file was last read
Size
Owners (user and group)
Permissions
Type – directory, link, regular file, etc.
ACLs – access control lists (not today)
40
File Time Attributes
Time Attributes:
when the file was last changed ls -l
sort by modification time ls -lt
41
File Owners
Each file is owned by a user.
You can find out the username of the
file's owner with the -l or -o option to
ls:
$ ls -l foo
-rw-rw---- 1 hollingd grads 13 Jan 10 23:05 foo
size
permissions name
owner group
time 42
File Permissions
Each file has a set of permissions that control
who can mess with the file.
There are three types of permissions:
read abbreviated r
write abbreviated w
execute abbreviated x
There are 3 sets of permissions:
1. user
2. group
3. other (the world, everybody else)
43
ls -l and permissions
-rwxrwxrwx
User Group Others
Type of file:
- – plain file
d – directory
s – symbolic link
(others)
44
rwx
Files:
r - allowed to read.
w - allowed to write
x - allowed to execute
Directories:
r - allowed to see the names of the
contents
w - allowed to add and remove files.
x - allowed to “enter” the directory
45
Changing Permissions
chmod - changes the permissions
associated with a file or directory.
The simplest chmod looks like this:
chmod mode file
46
chmod – numeric modes
Consider permission for each set of users
(user, group, other) as a 3-bit no.
r–4
w–2
x–1
A permission (mode) for all 3 classes is a 3-
digit octal no.
755 – rwxr-xr-x
644 – rw-r—r--
700 – rwx------
47
chmod - examples
$ chmod 700 CS571
$ ls –o Personal
drwx------ 10 kschmidt 4096 Dec 19 2004 CS571/
$ chmod 755 public_html
$ chmod 644 public_html/index.html
$ ls –ao public_html
drwxr-xr-x 16 kschmidt 4096 Jan 8 10:15 .
drwx--x--x 92 kschmidt 8192 Jan 8 13:36 ..
-rw-r--r-- 5 kschmidt 151 Nov 16 19:18 index.html
$ chmod 644 .plan
$ ls –o .plan
-rw-r--r-- 5 kschmidt 151 Nov 16 19:18 .plan
48
chmod – symbolic modes
Can be used to set, add, or remove
permissions
Mode has the following form:
[ugoa][+-=][rwx]
u – user g – group o – other a – all
+ add permission - remove permission =
set permission
49
chmod examples
$ ls -al foo
-rwxrwx--x 1 hollingd grads foo
$ chmod g-wx foo
$ ls -al foo
-rwxr----x 1 hollingd grads foo
$ chmod u-r .
$ ls
ls: .: Permission denied
50
grep
l Searches its input for a pattern.
l The pattern can be a simple substring or a complex
regular expression.
l If a line matches, it’s directed to STDOUT; otherwise,
it’s discarded.
l % echo “blah-foo” | grep blah
» Will print the matching line
l % echo “blah-foo” | grep zee
» Will not.
51
Shell metacharacters
Some characters have special meaning
to the shell. These are just a few:
I/O redirection
< > |
wildcards
* ? [ ]
others
& ; $ ! \ ( ) space tab newline
These must be escaped or quoted to
inhibit special behavior
52
Wildcards
* – matches 0 or more characters
? – matches exactly 1 character
[<list>] – matches any single character in
<list>
E.g.
ls *.cc – list all C++ source files in directory
ls a* – list all files that start w/’a’
ls a*.jpeg – list all JPEGs that start w/’a’
ls * - (make sure you have a subdirectory, and try it)
53
Wildcards (more examples)
ls file?
- matches file1, file2, but not
file nor file22
ls file?.*.DEL
- matches file1.h.DEL,
file9.cc.DEL, file3..DEL but not
file8.DEL nor file.html.DEL
These are not regular expressions!
54
Wildcards - classes
[abc] matches any of the enclosed characters
ls T[eE][sS][tT].doc
[a-z] matches any character in a range
ls [a-zA-Z]*
[!abc…] matches any character except those
listed.
ls [!0-9]*
55