[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

System User's Guide: Operating System and Devices

Directory-Handling Procedures

You can work with directories and their contents in a variety of ways.

The command and an example are presented for each of the following directory tasks:

Creating a Directory (mkdir Command)

You can use the mkdir command to create one or more directories specified by the Directory parameter. Each new directory contains the standard entries dot (.) and dot dot (..). You can specify the permissions for the new directories with the -m Mode flag.

When you create a directory, it is created within the current, or working, directory unless you specify an absolute path name to another location in the file system.

The following are examples of how to us the mkdir command:

  1. To create a new directory called Test in the current working directory with default permissions, type:
    mkdir Test
    Press Enter.
  2. To create a directory called Test with rwxr-xr-x permissions in a previously created /home/demo/sub1 directory, type:
    mkdir -m 755 /home/demo/sub1/Test
    Press Enter.
  3. To create a directory called Test with default permissions in the /home/demo/sub2 directory, type:
    mkdir -p /home/demo/sub2/Test
    Press Enter.

    The -p flag creates the /home, /home/demo, and /home/demo/sub2 directories if they do not already exist.

See the mkdir command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Moving or Renaming a Directory (mvdir Command)

To move or rename a directory, use the mvdir command.

For example, to move a directory, type:

mvdir book manual

Press Enter.

This moves the book directory under the directory named manual, if the manual directory exists. Otherwise, the book directory is renamed to manual.

For example, to move and rename a directory, type:

mvdir book3 proj4/manual

Press Enter.

This moves the book3 directory to the directory named proj4 and renames proj4 to manual (if the manual directory did not previously exist).

See the mvdir command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Displaying the Current Directory (pwd Command)

You can use the pwd command to write to standard output the full path name of your current directory (from the /(root) directory). All directories are separated by a slash (/). The /(root) directory is represented by the first slash (/), and the last directory named is your current directory.

For example, to display your current directory, type:

pwd

Press Enter.

The full path name of your current directory displays similar to the following:

/home/thomas

Changing to Another Directory (cd Command)

The cd command moves you from your present directory to another directory. You must have execute (search) permission in the specified directory.

If you do not specify a Directory parameter, the cd command moves you to your login directory ($HOME in the ksh and bsh environments, or $home in the csh environment). If the specified directory name is a full path name, it becomes the current directory. A full path name begins with a slash (/) indicating the /(root) directory, a dot (.) indicating current directory, or a dot dot (..) indicating parent directory. If the directory name is not a full path name, the cd command searches for it relative to one of the paths specified by the $CDPATH shell variable (or $cdpath csh variable). This variable has the same syntax as, and similar semantics to, the $PATH shell variable (or $path csh variable).

The following are examples of how to use the cd command:

  1. To change to your home directory, type:
    cd
    Press Enter.
  2. To change to the /usr/include directory, type:
    cd /usr/include
    Press Enter.
  3. To go down one level of the directory tree to the sys directory, type:
    cd sys
    Press Enter.

    If the current directory is /usr/include and it contains a subdirectory named sys, then /usr/include/sys becomes the current directory.

  4. To go up one level of the directory tree, type:
    cd ..
    Press Enter.

    The special file name, dot dot (..), refers to the directory immediately above the current directory, its parent directory.

See the cd command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Copying a Directory (cp Command)

You can use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters. If the file specified as the TargetFile exists, the copy writes over the original contents of the file. If you are copying more than one SourceFile, the target must be a directory.

To place a copy of the SourceFile into a directory, specify a path to an existing directory for the TargetDirectory parameter. Files maintain their respective names when copied to a directory unless you specify a new file name at the end of the path. The cp command also copies entire directories into other directories if you specify the -r or -R flags.

The following are examples of how to use the cp command.

  1. To copy all the files in the /home/accounts/customers/orders directory to the /home/accounts/customers/shipments directory, type:
    cp /home/accounts/customers/orders/* /home/accounts/customers/shipments
    Press Enter.

    This copies only the files in orders directory into the shipments directory.

  2. To copy a directory, including all its files and subdirectories, to another directory, type:
    cp -R /home/accounts/customers /home/accounts/vendors
    Press Enter.

    This copies the customers directory, including all its files, subdirectories, and the files in those subdirectories, into the vendors directory.

See the cp command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Displaying Contents of a Directory (ls Command)

You can display the contents of a directory by using the ls command.

ls command

The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags. If you do not specify a File or Directory, the ls command displays the contents of the current directory.

By default, the ls command displays all information in alphabetic order by file name. If the command is executed by a user with root authority, it uses the -A flag by default, listing all entries except dot (.) and dot dot (..). To show all entries for files, including those that begin with a . (dot), use the ls -a command.

You can format the output in the following ways:

To determine the number of character positions in the output line, the ls command uses the $COLUMNS environment variable. If this variable is not set, the command reads the terminfo file. If the ls command cannot determine the number of character positions by either of these methods, it uses a default value of 80.

The information displayed with the -e and -l flags is interpreted as follows:

The first charactor may be one of the following:

d Entry is a directory.
b Entry is a block special file.
c Entry is a character special file.
l Entry is a symbolic link.
p Entry is a first-in, first-out (FIFO) pipe special file.
s Entry is a local socket.
- Entry is an ordinary file.

The next nine characters are divided into three sets of three characters each. The first three characters show the owner's permission. The next set of three characters shows the permission of the other users in the group. The last set of three characters shows the permission of anyone else with access to the file. The three characters in each set show read, write, and execute permission of the file. Execute permission of a directory lets you search a directory for a specified file.

Permissions are indicated as follows:

r Read permission granted
t Only the directory owner or the file owner can delete or rename a file within that directory, even if others have write permission to the directory.
w Write (edit) permission granted
x Execute (search) permission granted
- Corresponding permission not granted.

The information displayed with the -e flag is the same as with the -l flag, except for the addition of an 11th character, interpreted as follows:

+ Indicates a file has extended security information. For example, the file might have extended ACL, TCB, or TP attributes in the mode.
- Indicates a file does not have extended security information.

When the size of the files in a directory are listed, the ls command displays a total count of blocks, including indirect blocks.

For example, to list all files in the current directory, type:

ls -a

Press Enter.

This lists all files, including

For example, to display detailed information, type:

ls -l chap1 .profile

Press Enter.

This displays a long listing with detailed information about chap1 and .profile.

For example, to display detailed information about a directory, type:

ls -d -l . manual manual/chap1

Press Enter.

This displays a long listing for the directories . and manual, and for the file manual/chap1. Without the -d flag, this would list the files in the . and manual directories instead of the detailed information about the directories themselves.

See the ls command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Deleting or Removing a Directory (rmdir Command)

You can use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can only contain . and ..) before you can remove it, and you must have write permission in its parent directory. Use the ls -a Directory command to check whether the directory is empty.

The following are examples of how to use the rmdir command:

  1. To empty and remove a directory, type:
    rm mydir/* mydir/.* 
    rmdir mydir
    Press Enter.

    This removes the contents of mydir, then removes the empty directory. The rm command displays an error message about trying to remove the directories dot (.) and dot dot (..), and then the rmdir command removes them and the directory itself. 

    Note
    rm mydir/* mydir/.* first removes files with names that do not begin with a dot, and then removes those with names that do begin with a dot. You might not realize that the directory contains file names that begin with a dot because the ls command does not normally list them unless you use the -a flag.
  2. To remove the /tmp/jones/demo/mydir directory and all the directories beneath it, type:
    cd /tmp
    rmdir -p jones/demo/mydir
    Press Enter.

    This removes the jones/demo/mydir directory from the /tmp directory. If a directory is not empty or you do not have write permission to it when it is to be removed, the command terminates with appropriate error messages.

See the rmdir command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

Comparing the Contents of Directories (dircmp Command)

You can use the dircmp command to compare two directories specified by the Directory1 and Directory2 parameters and write information about their contents to standard output. First, the dircmp command compares the file names in each directory. If the same file name is contained in both, the dircmp command compares the contents of both files.

In the output, the dircmp command lists the files unique to each directory. It then lists the files with identical names in both directories, but with different contents. If no flag is specified, it also lists files that have identical contents as well as identical names in both directories.

The following are examples of how to use the dircmp command:

  1. To summarize the differences between the files in the proj.ver1 and proj.ver2 directories, type:
    dircmp proj.ver1 proj.ver2
    Press Enter.

    This displays a summary of the differences between the proj.ver1 and proj.ver2 directories. The summary lists separately the files found only in one directory or the other, and those found in both. If a file is found in both directories, the dircmp command notes whether the two copies are identical.

  2. To show the details of the differences between the files in the proj.ver1 and proj.ver2 directories, type:
    dircmp -d -s proj.ver1 proj.ver2
    Press Enter.

    The -s flag suppresses information about identical files. The -d flag displays a diff listing for each of the differing files found in both directories.

See the dircmp command in the AIX 5L Version 5.2 Commands Reference for the complete syntax.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]