Next Previous Contents

4. Managing Directories

4.1 What Directory Am I In?

pwd

Indicate what directory we are in.

4.2 Changing Directories

cd foobar

Move to the directory foobar. You may specify the directory as an absolute or relative address.

cd

If you don't tell cd where to go, it will move you to your home directory.

cd -

Move to the previous directory. You were in some directory prior to moving to the current one. This puts you back to it.

4.3 Listing Directory Contents

The command for listing directories is ls. Running ls by itself results in nothing more than a list of filenames, of the files in the directory. The command line switches in the following examples can be mixed and matched to suit your requirements.

ls

List the files in the current directory

ls -l

List files in current directory, showing file permissions, owners, groups and file size. On SunOS4.1, you won't get the group listing.

ls -lg

Use this in SunOS4.1 to include the groups in your detailed file listing.

ls -l foo

List the directory entry of foo, showing file permissions, owners and size. If foo is a directory, List its contents, instead.

ls -ld foo

List the directory entry of foo, showing file permissions, owners and size, even if foo happens to be a directory.

ls -a

List the contents of the current directory, including the hidden files. When a filename starts with a period, UNIX will hide it. In other words, it will not list it unless specifically told to do so. Try this command out from your home directory.

ls -ld foo*

Do a detailed listing of everything in the current directory whose names start with foo. Do not list the contents of subdirectories.

ls -l | more

Do a detailed listing of files in the current directory, pausing at the end of each screen. The character in front of the word ``more'' is called a pipe. On my PS/2 keyboard, it is the uppershift backslash, next to the backspace button on the top row.

The behaviour of ls varies from UNIX to UNIX. You should read the man page for this.

Here is a sample directory listing...

-rw-r--r--   1 howard   users        6425 Apr  3  2000 bar
-rw-r-----   1 howard   users        2498 Apr  3  2000 foo
drwxr-x---   2 howard   users        1024 Oct 25 21:21 foobar
    
The files var and foo contain 6425 and 2498 bytes, respectively. They are owned by the user howard and the group users. There were both last modified on April 3, 2000. Both file may be read from and written to by howard, and may be read by member of the group users. The file bar may be read by anyone with access to the computer. The file foo cannot be read by anyone outside the group users. The other pathname foobar is a directory, indicated by the leading character d with the file permissions. Files in the directory were last modified on the evening of October 25 of the current year. The directory can only be written to by howard, but members of the group users can read it.

A long time ago when I was a Solaris administrator, someone came to me complaining that the computer would not load one of his files. I eventually ran ls with a tag that showed me a control character he had somehow embedded in the filename. I don't bother keeping track of how to do stuff like this. I read the man page back then. I haven't had the problem since.

4.4 Creating a New Directory

mkdir foo

Create the new directory foo. This could be a new directory in the current directory, or it can be an absolute pathname.

4.5 Deleting a Directory

rmdir foo

Delete the directory foo. This must be a directory. It can be a relative or absolute pathname. UNIX will refuse to delete the directory if there is anything in it.

rm -ri foo

Delete the directory foo, and all of its contents, prompting you before deleting each file.

rm -rf foo

Delete the directory foo and all of its contents. Too bad if you made a typo!

4.6 Setting Directory Permissions

File permissions determine who can access your directories. There are three entites you can provide access to, the owner (you), members of your group (see /etc/group), and everyone.

If you list a directory with the command ls -ld foo, the first column (on Linux anyway) will be the file permissions, shown as something like drwxr-x---. The letter d shows that the entry is a directory. This particular code shows that the owner can read from, write to, or execute the contents of the directory (rwx). Members of the owners's group can read or execute the contents of the directory, and everybody else is allowed no access to the directory.

If you want to change the permissions, you must use the command chmod, as follows...

Command          Permissions from command ls
-------          ---------------------------
chmod 700 foo    drwx------
chmod 750 foo    drwxr-x---
chmod 770 foo    drwxrwx---
chmod 755 foo    drwxr-xr-x
chmod 777 foo    drwxrwxrwx
          
UNIX directories do not work unless the execute permissions are turned on, as indicated by the x's above.

Computer geeks -- note how careful we are to not use the word ``octal''.


Next Previous Contents