previoussection   next section   upper level


Section 5: Working with Files and Directories

The following topics will be discussed in this section:

The UNIX filesystem structure

All the stored information on a UNIX computer is kept in a filesystem. Any time you interact with the UNIX shell, 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 heirarchical (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 file folder. 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 location in the filesystem heirarchy, you must specify a "path." The path to a location can be defined as an absolute path 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.

UNIX provides the shorthand notation of "." to refer to the current location, and ".." to refer to the parent directory. These two symbols actually are two hidden directories, the first being a link to the entries in the file structure for the current directory, and the second a link to the entries in the file structure in the directory just above it in the directory tree. These names make it easier to move through the directory tree and to describe paths relative to the current directory.

EXERCISE: Make directory mail under directory yupa

EXERCISE: Specify the absolute path to the directory named yupa at the bottom of the tree diagram.

EXPLANATION: Since the absolute path must always begin at the root (/) directory, the path would be:

/usr2/others/yupa/mail

EXERCISE: Specify the relative path from the directory named mail to the directory named public_html in the tree diagram.

EXPLANATION: Starting from the mail directory, we would first have to move up the filesystem tree (using the .. notation) to the directory called user1 before we could descend to the directory called public_html. The path would be:

../public_html

Changing Directories

In UNIX, your location in the filesystem heirarchy 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

pwd

which stands for "print working directory."

To change your location in the filesystem heirarchy, 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

cd mail

to go to the /usr2/others/yupa/mail directory. You can type

pwd

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

pwd

and note the result. Then type

cd ..

to the shell. Type

pwd

again to see where you end up.

EXPLANATION: The cd .. command should have moved you up one level in the directory tree, since .. 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.

File and directory permissions

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

Read permission
For a file, read permission allows you to view the contents of a file. For a directory, having read permission allows you to list the directory's contents.
Write permission
For a file, write permission allows you to modify the contents of the file. For a directory, write permission allows you to alter the contents of the directory, i.e., to add or delete files.
Execute permission
For a file, execute permission allows you to run the file, if it is an executable program, or script. Note that file execute permission is irrelevant for nonexecutable files. For a directory, execute permission allows you to cd to the directory, and make it your current working directory.

Viewing permissions

To see the permissions on a file, use the ls command, with the -l option.

EXAMPLE: Execute the command

ls -l /etc/passwd

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.

Setting permissions

UNIX allows you to set the permissions on files that you own. The command to change the file permission mode is chmod. The chmod command 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 the rwx (read, write, and execute) notation to specify the type of permissions, and the ugo (user, group, others) notation to specify who those 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

ls -la mail

Notice the permission of directory mail

chmod g=r-- ~/mail

to change the file permissions on the directory mail, in your home directory. The tilde character, ~, is UNIX shorthand for your home directory. Specifically, you are specifying group read access, with no write and execute access.

EXERCISE: Change the permissions on the mail directory in your home directory so that group and others have read permission only.

EXPLANATION: Typing the command

chmod go=r-- ~/mail

would accomplish the task.

Listing the contents of a directory

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

ls -lR /etc/i*

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, /mail/sa*, directs the ls command to list files and directories in the /mail directory, that begin with the letter "sa". The wildcard character, "*", matches any character(s).

EXERCISE: Type the command

ls -m /mail/sa*

to the shell. How did the shell respond, and why?

EXPLANATION: The shell responded by printing all the entries in the /mail directory that start with the letter "sa". 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

cd mail
pwd
cd~
pwd

to get to your home directory, and then type

ls -la

The -a option instructs the ls command to list all files, including those that start with the period character. The directory permissions are listed next to the . symbol. Remember that . is UNIX shorthand for the current working directory.

Viewing the contents of a file

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

file mail

to see what kind of file the mail is.

The cat command
The cat command concatenates files and sends them to the screen. You can specify one or more files as arguments. The 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 .bashrc file to the screen by typing

cat .bashrc

to the shell.

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 also scroll up or down one line at a time by pressing the "k" and "j" keys respectively. You can quit at any time by pressing the "q" key.

EXAMPLE: Type

more .bash_history

to the shell. Scroll down by pressing return, and by pressing the spacebar. Scroll up and down by pressing "j" and "k". 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

head .bash_history

to see the first fifteen lines of the .bash_history file.

The tail command works like head, except that it shows the last lines of of file.

EXAMPLE: Type

tail .bash_history

to see the last ten lines of the file .bash_history. Because we did not specify the number of lines as an option, the tail command defaulted to ten lines.

Copying files and directories

The UNIX command to copy a file or directory is cp. The basic cp command syntax is cp source destination.

EXAMPLE: The command

mkdir sample
cp ~/.pinerc ~/sample/pcopy
cd sample
ls -la

makes a copy of your .pinerc file, and stores it in a file called pcopy in sample directory.

EXERCISE: Describe the permissions necessary to successfully execute the command in the previous example.

EXPLANATION: To copy the .pinerc file, one must have read permission on the file. To create the new file called pcopy, one must have write permission in the directory where the file will be created.

Moving and renaming files

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

mv pcopy qcopy

takes the pcopy file you created in the cp exercise, and renames it qcopy.

Removing files

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.

CAUTION: Once the rm command has been executed on a file it is gone and cannot be retreived.

EXAMPLE: Remove the qcopy file that you placed in your home directory in the section on moving files by typing

rm qcopy

Creating a directory

The UNIX mkdir command is used to make directories. The basic syntax is mkdir directoryname. 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 temp within your home directory by typing

mkdir ~/temp

EXERCISE: Create a directory called bar, within the directory called temp, within your home directory.

EXPLANATION: Once the temp directory is created, you could just type

mkdir ~/temp/bar

Alternately, you could type

cd ~/temp; mkdir bar

In the second solution, two UNIX commands are given, separated by a semicolon. The first part of the command makes temp the current working directory. The second part of the command creates the bar directory in the current working directory.

Removing a 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.

CAUTION: When the rm -r command is executed on a directory the directory and all it's contents are removed and cannot be retreived.

EXERCISE: Describe how to remove the temp directory you created, using both rmdir, and rm with the -r option.

EXPLANATION: You could enter the commands

rmdir ~/temp
rmdir ~/temp/bar
ls -la
rmdir ~/temp

to accomplish the task with the rmdir command. Note that you have to rmdir the bar subdirectory before you can rmdir the temp directory. Alternately, you could remove the temp directory with the command

mkdir~/temp/bar
rm -r ~/temp


previoussection   next section   upper level