The UNIX operating system organizes files into a tree structure with a root named by the character '/'. An example of the directory tree is shown below:
All files have two types of names. An absolute path name is the name of the file that begins with a "/"; for example, in the above picture, the program ls has as its absolute path name /bin/ls (because to find it, start at the root /, then go to the directory bin in /, then to ls in bin). A relative path name identifies a file relative to an arbitrary directory in the tree; for example, the relative path name of ls with respect to the directory bin is ls, because within the directory bin you need only look for the file ls. An easy way to keep the two types straight is to remember that an absolute path name always begins with a /, and a relative path name never does.
Every directory has at least two subdirectories. The directory . refers to the directory containing that entry; in other words, in the above picture, /bin/. and /bin refer to the same directory. The . is most often used in contexts where the "current directory" is important, and not the specific name of the current directory. The directory .. refers to the parent directory; so /bin/.. is another way of saying /, and /usr/ucb/.. is the same as /usr. This gives you a convenient way to get back up the file tree.
An ordinary file (also called a regular file) may contain text, a program, or other data. It can be either an ASCII file, with each of its bytes being in the numerical range 0 to 127 (representing characters), or a binary file, whose bytes can be of all possible values 0 to 255.
A directory is a file that consist of directory entries for the files in that directory. There is one directory entry for each file. Each directory entry contains the name of the file and a pointer to the file.
A device file is a file that represents a device. The UNIX operating system represents physical devices (printers, terminals etc.) as filed so that the same I/O functions used to read and write regular files can be used to read from and write to these devices. Devices which do input and output a character at a time, such as terminals and printers, are represented as character special device files; devices which do input and output in chunks of characters (usually a multiple of 256 characters) are represented as block special device files.
Understanding link files requires you to know a little about how the system implements the concept of "file." The UNIX operating system associates a structure called an inode with each file; all characteristics of the file are stored there. Among things like access permissions, date and time of last access and modification, and owner, the inode identifies the data that makes up the file. The "pointer" to the file that a directory contains is simply the inode number of the file.
Two files are said to be linked if they refer to the same inode number. If the file X is linked to the file Y, then X is an alternate name for the file Y. For example, cat X will produce exactly the same effect as cat Y. There are two types of links. A hard link is simply an entry in a directory which contains an inode number that also is associated with another file name; so, using X and Y as examples, the directory entries would have two different names (one would be X and the other Y) but the inode numbers would be the same. A symbolic link (or soft link) is simply a file containing the name of the file it is linked to. The difference is that with a hard link, deleting one of the directory entries does not affect the other but with a symbolic link, deleting the file which is linked to makes the link useless. The command ln(1) creates both types of links; give it the -s option to get a symbolic link, and no options to get a hard link.
The other types depend upon the system you are using. The most common other types are sockets (which are used by processes to communicate with one another) and FIFOs (also called named pipes, these are very similar to sockets).
The command ls -l allows you to see the type of each file in a directory (among other things). The following is the output of the command
ls -alg(the a option means to list those files and directories whose names begin with a period, and the g option means to show the group of the file):
drwxr-xr-x 6 ecs4005 student 1024 Apr 22 13:30 ./ drwxr-xr-x 74 root student 1536 Mar 24 12:51 ../ -rw------- 1 ecs4005 student 188 Apr 13 15:53 .login -rw------- 1 ecs4005 student 6 Mar 24 11:29 .logout -rw------- 1 ecs4005 student 253 Apr 10 12:50 .xinitrc -rw-r--r-- 1 ecs4005 student 516 Apr 10 13:00 .twmrc -rw-r--r-- 1 ecs4005 student 1600 Apr 22 10:59 test2.outThe output is separated into 7 columns. The meaning of each column in the output is shown below:
1st column type and protection mode of the file 2nd column number of links to that file (the original file is considered a link) 3rd column owner of the file 4th column group of the file 5th column size of file in bytes 6th column date and time of last modification 7th column name of the file
drwxr-xr-xThe meanings of the characters in this column are shown below:
b
c d l p s - |
block special device
character special device directory symbolic link FIFO (named pipe) socket regular file |
r
w x s - |
as character 2 means the owner can read the file
as character 3 means the owner can write the file as character 4 means the owner can execute the file as character 4 means the program runs as if the owner executed it, regardless of who actually executed it (called "the setuid bit;" don't worry about this) instead of any of the above means the owner can't access the file as appropriate |
r
w x s - |
as character 2 means the group can read the file
as character 3 means the group can write the file as character 4 means the group can execute the file as character 4 means the program runs as if the group executed it, regardless of who actually executed it (called "the setgid bit;" don't worry about this) instead of any of the above means the group can't access the file as appropriate |
r
w x s - |
as character 2 means anyone can read the file
as character 3 means anyone can write the file as character 4 means anyone can execute the file as character 4 means the program is not swapped out when it finishes (called "the sticky bit;" don't worry about this) instead of any of the above means no-one can't access the file as appropriate |
You can change the access characteristics of a file using the command chmod(1), which we will talk about in a later section.
% pwd show which directory I'm in /usr/ucb % cd /tmp change to /tmp % pwd now check I'm in /tmp /tmp
% ls % mkdir help % ls help % mv help nohelp % ls nohelp % rmdir nohelp % ls %
% ls . /tmp .: hw1 /tmp: % mv hw1 /tmp/hw1 mv: can't mv directories across file systems % cp -r hw1 /tmp/hw1 % ls . /tmp .: hw1 /tmp: hw1
chmod mode filename or chmod who op permissions
This changes the access permissions of the file. It has two forms, illustrated above.
In the first form, you specify an absolute mode , which is an octal number constructed from the logical OR of any set of the following modes
400
200 100 040 020 010 004 002 001 |
owner can read the file
owner can write to the file for a file, owner can execute it; for a directory, owner can look for a file in it (search permission) members of the file's group can read the file members of the file's group can write to the file for a file, members of the file's group can execute it; for a directory, members of the directory's group can look for a file in it (search permission) all other users can read the file all other users can write to the file for a file, all other users can execute it; for a directory, all other users can look for a file in it (search permission) |
chmod 751 .loginmakes the file .login readable, writeable, and executeable by the owner, readable and executable by any members of .login's group, and executable only by everyone else.
The second form of the chmod(1) command allows you to specify the mode symbolically. A symbolic mode has the form
who op permissionwhere who is a combination of:
u
o g a |
owner (user) permissions
others' permissions group permissions all users; same as ugo |
+
- = |
to add the permission
to remove the permission to assign the permission explicitly (all other bits for that category, owner, group, or others, will be cleared) |
r
w x |
read
write execute |
chmod u=rx,g-r,o+r .loginwill change the owner's access to read and execute only (the writing permission is deleted), will delete the write permission for members of the group, and will add read permission for everyone else. Here's what the two commands would do; note we use
ls -al .loginto check file status:
% ls -al .login check current access mode -rw-rw-rw- 1 ecs4005 188 Apr 13 15:53 .login % chmod 751 .login change access modes % ls -al .login check current access mode -rwxr-x--x 1 ecs4005 188 Apr 13 15:53 .login % chmod u=rx,g-r,o+r .login change permissions again % ls -al .login check current access mode -r-x--xr-x 1 ecs4005 188 Apr 13 15:53 .login %
% ls % touch xyzzy % ls -l -rw-r--r-- 1 ecs4005 0 Jan 11 15:53 xyzzy ... 2 minutes go by ... % touch xyzzy % ls -l -rw-r--r-- 1 ecs4005 0 Jan 11 15:55 xyzzy
% cmp hello goodbye hello goodbye differ: char 17816, line 49
% diff xyzzy hello 57,58c57,60 < and so I came down the mountain < and saw a clear lake --- > and when he got there > his poor cupboard was bare > and so the poor doggie > got none.
% df Filesystem kbytes used avail capacity Mounted on /dev/xy0a 14584 10014 3111 76% / /dev/xy0g 686209 304550 313038 49% /usr