[Next] [Previous] [Up] [Top] [Contents] [Index]

Chapter 6: The UNIX File System

6.5 Manipulating Directories

This section describes the commands you can use to organize and use the UNIX directory structure. It describes how to make and remove directories, and move from one directory to another. Listing the contents of a directory (files and subdirectories) was described in a previous section. Section 6.6.2 explains the meaning of access permissions as applied to directories.

6.5.1 Print Working Directory: pwd

The pwd command (for print working directory) displays the path name of your working (current) directory. The command format is:

% pwd 

6.5.2 List Directory Contents: ls

The ls command, which stands for list, is used to list the contents of a directory. ls has many options, some of which are system-dependent. A few of them are described in section 6.3.1. For a complete description of the command, refer to the man pages for ls.

6.5.3 Change Directory: cd

When you first log in to the system, you are placed in your home directory, which is then also your current working directory. You can use the cd command (for change directory) to change your current working directory. The command format is:

% cd [directory] 

You can specify a complete path or a relative path. You can use .. (for the parent directory) in your pathname. You must have execute permission (which provides search permission in this case) on a directory to cd to it.

If directory is not specified, you are returned to your home directory.

The following examples illustrate moving to different directories:

% cd
% cd Tools 
% cd /usr/jones 
% cd ~jones/ourfiles 
% cd ../Tools 

6.5.4 Make a Directory: mkdir

The mkdir command (for make directory) is used to create a directory. The command format is:

% mkdir dirname ... 

If a pathname is not specified, the directory is created as a subdirectory of the current working directory. Directory creation requires write access to the parent directory. The owner ID and group ID of the new directory are set to those of the creating process.

Examples:

% mkdir progs 
% mkdir /usr/nicholls/Tools/Less 

6.5.5 Copy a Directory

The most straightforward way of copying a directory and its contents is to pipe the output of the ls command (see section 6.3.1) into the file copy facility cpio (see the man pages).

First, create the destination directory using mkdir (see section 6.5.4), if it doesn't already exist. Secondly, from the source directory, run the command (shown with recommended options; see man pages for option information):

% ls | cpio -dumpV destination_dir

The destination_dir must be specified relative to the source directory.

Another way to copy directory hierarchies is to use the tar utility, described in section 6.3.7. The following sequence of commands copies a structure from the source directory to the destination directory:

% cd source_dir; tar cf - . | (cd destination_dir; tar xfBp -)

The "-" is used for the name of the tar file (argument to the f option) so that tar writes to the standard output or reads from the standard input, as appropriate.

6.5.6 Move (Rename) a Directory: mv or mvdir

See section 6.3.4 for information on mv. To move a directory (olddirname) and its contents to a different position in the directory tree, use the command format:

% mvdir olddirname newdirpath

If newdirpath exists already, then the directory gets moved to newdirpath/olddirname. Note that the two arguments cannot be in the same path. For example:

% mvdir x/y x/z

is ok, but

% mvdir x/y x/y/z

is not ok.

6.5.7 Remove a Directory: rmdir

You can remove a directory with the rmdir command. The directory must contain no files or subdirectories, and you must have write permission to the parent directory.

% rmdir dirname ... 

You can use an absolute or relative pathname.

[Missing image]You can also use rm -r as described in Section 6.3.6. rm -r will delete a directory, all subdirectories, and all files. This command should be used with extreme caution.

For example, the following command deletes the directory temp, all subdirectories of temp and all files contained in those directories, prompting before each removal, and confirming removal of write-protected files (-i):

% rm -ir /usr/jones/temp 

UNIX at Fermilab - 10 Apr 1998

[Next] [Previous] [Up] [Top] [Contents] [Index]