Você está na página 1de 22

COMP 121-401: UNIX Programming

Files and File


Systems

Andrew Nashel

nashel@cs.unc.edu
Department of Computer Science

January 12, 2004

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Course topics
• What is UNIX?
• Using UNIX systems
• Programming in UNIX
• Files and file systems
• Shells and I/O
• Editors and Printing
• System tools

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Course topics
• Files and file systems
♦File system structure
♦Navigating, creating and deleting…
♦Unix permissions
♦AFS
♦Example: Creating a web page

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Unix filesystem
structure
The filesystem is a heirarchical
structure resembling a tree,
anchored at the root (“/”):
/

bin usr etc users lib

bin local games include lib

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Unix filesystem
structure
Every item in the filesystem is a
file or directory (or a link).

Directories can contain file and


other directories (children).

Each directory may have only one


parent.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Unix filesystem
structure
To describe an object in the file
system you specify a path.
Paths are either absolute, refering
to the root:
/usr/local/bin/howto
Or they are relative to the current
directory:
../stuff/morestuff/afile

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Navigating…
cd <dir> change to <dir>
cd .. change to parent dir
pwd print current directory
ls list directory contents
ls -l long listing
ls -a list all files
ls -al long listing, all files

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Creating and deleting…
cp copy file or
directory
mv move file or
directory
mkdir create a directory
rmdir delete a directory
rm delete a file
rm -r delete recursively
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Unix permissions
Each file and directory has permissions that
support access control.

Permissions are defined as:


read (‘r’) – view contents
write (‘w’) – change contents
execute (‘x’) – run the file or change to the
directory

Permissions are defined in three sets, for the


owner, group, and all others. Directories must
be executable to be accessed.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Viewing permissions
$ ls –l /etc/passwd
-rw-r--r-- 1 root root 1369 Apr 30 2003 /etc/passwd
$ ls –l /bin/ls
-rwxr-xr-x 1 root root 46888 Jan 19 2003 /bin/ls*

owner group size creation date

permissions
1st character: type, ‘-’ for file, ‘d’ directory, ‘s’ special, ‘l’ link
Next three are read, write, execute for owner
Then, next three for group, and next three for all others.

Character means permission granted, ‘-’ means denied.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Examples – Files
-rw-------
Only owner can read and write

-rwxrwxrwx
Everyone can read, write and execute
(and delete) (not very common!)

-rw-r--r--
Everyone can read, but only owner can
write
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Examples – Directories
drwx------
Only owner can view, cd into, and
delete

drwxrwx---
Owner and group can view, write, etc.

drwxr-xr-x
Everyone can cd into but only owner
can
modify The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Changing permissions
$ chmod [ugoa(+/-)rwx] <dir>

Where u = user, g = group, o = others, a


= all;
+ = add permission, - = remove

To make a directory readable by others:


$ chmod go+rx <dir>

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Changing permissions
Permissions can also be expressed in
octal:
$ chmod ### <dir>
Where # is 0-7, and read = 4, write = 2,
execute = 1. Add the options together
to get the value. E.g. rwx = 7, rx = 5, rw
= 6.

To make a directory readable by all, and


writeable by owner only:
$ chmod 755 <dir>
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
A very common problem!
Make sure your executables have
the ‘x’ flag set for the owner!

If you compile yourself this should


already be the case, but many
copied files might night get correct
permissions copied.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


More on permissions
chown – change ownership
chgrp – change group ownership

Type ‘info fileutils’ to learn about all


of the utilities.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


But something’s wrong!
“So, I tried all of this on the
deparment systems and it didn’t
seem to have any effect on
directories!”

The department stores most things


(including user space) using the
Andrew File System.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Andrew File System
(AFS)
Developed at Carnegie-Mellon, named
after those Andrews.

AFS is a distributed file system using


storage servers and clients that provides
transparent access to local and remote
files.

Department specific information:


howto afs-intro
howto afs-security

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Andrew File System
(AFS)
Everything in AFS (in the world!) is stored in
/afs, and UNC-CS files are in /afs/cs.unc.edu/

Differences between Unix and AFS include:


• AFS works only at the directory level.
• Users can create their own groups in AFS.
• Users must have a valid token in AFS.

Don’t sweat the details, I’m going to give you all


of the commands you’ll need.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Making a web page
Create the directory:
$ mkdir public_html

Set the Unix permissions:


$ chmod go+rx public_html

Set the AFS permissions:


$ fs sa public_html system:anyuser read

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Making a web page, part
2
Create the index.html file:
$ cd ~/public_html
$ cp ~nashel/unix/index.html .

Feel free to edit or substitute your


own file(s).

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL


Homework
Create a web page for yourself in
your directory, on the
departmental system.

Send me an email with your


username, by noon, Wed., Jan. 14,
so that I can enter:
http://www.cs.unc.edu/~username
/
and see a web The
page.
UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Você também pode gostar