Here is an index to the topics in this section:
All the stored information on a Unix computer is kept in a filesystem. Any time you initiate a shell login session, the shell considers you to be located somewhere within a filesystem. Although it may seem strange to be "located" somewhere in a computer's filesystem, the concept is not so different from real life. After all, you can't just be, you have to be somewhere. The place in the filesystem tree where you are located is called the current working directory.
CONCEPT: The Unix filesystem is hierarchical (resembling a tree structure). The tree is anchored at a place called the root, designated by a slash "/". Every item in the Unix filesystem tree is either a file, or a directory. A directory is like a container. A directory can contain files, and other directories. A directory contained within another is called the child of the other. A directory in the filesystem tree may have many children, but it can only have one parent. A file can hold information, but cannot contain other files, or directories.
CONCEPT: To describe a specific file or directory in the filesystem hierarchy, you must specify a "path." The path to a location can be defined as an absolute path, starting from the root anchor point, or as a relative path, starting from the current location. When specifying a path, you simply trace a route through the filesystem tree, listing the sequence of directories you pass through as you go from one point to another. Each directory listed in the sequence is separated by a slash.
It is initially confusing to some that Unix uses the slash character "/" to denote the filesystem root directory, and as a directory separator in paths. Just remember, when the slash is the first thing in the path to the file or directory, the path begins at the root directory. Otherwise, the slash is a separator.
Unix provides the shorthand notation of "." to refer to the current location, and ".." to refer to the parent directory. Because Unix filesystem trees have no loops, the ".." notation refers unambiguously to the directory's parent.
EXERCISE: Specify the absolute path to the directory named "jon" at the bottom of the tree diagram.
EXPLANATION: Since the absolute path must always begin at the root (/) directory, the path would be:
EXERCISE: Specify the relative path from the directory named "student" to the directory named "jon" in the tree diagram.
EXPLANATION: Starting from the student directory, we would first have to move up the filesystem tree (using the ".." notation) to the directory called "users" before we could descend to the directory called "jon". The path would be:
CONCEPT: Unix supports access control. Every file and directory has associated with it ownership, and access permissions. Furthermore, one is able to specify those to whom the permissions apply.
Permissions are defined as read, write, and execute. The read, write, and execute permissions are referred to as r, w, and x, respectively.
Those to whom the permissions apply are the user who owns the file, those who are in the same group as the owner, and all others. The user, group, and other permissions are referred to as u, g, and o, respectively.
A short note on groups: Unix allows users to be placed in groups, so that the control of access is made simpler for administrators.
The meaning of file and directory permissions
To see the permissions on a file, use the ls command, with the -l option.
EXAMPLE: Execute the command
to view the information on the system password database. The output should look similar to this:
-rw-r--r-- 1 root sys 41002 Apr 17 12:05 /etc/passwd
The first 10 characters describe the access permissions. The first dash indicates the type of file (d for directory, s for special file, - for a regular file). The next three characters ("rw-") describe the permissions of the owner of the file: read and write, but no execute. The next three characters ("r--") describe the permissions for those in the same group as the owner: read, no write, no execute. The next three characters describe the permissions for all others: read, no write, no execute.
Unix allows you to set the permissions on files that you own. The command to change the file permission mode is chmod. Chmod requires you to specify the new permissions you want, and specify the file or directory you want the changes applied to.
To set file permissions, you may use to the "rwx" notation to specify the type of permissions, and the "ugo" notation to specify those the permissions apply to.
To define the kind of change you want to make to the permissions, use the plus sign (+) to add a permission, the minus sign (-) to remove a permission, and the equal sign (=) to set a permission directly.
EXAMPLE: Type the command
to change the file permissions on the file .shrc, in your home directory. Specifically, you are specifying group read access and write access, with no execute access.
EXERCISE: Change the permissions on the .shrc file in your home directory so that group and others have read permission only.
EXPLANATION: Typing the command
would accomplish the task.
You can also combine multiple operation codes in a single statement. Separate each operation code with a comma, and do not have any embedded space anywhere in the operation code sequence. For example:
chmod u=rwx,go=r-x foo
Would set owner permissions to rwx, with group and other permissions set to r-x.
In Unix, your location in the filesystem hierarchy is known as your "current working directory." When you log in, you are automatically placed in your "home directory." To see where you are, type the command
which stands for "print working directory."
To change your location in the filesystem hierarchy, use the cd (change directory) command, followed by an argument defining where you want to go. The argument can be either an absolute path to the destination, or a relative path.
EXAMPLE: Type the command
to go to the /tmp directory. You can type
to confirm that you're actually there.
If you type the cd command without an argument, the shell will place you in your home directory.
EXERCISE: Type the command
and note the result. Then type
to the shell. Type
again to see where you ended up.
EXPLANATION: The "cd .." command should have moved you up one level in the directory tree, because ".." is Unix shorthand for the parent directory. The result of the second "pwd" command should be the same as the first, with the last directory in the path omitted.
The ls command allows you to see the contents of a directory, and to view basic information (like size, ownership, and access permissions) about files and directories. The ls command has numerous options, so see the manual page on ls (type man ls) for a complete listing. The ls command also accepts one or more arguments. The arguments can be directories, or files.
EXAMPLE: Type the command
to the Unix shell.
In the example, the "l" and "R" options of the ls command are invoked together. Some commands permit you to group options in that way, and some commands require the options to be named separately, e.g., ls -l -R. The l option calls for a long output, and the R option causes ls to operate recursively, moving down directory trees.
The last part of the example, "/lib/l*", directs the ls command to list files and directories in the /lib directory, that begin with the letter l. The wild card character, "*", matches any character(s).
EXERCISE: Type the command
to the shell. How did the shell respond, and why?
EXPLANATION: The shell responded by printing all the entries in the /etc directory that start with the letter i, and end with the letter g. The -m option causes the output to be streamed into a single line. See the manual page for ls to get a complete description of the ls command's options.
EXERCISE: Find the permissions on your home directory.
EXPLANATION: There are many ways to accomplish this. You could type
to get to your home directory, and then type
The -a option instructs the ls command to list all files, including those that start with the period character (ls normally ignores files that begin with a period). The directory permissions are listed next to the "." symbol. Remember that "." is Unix shorthand for the current working directory.
CONCEPT: Text files are intended for direct viewing, and other files are intended for computer interpretation.
The Unix file command allows you to determine whether an unknown file is in text format, suitable for direct viewing.
EXERCISE: Type the command
to see what kind of file the shell is.
EXPLANATION: The shell is a shared executable, indicating that the file contains binary instructions to be executed by the computer.
The cat command
The cat command concatenates files and sends them to the screen. You
can specify one or more files as arguments. Cat makes no attempt to format
the text in any way, and long output may scroll off the screen before you
can read it.
EXAMPLE: Send the contents of your .profile file to the screen by typing
to the shell. The tilde character (~) is Unix shorthand for your home directory.
The more command
The more command displays a text file, one screenful at a time. You
can scroll forward a line at a time by pressing the return key, or a screenful
at a time by pressing the spacebar. You can quit at any time by pressing
the q key.
EXAMPLE: Type
to the shell. Scroll down by pressing return, and by pressing the spacebar. Stop the more command from displaying the rest of the file by typing q.
The head and tail commands
The head command allows you to see the top part of a file. You may
specify the number of lines you want, or default to ten lines.
EXAMPLE: Type
to see the first fifteen lines of the /etc/rc file.
The tail command works like head, except that it shows the last lines of of file.
EXAMPLE: Type
to see the last ten lines of the file /etc/rc. Because we did not specify the number of lines as an option, the tail command defaulted to ten lines.
The Unix command to copy a file or directory is cp. The basic cp command syntax is cp source destination.
EXAMPLE: The command
makes a copy of your .profile file, and stores it in a file called "pcopy" in your home directory.
Before creating the new copy, the cp command is clever enough to check to see if a file or directory with the destination name already exists. If a file with that name already exists, the shell will overwrite the old file. If a directory with that name already exists, the shell will place the copy in that directory, and make the name of the new copy the same as the original.
EXERCISE: Describe the permissions necessary to successfully execute the command in the previous example.
EXPLANATION: To copy the .profile file, one must have read permission on the file, and execute permission in the directory where the file resides. To create the new file called pcopy, one must have write and execute permission in the directory where the file will be created.
The Unix mv command moves files and directories. You can move a file to a different location in the filesystem, or change the name by moving the file within the current location.
EXAMPLE: The command
takes the pcopy file you created in the cp exercise, and renames it "qcopy".
The rm command is used for removing files and directories. The syntax of the rm command is rm filename. You may include many filenames on the command line.
EXAMPLE: Remove the the shrccopy file that you placed in your home directory in the section on moving files by typing
The Unix mkdir command is used to make directories. The basic syntax is mkdir directory-name. If you do not specify the place where you want the directory created (by giving a path as part of the directory name), the shell assumes that you want the new directory placed within the current working directory.
EXAMPLE: Create a directory called foo within your home directory by typing
EXERCISE: Create a directory called bar, within the directory called foo, within your home directory.
EXPLANATION: Once the foo directory is created, you could just type
Alternately, you could type
In the second solution, two Unix commands are given, separated by a semicolon. The first part of the command makes foo the current working directory. The second part of the command creates the bar directory in the current working directory.
The Unix rmdir command removes a directory from the filesystem tree. The rmdir command does not work unless the directory to be removed is completely empty.
The rm command, used with the -r option can also be used to remove directories. The rm -r command will first remove the contents of the directory, and then remove the directory itself.
EXERCISE: Describe how to remove the "foo" directory you created, using both rmdir, and rm with the -r option.
EXPLANATION: You could enter the commands
to accomplish the task with the rmdir command. Note that you have to rmdir the bar subdirectory before you can rmdir the foo directory. Alternately, you could remove the foo directory with the command