UNIX IS CASE SENSITIVE - This means that it matters whether you type in lower case or upper case. Most commands are in lower case but you will find some special uses of capital letters.
When you connect to the UNIX system you will be asked for your login name and your password - you will be given these by the system administrator when you open your account.
In UNIX you work in a SHELL, which is like a layer in the system. There are parent and child shells. The shell is the program that interprets and executes the commands you type to it.
/bin/bash is the
shell running at TAFE which the students can use
(born again
shell)
csh (pronounced C shell) is the name of another shell.
USEFUL INFORMATION TO START WITH
man
manual (help)
press spacebar or 'f ' to
scroll down a page
arrow keys to go up or down a line
'y' to scroll back one line
'g' jump back to the start of the
file
'G' jump to the end of the file
man -h prints a one line help message on each switch you can use with man
man
intro
list of basic system commands
man
man
to find out about manual
man
ls
to find out about the command
ls
man -k queue The
"man" command will search for the
keyword
'queue' and
will display all UNIX commands that have that keyword in
their short description.
ls --help for more help on ls (use this syntax for any command; e.g. rm --help)
q
quit - use to exit many processes including the
manual
pwd shows you where you are - Print Working Directory
$
shows you are in parent
shell
sh
shell command - now in the child shell
^d
exit child shell - back to parent
(^d means Press Control key and d )
^d
to exit a process - kills shell (e.g. when using the command
'cat')
('cat' is explained in the next section)
^c
to exit a process - emergency stop (e.g. if system has
stopped)
logout
exit UNIX
exit
exit UNIX
ls
list
(same as dir in DOS)
ls
-a
list ALL files, including hidden ones
ls
-l
(or ls -long) long listing (permissions, owner, size,
when last modified)
ls
-t
list by time last modified
ls
-i
shows all files you have linked (same i-node)
ls
-R list
contents of subdirectories
ls -l /etc | grep passwd
- shows long listing of all files with the name passwd in /etc
ls | wc -w shows
how many files in this directory and counts the words
/
root directory
/etc
directory called 'etc'
~
home directory
cd
back to login directory
cd
~
back to your home (login) directory
cd ~dai go to
dai's home directory (if you have permission)
cd
..
up one level
cd
/
goes to root directory
cd /bin
takes you to the 'bin' directory
cal
calendar
date
gives time and date
cat
Concatenates or joins files and prints to the screen.
cat /etc/passwd -
gives a list of all users on system.
When used with a ">", it
can join or concatenate multiple files.
cat file1 file2 > file3
appends file2 to the end of file1 and saves it as
file3.
cat > myfile -
will copy nothing to myfile (deletes what is there), OR
cat > myfile -
like COPY CON in DOS, creates myfile and then you add content,
^d to finish.
cp copy
cp /pub/prac/red
~/red copy /pub/prac/red to a file
called red in my directory
mv rename or move a file or directory
rm
delete file - UNIX doesn't give you a second chance - delete means
delete...
rm -i gives you a chance to change
your mind - do you really want to delete
rmdir delete directory
rm -r
/chicken If a directory still
has files in it, you can get rid of
the files by using this command, which will remove all
files and directories in /chicken, and then will remove
the directory /chicken itself
Be very careful when using rm -r as it
removes everything in the directory
mkdir
make a new directory (can only do this in your own home
directory)
mkdir
myname
makes a new directory, myname
mv red
myname moves the file
red into the myname directory
mv myname green
changes the name of the myname directory to green
'
'
Use to enclose strings when commands contain characters UNIX
gives
other meanings to - e.g. $ ^
grep
Looks for patterns (print lines matching a
pattern).
A powerful search command.
grep
'^abe…$'
/etc/dic Shows all words starting
with (^) abe and 3 (…) other letters
$ is the terminator (can leave off $ - means finish at the end of
the line)
grep '^abe' /etc/dic -
shows all words starting with abe, any number of letters.
grep
'……………$'
/etc/dic > fifteen Send all
words with 15 or more letters (shown
by the fifteen dots) to a file called fifteen (leave
off the ^ to mean 15+)
grep $LOGNAME
/etc/passwd > myrecord
Searches for the word that is your login name
(LOGNAME) in the password file, and sends every line containing
that
word in the password file to a new file it creates, myrecord
(if myrecord
does not exist).
grep blue myfile This
searches the file myfile for the string 'blue' and displays
each line containing the word or phrase on your screen.
wc
word count (gives number of characters (bytes), words and
lines in a file)
wc
myfile
prints 3 numbers - the number of lines, words and characters in
myfile
wc -l
myfile number of lines in the
file 'myfile'
wc -w myfile
number of words in the file 'myfile'
wc -c
myfile number of characters in the
file 'myfile'
passwd
to change your password
PERMISSIONS - Unix is a multi-user
system, and so it needs to be very secure.
You must have permission to use files belonging to other users.
Following are the numbers and letters you can use in commands to change the mode of a file (chmod)
read =
4
owner/user - u
write =
2
group -
g
execute =
1
others -
o
all
- a
4, 2 & 1 are octal
digits (can add them up in order of owner, group, others
instead of writing u,
g, or o - see
chmod)
For a
file,
read means read or copy
write means write or change
execute means execute or run the program or shell
script
For a directory, read means the
user can use ls to obtain a listing
write means the user can delete files or add them
execute means they can
cd into that directory, but
cannot
make a listing unless they have read privileges as well
chown change ownership of a file or directory (only ones YOU own)
chown -R changes ownership of files in subdirectories as well
chown dai myfile this gives ownership of 'myfile' to dai
A better way of doing this is to give dai read privileges so
that he can copy it to his
own directory. With chown the file
is still kept in your original directory.
chmod change mode of a file - can give others read, write or execute privileges
chmod [ugoa]+-[rwx] filename - syntax for chmod command
+ adds
attributes to a file or directory
e.g. chmod +x red
- takes
attributes away from a file or directory e.g.
chmod -wx red
chmod +x red gives everyone execute privileges for red (could type chmod 111 red)
chmod a+r
*.pub
- This gives everyone (a)
permission (+) to read
(r) files
with the extension ". pub"
chmod u+rwx red gives user (owner) all privileges (read, write and execute) for red
chmod 666 passwd gives read (4) and write (2) privileges to all three groups for the passwd file
Example of long listing: -rw-r--r-- 1 root root 11563 May 14 11.19 passwd
'-
'
at beginning means it is an ordinary file
' d
' at
beginning would mean it is a directory
rw-
owner privileges (read, write, not execute)
r--
group privileges (read, not write, not execute)
r--
others privileges ( read only)
1
links (how many names the file appears under)
root
owner
root
group
11563 size of
file
May
14 date
11.19
time
passwd
filename
grep
'^…..$'
/etc/dic | rev | sort | rev > mystery -
finds all the 5 letter words
(^…..$)
in /etc/dic, arranges them by
the last letter (rev), i.e.
peace becomes
ecaep, then sorts them (sort) in
reverse order (rev) - word is written
right
way, but sorted by last letter, then second last, and so on, and
sends the
results to a file called mystery
|
pipe A
process. Directs output from one
process/program/command to
another process/program/command.
Can use the output of one command as input to the next
command.
ls -l | grep
'^d........x'
This command gives a long listing of all the directories (d)
in
the current directory for which other users (the x only
covers the last
grouping of three dots) have execute permission (x).
The expression
is quoted to prevent the shell interpreting the ^ metacharacter.
ls -l /dev |
more Shows a list of all terminals
being used, a page at a time - find your
terminal number (in the following example it is ttyp7) with
this command,
and can then type a command to stop other users writing to your
screen :
chmod 600
/dev/ttyp7 This makes your terminal
(/dev/ttyp7) read only for as long as you
are logged on (using the octal digits instead of r--r--r-- or
a-wx)
Note:
tty or ttyp means teletypwriter - it is now used to mean terminal
WRITING MESSAGES TO OTHER USERS
write username
e.g. write dai (whatever you
write appears on dai's screen)
^c
to go back to prompt (and stop writing messages…)
mesg
n to
stop messages coming in
mesg
y to
receive messages again
MORE COMPLEX COMMANDS & EXAMPLES
du
shows disk usage - size of files or directories - in kilobytes
du -b
red shows how many bytes in
the file 'red'
-a
all
-c
total
-l
count links
-s
summarise (doesn't break down into subdirectories)
-S
separate directories - count directory, not including the
size of sub-directories
uniq to identify duplicates
sort sorts files
. (dot) current directory
cp /etc/passwd . copies passwd file to current directory
sort mypass | uniq >
uniqpass takes out the
duplicate lines from mypass and
copies the file without the duplicates into uniqpass
sort mypass | uniq -d >
duplicpass puts the duplicate lines into the
file duplicpass
sort .bash_history | wc -l shows how many lines are in your history file
tr
translates, e.g. can translate lower case to upper case
tr a-z A-Z < /etc/group >
groups - copies the file /etc/group to groups
in the current
directory and capitalizes the contents
cut
cuts out a section you specify from a file
cut -c 1-4
/etc/passwd > four - creates a file
called four which has a list of the first
four letters of each entry in /etc/passwd
grep 100 /etc/passwd | cut -f
5 -d
:
| sort finds all entries in the password
file with
100 anywhere on the line, sorts alphabetically by field 5 (user
name)
grep finds a pattern
100 the pattern is any line with 100 in it
/etc/passwd the name of the file you are searching
| cut send the results to cut
-f cut looks for a field
5 field number 5
-d delimiter - the next character shows where a field starts
: this shows how to tell where a field starts
| sort sends the output to sort, which sorts alphabetically or numerically
more
1. use in front of the file name you want to view - shows
you the contents
of a file, a page at a time (very useful if you want to view a
long file)
2. use with | (pipe)
after a command (as in DOS)
while viewing a file with more can move to the next page by
using:
spacebar or f to scroll a
page at a time
<enter>
to scroll results a line at a time
q
to quit
man
more
get help on using 'more'. Can also get help by pressing
'?'
from within a 'more' screen
sort /etc/passwd |
more Shows all entries in passwd
file, a page at a time
more myfile Displays the contents of "myfile" a page at a time
more myfile red
blue
Displays the contents of each of
these three files, one after the other,
a page at a time
"more" doesn't let you go backwards; see the command "less"
less
is an advanced version of more. In addition to the commands
given for 'more' less adds:
y
To scroll back one line.
g Jump back to the start of the file.
G Jump to the end of the file.
Both more and less offer the ability to search for words in the file, using '/ '
If you want to scroll forwards to some particular word, type a forward slash (/) followed by the word you want; for example, to search for the word "computer" while viewing a document with more or less, type /computer and <enter>
head
filename Lists the first 10
lines of a file
head -25 myfiles
Shows the first 25 lines of
myfile
tail
filename
Lists the last 10 lines of a file
tail -5
myfile Lists the
last 5 lines of the file 'myfile'
tail +25
myfile All but the last 25 lines
will be displayed of myfile
touch
change the file time stamps to the current time
touch red changes date
and time of 'red' file to current date and time
touch red -d 06:00
changes date of 'red' to current, and time to 6.00
touch 06081200 red changes the
date of red to Jun 8 and the time to 12.00
-m change modification time only
-a change access time only
-t use the argument
MMDDhhmm[[CC]YY][.ss] instead of current time
( )
or
'
'
can give more than one command on a line, inside
brackets
or quote signs separated by a semi-colon
(sleep 10; date)
& Using the
& makes the commands inside the
brackets run as a
background process, so you get the $ command prompt back
straight
away (after 10 seconds the date will come on the screen)
[1] 5541 (can be any number) will appear after this command
- this is
the process identification number (PID):
kill 5541 this will kill the process, and you go back to the prompt.
ps
shows which processes are running for you (all windows, the
desktop
menu, the clock, etc).
ps
-a
shows others processes as well as your own
ps
-u
shows the user name of the processes that are running
ps
-x
shows process status of every process which belongs to the
operating
system (daemons)
ps -ef | grep
username The result will be all the
processes running for username.
For example, for user dai, the numbers 5577, 5544, etc,
are process identification numbers (PID):
dai 5577
5576 2 18:23:14 pts/2 0:00 -csh
dai
5544 1 0 18:19:54
? 0:00
/usr/bin/X11/xterm -geom 80x24+17-17
dai
5543 1 0 18:19:54
? 0:01
/usr/bin/X11/toolchest -name ToolChest
dai
5590 1 0 18:23:26
? 10:36
/tools/bin/netscape3.0
dai 5884
5559 0 21:27:37 pts/1 0:02 nedit
lab5_96.html
ln
gives two paths to the same program, e.g. ln ksh sh (can
call it either
ksh or sh)
Aliases are abbreviations for commands or sets of commands. This feature is particularly useful to execute commands or command sequences that you use frequently. In this way you can avoid retyping a command sequence many times during a login session.
alias To see the aliases already being used
alias h = 'history' Entering h will execute the history command
alias l = 'ls -al' Entering 'l' will show a long listing of all files, including hidden files
unalias h
} To delete the
previous alias definitions
unalias l
}
w
}
who
} shows logged in users (login
name, terminal line, login time, remote hostname)
rusers
}
who
-c
shows whether users allow messages or
not
(+
or
-
after name)
/proc a file that has process information for currently logged on users
/etc/utmp temporary user file in the etc directory - information on current users
uptime shows number of users logged on, and how long the system has been running
finger
Getting more information about other users. The finger command
displays
information about a specific user. For instance, entering:
finger joe lists
information available on any user whose username is joe
finger
-s includes write
status as well (if the user will receive messages)
finger
-l gives more
information, such as phone number and user's home directory
set | more shows environment variables (initialized when the system is started)
User defined variables
Example 1
mydog = rover
echo $mydog
rover When shell sees $name it performs variable substitution.
Example 2
mycat = "big black moggy" (need " " for a string)
echo $mycat
big black moggy (appears on screen)
sh (change to child shell)
echo $mycat (child shell - my cat does not exist there, so no response)
exit (change back to parent shell)
echo $mycat
big black moggy (now in parent shell - it shows the variable on the screen)Parent and child shells do not know each other's variables
CHANGING THE LOOK OF YOUR SHELL
To change prompt, type in PS1="Yes, Veronica>" (whatever you want in the " ")
To change it permanently, every time you login, need to change the .profile file
.profile customises the look of your login shell (it is a hidden file).
To edit your .profile file, type
vi
.profile (see how to
use vi later in these notes)
PS1 = "Yes, $LOGNAME>"
(if you want your login name - or anything else you want
there)
echo Welcome to the UNIX
system
who
users = `who | wc
-l`
(` is on the tilde (~) key. You must use these
parameters to enclose command - the result of these two commands
is a
listing of who is logged on (who) counted by number of lines (wc
-l).
UNIX knows that users is a variable,
because it is followed by = and a string
echo There are $users users logged
on ($ before the variable name tells UNIX
to replace the variable with the string following the = sign.
date
The result of this .profile gives you a welcome screen that says,
Welcome to the UNIX system
(shows a list of who is logged
on)
There are (9) users logged
on
Today is (Tuesday 17th June)
Yes, Veronica>
On the other hand, if you want no messages when you log in, you
can ask for a quiet login:
>
.hushlogin makes a file called
.hushlogin in your directory (you don't need to put anything
into the file) .hushlogin gives a quiet login; no
messages, and just the default
prompt.
There are 3 predefined data streams in UNIX
You can attach these data streams to other files or devices - this is called redirection.
who >
who.out
sends the output of the who command to a file, who.out
sort <
who.out
takes input from who.out and sorts contents on screen
(doesn't change the file)
sort <
who.out
>
who.sort
as above, but sends it to a new file instead, who.sort
UNIX treats all devices as part of the file system. They
all live in /dev
e.g. /dev/fd0
(floppy disk drive
0)
/dev/hda1 (hard disk drive 1)
ls -l /dev to get a
listing of all devices - the first letter in the listing is b
(block) or
c (character)
A keyboard is character oriented (deals with a character at
a time)
FDD and HDD are block oriented (deal with chunks of data)
crw-rw-rw- 1 root 0, 0 Sep 27
23:09 console
crw-rw-rw- 1 bob 1, 0 Sep 28
02:30 tty0
crw-rw-rw- 1 root 3, 2 Sep 27
23:15 null ( where 3 = major device number and
2 = minor device number )
/dev/null
is a bit bucket - a black hole (data goes in but never comes
out…)
find /home -name
myfile -print 2> /dev/null
There will be masses of error messages
because find will search even directories which you don't have
executable
rights over. You will now see only the
listings of myfile on the screen.
find find
/home start search from the /home directory downwards
-name the next word is the name of the file I want to find
myfile name of file I want
-print show it on the screen
2> redirects errors somewhere else.. (to screen by default…)
/dev/null send the error messages to the black hole
find /home -name
myfile -print 2>
err.log
same command, except that the error
messages are sent to a file, err.log
WILD CARDS
* matches any character any number of times
? matches a single character
[a-f] match any character in range a - f
cp /etc/*
.
copy all files in /etc directory to current directory
ls
b*
list all files starting with b
rm
c????
remove all files starting with c and having 5 letters
rm d [aeiou]* remove all files
starting with d and having a,e,i,o,u for the second letter
ls
d[^g-o]* list all files
starting with d, but NOT the ones with g to o as second letter
SHELL SCRIPTS (like batch files in DOS)
script
records all interactions in a window and saves in a file.
script
nu
write your script to a file called nu. To stop
recording, use Ctrl-D.
Alternative way to make shell scripts:
cat >
nu
makes new file called 'nu' (like copy con in DOS)
who | wc -l
input into nu
^D
end of input - closes file
running the shell script:
when you type nu it runs the
command who | wc -l
and gives the result 9 (if there are 9 users logged
on)
vi passwd opens the passwd file for editing. Type a filename after vi or it opens an empty file.
man ls >
ls.out
copies the manual for ls to a file called ls.out
vi
ls.out
can then edit the manual for ls in your file
a append - inserts text after the cursor } automatically puts you in edit mode -
i insert - inserts text before the cursor} whatever you type now will be put in
o open - inserts text in the next line } your file
<ESC> puts you in command mode
h move one place left (9h moves 9 characters to the left)
l move one place right (9l " " right)
j move one place up (9j " " up)
k move one place down (9k " " down)
Arrow keys can also be used, but only in command modew forward one word
b back one word
e end of the current word
$ moves to end of line
0 or | moves to beginning of line
G moves to end of file
nG moves to line n; e.g. 12G jumps to line 12
^ first non-whitespace character in the line
{ back one paragraph
} forward one paragraph
t to (e.g. tx places the cursor a character before the next x on the same line)
/word moves to the next word after the /
(e.g. /And moves to the next And in the document)r replace character under cursor
x delete character under cursor
dw delete a word - e.g. 5dw deletes next five words
dd delete a line - e.g. 9dd deletes next 9 lines
d$ deletes to the end of the line
dG delete to the end of the file
d | delete to the beginning of the line
d2G delete to the second line of the file
d{ delete to the beginning of the paragraph
dtx deletes from the cursor to the next xcw changes the current word (like highlighting the word and typing over it)
c4w changes the next four words
c} changes the rest of the current paragraph
cc changes the current lineu undo the last change to a line
U undo all changes to the line - restore to original state
<ESC>
:
wq write file and quit
<ESC>
:
q! quits without
saving
<ESC>
: w
newfilename substitute your file
name for newfilename if you have
opened vi without using a
name for your file
history lists previous commands by number.
After you have found the command number you want to repeat, type:
!n executes the nth command
!! executes the last command
Can also use the up and down arrows to see previous commands,
and press enter to execute it again (as in DOS using doskey)
All invisible files have names that start with a "
. " (dot) and are also
referred to as dot files. They are called invisible because they
are not normally displayed in a directory listing. In other words
when you issue the command ls in a
UNIX shell, dot files are not displayed. To see all of your files
use the -a switch:
ls -a
There are several important default hidden files in your home
directory: the
. (dot),
..
(dot dot),
.login,
.cshrc,
.user,
.profile
The system uses the
.
and .. files to connect
to the current directory (.) and its parent directory
(..)
The
.login
file contains commands that UNIX will execute every time you login
to your account.
There are some standard names for UNIX directories
and subdirectories based on
the purpose of the directory.
/bin -
normally contain executable programs
/man - manual pages are
stored in man directories
/doc -
miscellaneous documentation is frequently found in a doc
directory
/lib -
the lib directories hold libraries of various procedures and
functions that
can be used when writing a program, such as special constant,
function,
and procedure declarations for use in C programs
/src -
directories for storing source code are usually named src
/tmp - directories
named tmp are usually for temporary storage of files