Unix File system Commands: Directories

Now that the UNIX tree-structured file system has been introduced, it would be useful to know how to find our way through the system and how to create files, delete them and rename them.

cd: Change Directory

To move from one directory to another the cd command is used. Use as:

cd [directory]

directory may be an absolute, or a relative pathname. Or it may be nothing at all: if cd is used by itself, the user returns to his/her home directory.

An absolute—or full—pathname starts with /. In other words, the path taken to get to the directory is given from the top of the tree, through all the nodes, to the destination directory.

The relative pathname of the directory gives the route taken from the current directory, to the destination directory. Note that . (dot) means this current directory, .. (dot dot) means the previous directory, and that ~(tilde) means the user’s home directory. For example

cd ../testdir

means go up one directory, then down into the testdir directory at that node.

cd (by itself) is equivalent to cd ̃.

mkdir: Make Directory

To create a new directory, mkdir is used, as

mkdir [-p] directory

-p means create parent directories, if needed. directory may be an absolute or relative directory name.

The user must have write permission on the parent directory to create a new directory.

rmdir: Remove Directory

A directory can be removed with

rmdir directory

and again, directory may be an absolute or relative path name. The directory being deleted must be empty (i.e., cannot have any child directories, or files), and the user must have write permission.

pwd: Print Working Directory

The command pwd is used to display the full pathname of the current directory; the display is always in the absolute format. Example:

prompt> pwd
/usr/people/cantin/docs/unix

cp: Copy

To copy the contents of a directory into another directory, the following is used:

cp -r [-ip] directory1 directory2

or
cp -R [-ip] directory1 directory2 (not linux)
or
cp –recursive [-ipd] directory1 directory2 (linux only)
or
cp -r [-ipd] directory1 directory2 (linux only)

mv: Move

In UNIX, mv means move, or rename.

mv [-i] directory1 directory2

where directory1 and directory2 are directories.

This command will simply rename directory1 to directory2. If directory2 exists, the files of directory1 will be moved into directory2.

-i will prompt the user for confirmation if a file is to be overwritten.