Professional Documents
Culture Documents
Chapter 2 UNIX System Commands
Chapter 2 UNIX System Commands
1
What is Unix?
2
Kernel (OS)
3
Structure of the UNIX system
Applications
There are many
Shell standard applications:
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
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
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
16
passwd
18
cal
19
clear
20
sleep
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
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
26
exit / logout
27
shutdown
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
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
38
script
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
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 u-r .
$ ls
ls: .: Permission denied
50
grep
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
55