Working with Unix Directories
Unix uses a hierarchical file system for storing files. This system consists of a "root" directory which is made up of sub-directories which in turn are made up of sub-directories. If you are not already familiar with the concept of a hierarchical file system, see one of the general books listed in the references for a detailed discussion of this structure.
The ls command is used to list the contents (names of files) in a directory. If no directory path is specified, ls will display the files in the current directory.
Display a list of files:
Display file characteristics:
When this option is used, the names of executable files are followed by an asterisk ("*") and directory names will be followed by a slash ("/"). On some Unix systems, directory names are followed by both a slash and an asterisk ("/*").
Files which begin with a dot (.) are hidden and not displayed by the ls command unless specifically requested. These files are frequently used as configuration files by application programs. To display these files, enter:
To obtain a "long" listing of all files in the current directory, enter:
This listing includes the following information:
- File Permissions: who has permission to read, write, or execute the file.
- A "d" in the first column indicates that this file corresponds to a directory, a "-" indicates a regular file.
- The next three columns indicate whether the owner has read, write, or execute permission for the file.
- The next three columns indicate whether other members in your group have read, write, or execute permission for the file.
- The next three columns indicate whether individuals other than the owner and members in the owners group have read, write, or execute permission for the file.
- Number of Links
- Owner Name
- Group Name: On some Unix systems (typically BSD based) it is necessary to include the -g flag in order to display the group name; on VTAIX and System V based systems, inclusion of the -g option will suppress display of the group membership column.
- Size in Bytes
- Date of Last Modification
The primary commands used when working with directories are:
cd pwd cd xmp pwdmkdir test_dir ls -Fls *.c ls test_dir mv my_source.c test_dir ls *.c ls test_dirls -a test_dir rmdir test_dir mv testdir/mysource.c . ls -a test_dir rmdir test_dir ls -F
Note: You cannot remove a directory until you have first moved or removed (deleted) all of the files it contains. You must also have write permission for the parent directory containing the directory you wish to remove.
You should become familiar with the definitions of the following terms:
- Current Working Directory (.)
- The directory within which you are currently working. In an earlier exercise, you copied the sample files to the "." directory. To Print (display) the Working Directory, enter:pwd
- Parent Directory (..)
- The directory immediately above the current directory in the directory hierarchy. Observe the results of entering the following commands:pwd cd .. pwd cd .. pwd
- Home Directory
- The default directory associate with a userid. To change to the home directory of the VTAIX Class Instructors Id, enter:cd ~aixstu00
The ~ abbreviation is only available if you are using the C or Korn Shell. To change to your own home directory, you can simply enter:cd
- relative path
- Start from current directory (.) and observe the results of the following commands:cd cd xmp pwd cd c pwd ls
- absolute path
- Start from "root" (/) and observe the results of entering the following commands:cd /etc cd xmp cd $HOME/xmp ls cd / ls cd ls
- In the Current Directory: Include ./ in front of the filename.
- In Directories in the Search Path: Enter the filename (use "echo $PATH" to display the search path).
- Otherwise: Include absolute path followed by filename.
As an exercise, change to the root directory and observe the sub-directories it contains:
cd / ls -F
Recall that the "-F" option of the ls command appends a slash ("/") to the end of sub-directory names. You should see most of the following directories, plus some others:
- /bin -- standard Unix utilities
- /usr -- shared applications
- /bin -- the "less commonly used" standard Unix utilities
- /local -- local applications
- /lib -- programming library files
- /etc -- configuration files
- /dev -- hardware device files
- /tmp -- a temporary work area. Warning: Do not put any files you wish to keep in this directory!
Note: On VTAIX and BSD based Unix systems, the symbol @ is used to indicate a symbolic link. Observe here that "bin" is a symbolic link and displayed as "bin@" instead of "bin/" as it would be on many other Unix systems. Enter the command "ls -lF b*" and observe the "->" following "bin@". This symbol points to the directory /usr/bin which contains the files which are accessed whenever a reference is made to the /bin directory.
The home directories for each of the userids on the system may be included in directories called "us1", "us2", and "us3". On other Unix systems, these directories may be called home, users, or u. To return to your home directory, simply enter:
Recall that you can display the file access permissions by entering:
At the beginning of each entry you will see 10 columns containing information about file characters.
d r w x r - x r - -
The first column provides information about the file type: "d" for a directory, "l" for a symbolic link, or "-" (dash) for a regular file. The next columns contain the permission fields for user (owner), group, and other:
- r: read
- w: write
- x: execute
The "read", "write", and "execute" permissions associated with a file indicate whether you can look at the contents of the file, make changes to the file, or execute the program contained in the file.
You can change the current access permissions using the chmod command. The general syntax of the chmod command is given by:
chmod ugoa±rwx file_name
- ugoa is: u for user or owner, g for group, o for other, a for all
- ± is: + to add permission, - to remove it
- rwx is: r for read, w for write, x for execute
For example, to add execute permission for owner to the file "file_name", enter:
chmod u+x file_name
You can specify more than one option with the chmod command; for example, to remove read and execute permission for group and other from a file called file_name, enter:
chmod og-rx file_name
Be careful how you assign access permissions. It is possible to deny yourself read, write, and execute permission while granting these permissions to group and other.You can also use numeric codes to change the current access permissions for a file. A chmod value of 1 enables execute permission, a value of 2 enables write permission, and a value of 4 enables read permission. Combinations of read, write, and execute can be obtained by adding the values corresponding to the permissions you wish to assign. For example, read and execute permission can be specified by adding the values 4 and 1 to yield 5. These codes are summarized in the following table:
1 --x execute only 2 -w- write only 3 -wx write and execute 4 r-- read only 5 r-x read and execute 6 rw- read and write 7 rwx read, write, and execute
To change the access permissions of "file_name" to read, write, and execute for owner (4+2+1=7), with read and execute for group (4+1=5), and execute (1) only for other, enter:
chmod 751 file_name
Then observe the results of the following commands:
ls -l file_name ./file_name cat file_name chmod a-rx file_name ls -l f* chmod a+r file_name ls -l f*
Note: As illustrated above, it may be possible to turn on execute permission for a file; however, if the file does not contain meaningful commands, you will obtain nothing but error diagnostics when you attempt to execute it.
The umask command sets a mask which limits the access permissions assigned to new files. To display the current umask values, simply enter:
The "umask" values are complements of the chmod values: a 1 denies execute permission, a 2 denies write access, and a 4 denies read access. As with the chmodcommand, the "umask" values can be combined; e.g., read-only access (denial of execute and write permission can be specified by adding 1 and 2 to yield 3). A seven (7) denies all access permissions. To set the umask values to allow the owner read, write, and execute (0 limits) permission for all new files; to deny group write (2) access (which is equivalent to allowing read and execute permission), and to deny other read and write (2+4=6) permissions (which is equivalent to granting execute only), enter:
Note: The value 026 is the complement of 751 (777-026=751) used in the chmod example described in the previous section.
To set the default permission mask to allow the owner read, write, and execute capabilities and to limit group and other execute and read access, enter:
To set the umask for future logon sessions, include this command in the file ".profile" (Bourne or Korn Shell) or ".login" (C Shell).The following table lists some of the most commonly used umask values. In each of these cases, the owner is assigned default read, write, and execute permissions; group and other are assigned the default access permissions listed in the column labeled "group & others":
chmod umask group & others 777 000 read, write, & execute 755 022 read & execute 744 033 read only 711 066 execute 700 077 no access
The values given in the "umask" column can be obtained by subtracting the "chmod" values from "777".
The access permissions associated with directories influence the operations which can be performed on files they contain. The meanings of the directory access permissions are:
- read allows display of a directory -- read permission is required in order to use ls to list characteristics of files within a directory; otherwise you will receive the message "No permission" following the name of each file in the directory.
- execute enables access to the contents of a directory -- execute permission is required in order to cd into a directory or to access (execute or examine) any of the files it contains. Without execute permission, you will typically receive the message "Permission Denied".
- write enables creation, deletion, and moving of files within the directory. Thus deletion of a file is limited by the permission associated with the directory and not by the individual file access permissions. Be careful to whom you provide directory write access permissions. They will not be allowed to modify the file if they do not have file write access permission, but they can remove it if they have directory write permission.
If you wish to share files with others, you need to be sure that both the permissions for the file and the directory containing it are set appropriately.
It is possible for your userid to belong to more than one group (the list of groups and their memberships is typically contained in the file /etc/groups). On VTAIX, your userid will be listed in /etc/groups only if your userid belongs to more than one group. Use the "ls -l" command ("ls -gl" on some systems) to display a list of files and their group membership. If you would like to change the group to which an existing file is assigned, use the "chgrp" command. If you would like to change the default group used for the assignment of new files, use the "newgroup" command.
Note: On most Unix systems, you will only be able to use the chgrp and newgroup commands for groups of which you are a member.
The internal "name" used by the system for each file is its i-node number. Each directory in a Unix system is a file which lists the files it contains with their corresponding i-nodes. An i-node is a data structure which defines a file's existence and contains information about its location on disk, size, ownership, permissions, and time of last modification. To display the i-node number corresponding to file_name, enter:
ls -i file_name
Observe the result of the following commands:
ls -i page2 mv page2 new_name ls -i new_name
Although the mv command has changed the name of the file, the i-node number remains unchanged.
find . -name 'f*' -print
The find command can also be used to display directory listings by using the "-type" option. For example, to display all directories below the current directory, enter:
find . -type d -print
The whereis command is used to locate the binary, source, and manuals for a command. To locate the occurrence of "vi" in any of the standard locations, enter:
If an executable program is located in more than one directory, you may wish to know which of these files will be executed. To determine this, you can use thewhich command. For example, to identify the directory containing the command which will be issued when "vi" is entered, enter:
If a command alias has been defined, the which command will display the alias for the command.
The grep command (Global Regular Expression Print) can be used to locate text within files. For example, to list all files in the xmp/c directory containing the word 'float', enter:
cd xmp/c grep 'float' *
To print a list of all lines in the file /etc/passwd containing the name of your userid, enter:
grep $USER /etc/passwd
Note: The above command will not yield the same result on all Unix systems; some Unix systems (such as NeXT) store their userid information in other locations.