Unix Tutorial 2
Tree structure of the Unix Filesystem
-
all the stored information on a Unix computer is kept in
a filesystem
-
the place in the filesystem tree where you are located is
called the current working directory
-
some typical directories on a Unix file system:
-
/ - root directory, base of the filesystem's tree
structure
-
/bin - command binaries directory, includes executable
public programs that are part of the Unix operating system and its utilities
-
/dev - device directory, contains special files
for ttys, ptys, tape drives, and so on
-
/lib - contains library files for the C and other
programming languages
-
/lost+found - lost files directory, disk errors or
incorrect system shutdown may cause files to become "lost"
-
/usr - contains subdirectories for spooling, mail,
locally generated programs, executables for user commands, and other parts
of the Unix system
-
/usr/bin - command binary files and shell scripts
directory, similar to /bin
-
/usr/man - manual pages directory, contains the online
version of the Unix reference manuals
-
/usr/ucb - contains standard Unix commands originally
developed at the University of California, Berkeley
Some basic Unix commands for manipulating files
-
Listing files in a directory
-
ls -l : long listing
-
ls -a or ls -A : shows all files including
the hidden files
-
ls -F : shows the type of file by printing an extra
character after each filename
-
(nothing) - regular file
-
* - an executable
-
/ - a directory
-
@ - a symbolic link
-
= - socket
-
ls -R : lists all subdirectories, recursively
-
For example : ls -aF; ls -al
-
Moving around the file system
-
cd ~ : goes to the home directory
-
pushd : adds your directory to the directory stack
and moves you to the new directory
-
popd : takes the top directory off of the directory
stack and moves you to the directory underneath
-
For example:
-
pwd
-
pushd ~/pos
-
pushd
-
pushd
-
podd
-
File Access Permissions
-
when trying to access a file, you are either the file's owner,
a member of the file's group, or an "other"
-
3 bits determine whether you are allowed to read, write or
execute the file
-
a total of 9 mode bits to set the basic access permissions
-
For example:
-
-rw-------
is mode 600
-
-rwxrwxrwx is mode 777
-
-rw-rw-rw- is mode 666
-
chmod nnn filename : changes the file access permission
of filename according to nnn
Logging In and Logging Out
The 'dot' files in C shell
-
.login file is read when you start a login shell
-
.cshrc file is read any time a C shell starts
-
.logout file is read when you end a login shell
The login process
-
the login program starts a shell, e.g. the C shell
-
C shell reads .cshrc in the home directory
-
shell variables like cdpath, prompt and aliases
should be set here
-
other commands that should run every time you start a shell
-
C shell (as the login shell) will then read the .login
file next
-
search path, terminal type and environment variables
are
set here
-
other commands that you want to run whenever you log in
Shell and Environment variables
Environment variables
-
the shell need information like what kind of terminal you're
using, where any commands you want to use, what is your favourite editor,
and so on
-
Unix uses environment variables to store the above information
-
For example, the TERM environment variable tells programs
what kind of terminal you're using. Otherwise, you might have to
type commands like:
-
mail -editor vi -term aardvark48
-
environment variables are "inherited" by any program you
start, including another shell
-
Related commands :
-
setenv NAME value (e.g. setenv EDITOR /usr/bin/vi)
-
printenv
-
echo $NAME (e.g. echo $TERM)
Shell variables
-
shell variables are local to a particular instance of the
shell
-
environment variables are "global" variables, while shell
variables are "local" variables
-
shell variables have lowercase names
-
Related commands:
-
set name=value (e.g. set shell=/bin/csh)
-
set
-
echo $name (e.g. echo $shell)
More Unix commands
Aliases
-
"alias" facility lets you define abbreviations for commonly
used commands
-
for example:
-
alias is stored in .cshrc file
History
-
the history mechanism allows you to easily recall and repeat
past commands
-
to set C shell history, add "set history=n" in .cshrc
file,
where n=number of past commands you want to save
-
related commands:
-
history (e.g. history 10 will show the last
10 commands)
-
!! - repeats last command
-
!so - repeats last command that starts with so
-
!?fn? - repeats last command that has fn anywhere in it
-
!12 - executes command number 12
-
!:0 - selects only the command name; rather than the entire
command line
-
^xy^yx - shorthand substitution command
-
!!:s/xx/ab/ - another substitution command
Searching Text
-
grep - to look for strings matching a regular expression
and print only the lines found
-
use grep when you want to look at how a particular
word is used in one or more files
-
options commonly used:
-
-i : ignore distinction between upper & lower
case
-
-c : return only a count of the number of lines matched
-
-w : searches for the pattern "as a word"
-
-l : return only the name of file when grep finds
a match
-
-v : only prints out lines that don't match the search
pattern
On-line help
-
which - tells you which version of the program you
are using
-
where, whereis - locate the executable file, source
code and manual pages for a program
-
man - basis for Unix's online documentation
-
commonly used sections of the manual:
-
1 : user commands
-
2 : system calls
-
3 : library routines
-
manual pages:
-
NAME : the program's name
-
SYNOPSIS : how to invoke the program
-
DESCRIPTION : description of what the program does
-
OPTIONS : an explanation of each option
-
EXAMPLE : examples of how to use the program
-
ENVIRONMENT : environment variables that control the program's
behaviour