|
Introduction to the Unix File System |
A 'How To' Guide for the Nanopublisher |
Unix (and Linux) has a hierarchical file system that consists of directories and files (directory is the Unix name for what Windows calls a folder). You refer to a file or directory using its path. For example, the path to the password file on most Unix systems is
/etc/passwd
The initial “/” signifies the root directory or the root of the file system. Subsequent uses of “/” in the path serve to separate the directory and file names. Thus /etc is a directory at the root of the filesystem and /etc/passwd is a file contained within /etc. This is an absolute path because it begins with a “/” and so is relative to the root directory.
A process is a running program. When you log into a Unix system you are given a command interpreter process, which is referred to as your shell. From your shell you can run other programs. Every process carries the notion of a current working directory, also known as “.”. This is generally set to be the current working directory of the shell that started the process at the time it was started. Thus, if your current working directory is /etc, you can refer to /etc/passwd as either
./passwd
or simply
passwd
These are referred to as relative paths as they start from the current working directory rather than from the root directory.
The parent directory of the current working directory is referred to as “..”. So, if your current working directory is /home, you can refer to /etc/passwd using
../etc/passwd
You can use “.” and “..” anywhere along a path, but if not used at the beginning of the path they refer not to the current working directory but the current directory at that point in the path. So a complicated way of referring to /etc/passwd is
/etc/../home/../etc/./passwd
Every user has a home directory, which is the initial current working directory of their login shell. Your home directory is denoted “~”. Thus if your login name is peter, and your home directory is /home/peter, then a file recipes in your home directory could be referenced using
~/recipes
This is considered an absolute path as the ~ is simply replaced by /home/peter.
You can also refer to other peoples home directory using ~username. Assume there is a user named paul. Then their home directory is referenced using
~paul
and if Paul had a file named recipes in his home directory, it is referenced using
~paul/recipes
Associated with each file and directory is a set of permissions that determine what you and others can do with them.
With respect to each file and directory there are three types of permissions and three types of users. The permissions for files are the permission to read (r), write (w), or execute (x) the file. If a user has read permission, he or she may view the contents of that file. With write permission, the user may change the contents of the file. And execute permission indicates to Unix that the file can be executed. This is useful if the file contains a compiled program or a shell script. A shell script is a series of commands that could be typed into a command shell, but is instead placed in a file. When the file is executed, all of the commands within the file are executed in sequence. On directories, read permission gives one the ability to see the names of the contents (files and directories) that are contained in the directory, write permission gives one the ability to add, remove, and rename contents, and execute permission gives access to the directory and its contents, meaning that you can make the directory your current working directory, and you can read and write the contents of the files.
The three types of users are the user that owns the file (u), the users that are members of the group to which the file belongs (g), and all others (o). To see the permissions for a file or directory, use the command "ls -ld". For example, to see the permissions of your home directory use
> ls -ld ~ drwxr-xr-x 31 joe doc 2928 May 5 12:03 /home/joe
The permissions are listed in the series of letters and dashes on the far left. The first letter is a "d" indicating a directory. This will be a "-" for regular files. The rest of the letters and dashes are grouped into three sets of three. The first three represents the permissions of the owner of the file. In this case the owner is joe (as indicated by the third column) who has read, write, and execute permissions on his home directory, as one would expect. The permissions are always listed in this order, and a dash indicates the permission is withheld. The permissions for the file's group is listed next. The group of the file is "doc" (as indicted by the fourth column) and the members of doc have read and execute permission, but not write permission. The final set of three give the permissions for everyone else, in this case they are the same as the doc group, read and execute, but not write.
To read a file you need execute access to the directory it is in AND read access to the file itself. To write a file, your need execute access to the directory AND write access to the file. To create new files or delete files, you need write access to the directory. You also need execute access to all parent directories back to the root. Group access will break if a parent directory is made completely private.
You can change the permissions of a file or directory using the "chmod" or 'change mode' command. To allow the doc group to write the directory, use
chmod g+w /home/joe
The desired change is specified in this case using "g+w", which translates to "group adds write". Many different cases are possible. With this form of the chmod command, you give the affected users, an operator, and then affected permissions. The users are denoted with u (the user or final owner), g (the group associated with the file), o (others), and a (all). The possible operators are + (add the permissions), - (subtract or remove the permissions), and = (set or make the permissions equal to the given set). Finally, the permissions are denoted using r (read), w (write) and x (execute/access). You can specify more than one group or more than one permission at a time, with 'ugo' being equivalent to 'a'. Here are some examples:
chmod o-rx /home/joe # remove the ability of others to access the directory
chmod o= /home/joe # another way to remove the ability of others to access the directory
chmod g+w /home/joe # give the group the ability to write in the directory
chmod a+rwx /home/joe # give everyone full access to the directory
chmod -R g+rX /home/joe # give the group the ability to read and execute (as appropriate) to /home/joe and to all files and directories contained within /home/joe
The last example use two new features. The '-R' option indicates the the command should be applied recursively to /home/joe, and to any files or directories contained within /home/joe, and to any files and directories contained within those directories, etc. The 'X' permission gives the execute permission only if the permission is already present for some other user. So the 'X' is used to give members of the group execute permission on a file only if that file is already executable by someone else (probably the owner) and it gives access permission on directories where someone else already has access permission. Some versions of chmod also allow the u, g, or o on the right side of the operator, which means that the permissions for the users specified on the left side of the operator should be set to be the same as the current permissions of the user specified on the right side. On these systems, the following example might well be preferred over the last example.
chmod -R g=u /home/joe # recursively give members of the group the same permissions as the owner
When you create a file or directory it automatically is assigned an owner and a group. You are the owner and your default group is used as the group. As a normal user, you are able to change the group of a file or directory using the chgrp command, or you can change the permissions of the files and directories you own. The ability to change the owner for files and directories, to create new groups, and to change the members of groups is given only the root, the Unix name for the system administrator.
To see the list of groups you belong to, use the command 'groups'. To see a list of all groups, examine the file /etc/groups. And to change the group of a file or directory, use 'chgrp newgroup files'. For example, if joe moves from the doc group to the support group, one can use the following to change the group of all of his files and directories:
chgrp -R support ~joe
When collaborating, it is often desirable to create a directory that several people will share. To do this, create a group and add everyone that will collaborate to that group. Then create or choose a directory to share and use chgrp to change the group of this directory. Finally, set the "group ID" bit on the directory using
chmod g+rws dir
This will cause the directory to be readable and writable by members of the group, and will cause files created within that directory to be assigned the group of the directory rather than the primary group of the user that created the file. Users will still have to remember to make the files that they create readable and writeable to the groups using
chmod g+rwX files
![]() |
![]() |
Any questions or comments on these notes can be directed to theNurds@nurdletech.com.
| Created and hosted by |
|||||||||||