This chapter shows you how to manage directories on your system. After completing this chapter, you will be able to:
Create directories
Change directories
Display, copy, and rename directories
Remove directories
To learn about managing directories, try the examples in this chapter. You should perform each example in sequence so that the information on your screen is consistent with the information shown in this chapter.
Before you can do the examples, you must be logged in and your login directory should contain the following:
The files
file1
,
file2
,
file3
,
record1
, and
record6
The subdirectory,
reports
, that contains
the files
file1
,
file2
,
file3
, and
notes
The empty subdirectory
project
If you are using files with different names, make the appropriate substitutions
as you work through the examples.
Use the
ls
command, which
is explained in
Chapter 3
with the
-R
and
-F
flags as described in
Table 3-1
to produce a
listing of the files in your current directory.
Your screen should look similar
to the following:
$
ls -RF
file1 file2 file3 project/ record1 record6 reports/ ./project: ./reports: file1 file2 file3 notes $
4.1 Creating a Directory (mkdir)
Directories let you organize individual files into useful groups.
For
example, you could put all the sections of a report in a directory named
reports
, or the data and programs you use in cost estimating in
a directory named
estimate
.
A directory can contain files,
other directories, or both.
Your login directory was created for you when your computer account
was established.
However, you probably will need additional directories to
organize the files you create while working with the operating system.
You
create new directories with the
mkdir
(make directory)
command.
The format of the
mkdir
command is:
mkdir
dirname
The dirname entry is the name you want to assign to the new directory.
The system creates dirname as a subdirectory of your working directory. This means that the new directory is located at the next level below your current directory.
In the following example, return to your login directory by entering
the
cd
command, and create a directory named
project2
:
$
cd
$
mkdir project2
$
Now, create a subdirectory in
the
reports
directory by entering a relative pathname:
$
mkdir reports/status
$
Figure 4-1
shows the new file system tree structure.
The
project
,
project2
, and
reports
directories are located one level below your login directory, and the
status
subdirectory is located one level below the
reports
directory.
Figure 4-1: Relationship Between Directories and Subdirectories
Like file names, the maximum length of a directory name depends upon the file system used on your computer. For example, your file system may allow a maximum directory name length of 255 bytes (the default), or it may allow a maximum directory name length of only 14 bytes. Knowing the maximum directory name length is important to help you give meaningful names to your directories. See your system administrator for details.
The operating system does not have a symbol or notation that automatically
distinguishes between a file name and a directory name, so you may find it
useful to establish your own naming conventions to designate files and directories.
However, you can use the
ls -F
command to distinguish
between file names and directory names when the contents of your current directory
are displayed.
For more information on this command, see
Section 4.3.
4.2 Changing Directories (cd)
The
cd
(change directory) command lets you switch
from your current (working) directory to another directory.
You can move to
any directory in the file system from any other directory in the file system
by executing
cd
with the proper pathname.
Note
You must have execute permission to access a directory before you can use the
cd
command. For information about directory permissions, see Chapter 5.
The format of the
cd
command is:
cd
pathname
The pathname entry can either be the full pathname or the relative pathname of the directory that you want to set as your current directory.
If you enter the
cd
command without a pathname, the
system returns you to your login directory (also known as your HOME directory).
To check the name of and display the path for your current directory,
enter the
pwd
(print working directory) command.
See
Chapter 2
for information about the
pwd
command.
4.2.1 Changing Your Current Directory
In the following example, you enter the
pwd
command
to display the name (which is also the pathname) of your working directory.
You then use the
cd
command to change your current directory.
First return to your login directory, if necessary, by entering the
cd
command without a pathname.
Next, enter the
pwd
command to verify that your login directory is your current directory.
Remember
that the system substitutes the name of your login directory for the notation
/u/uname
in the example:
$
cd
$
pwd
/u/uname $
Enter the
cd
command with the relative pathname
project2
to change to the
project2
directory:
$
cd project2
$
Enter
pwd
again to verify that
project2
is the current directory.
Then, enter
cd
to return to your login directory:
$
pwd
/u/uname/project2
$
cd
$
To change your current directory to the
status
directory,
which is a different branch of the file system tree structure, enter the
cd
command with a full pathname:
$
cd reports/status
$
pwd
/u/uname/reports/status $
4.2.2 Using Relative Pathname Notation
You can use the following relative pathname notation to change directories quickly:
Dot notation (.
and
..
)
Tilde notation ( ~
)
Every directory contains at least two entries that are represented by
dot (.
) and dot dot (..
).
These entries
refer to directories relative to the current directory:
.
)This entry refers to the current directory.
..
)This entry refers to the parent directory of your working directory. The parent directory is the directory immediately above the current directory in the file system tree structure.
To display the
.
and
..
entries
as well as any files beginning with a period, use the
-a
flag with the
ls
command.
In the following example, change to the
reports
directory
by changing first to your login directory and then to the
reports
directory:
$
cd
$
cd reports
$
The
ls
command
displays the directory contents as well as the
status
subdirectory
you created earlier:
$
ls
file1 file2 file3 notes status $
Now, execute the
ls -a
command to list all directory entries as well as those that begin with a dot
(.
) - the relative directory names:
$
ls -a
./ ../ file1 file2 file3 notes status $
You can use the relative directory name dot dot
(..
) to refer to files and directories located above the
current directory in the file system tree structure.
That is, if you want
to move up the directory tree one level, you can use the relative directory
name for the parent directory rather than using the full pathname.
In the following example, the
cd ..
command changes
the current directory from
reports
to your login directory,
which is the parent directory of
reports
.
Remember that
the
/u/uname
entry represents your login directory.
$
pwd
/u/uname/reports
$
cd ..
$
pwd
/u/uname $
To move up the directory structure more than one
level, you can use a series of relative directory names, as shown in the following
example.
The response to the following
pwd
command, the
slash ( / ) entry, represents the root directory.
$
cd ../..
$
pwd
/ $
In the Korn or POSIX shell and the C shell you may use a tilde ( ~
) to specify a user's login directory.
For example, to specify
your own login directory, use the tilde alone as follows:
$
cd ~
The above tilde notation
does not save you keystrokes because in all operating system shells you may
get the same results by merely entering
cd
from any place
in the file system.
However, if you want to access a directory below your login directory,
tilde notation can save you keystrokes.
For example, to access the
reports
directory from anywhere in the file system, enter the following:
$
cd ~/reports
Tilde notation is also very useful when you want to access a file or directory either in or below another user's login directory. You may not know the precise location of that user's login directory, but assuming you have the appropriate permissions, you could get there with a minimum of keystrokes.
For example, from any place in the file system, you could specify the
login directory of a hypothetical user
jones
by entering
the following:
$
cd ~jones
In addition, if user
jones
tells you that you can find a file in the
status
directory
immediately below the login directory, you can access the directory by entering
the following:
$
cd ~jones/status
4.2.3 Accessing Directories Through Symbolic Links
When directories are connected through a symbolic link, the parent directory
you access with the
cd
command differs depending upon whether
you are specifying the actual directory name or the relative directory name.
In particular, using the full pathname to find the parent of a symbolically
linked directory results in accessing the actual parent directory.
For example, suppose
user2
is working on a file in
the
/u/user2/project
directory, which is the symbolic link
to
/u/user1/project
.
To change to the actual parent directory
(/u/user2
),
user2
types the following:
$
cd /u/user2
$
pwd
/u/user2
$
If
user2
specified the relative directory name (..),
the parent directory of the symbolic link would be accessed.
For example,
suppose
user2
is working on the same file in the
/u/user2/project
directory, which is the symbolic link to
/u/user1/project
.
To access the parent directory of the symbolic
link,
user2
enters the following:
$
cd ..
$
pwd
/u/user1
$
Instead of being in the
/u/user2
directory,
user2
is now in the directory
called
/u/user1
.
For background information on symbolic links, see
Section 3.4.
4.3 Displaying Directories (ls -F)
A directory can contain subdirectories as well as files.
To display
subdirectories, use the
ls -F
command.
This command
displays the contents of the current directory and marks each directory with
a trailing slash character ( / ) so that it readily can be distinguished
from a file.
The format of the
ls -F
command is:
ls
-F
In the following example, return to your login directory and
enter the
ls -F
command to display the directory
contents.
The
project
,
project2
, and
reports
directories are marked with a slash:
$
cd
$
ls -F
file1 file3 project2/ record6
file2 project/ record1 reports/
$
Some Korn or POSIX shell, and C shell
users define an alias for the
ls
command so that whenever
they enter
ls
, the
ls -F
command
is executed.
For more information about defining aliases, see
Chapter 8.
You can use the
cp
command with the
-r
flag to recursively copy directories and directory trees to another
part of the file system.
The
cp -r
command has the
following format:
cp
-r sourcedestination
The source entry is the name of the directory to be copied. The destination entry is the name of the directory location to which you want to copy source.
Figure 4-2
shows how the
cp -r
command in the following example copies the directory tree
reports
into the directory
project
.
It is assumed that
the command is entered from the login directory:
$
cp -r reports project
$
Figure 4-2: Copying a Directory Tree
The
reports
directory files,
file1
,
file2
,
file3
, and
notes
, as
well as the
status
subdirectory, have been copied to
project
.
You can use the
mv
command to rename a directory
only
when that directory is contained in the same disk partition.
The format of the
mv
command is:
mv
olddirectoryname newdirectoryname
The olddirectoryname entry is the name of the directory you want to move or rename. The newdirectoryname entry is the new name you want to assign to the original directory name.
In the following example, first change to the
reports
directory.
Then, enter
ls -i -d
command to
list the file serial number for the
status
directory:
$
cd reports
$
ls -i -d status
1091 status
$
Now, enter the
mv
command to change the name of
status
to
newstatus
.
Then, list the file serial number for the
newstatus
directory:
$
mv status newstatus
$
ls -i -d newstatus
1091 newstatus
$
The second
ls -i -d
command does not list the original directory name
status
.
However, it does list the new directory name,
newstatus
, and displays the same file serial number (1091 in this example) for the
new directory as for the original
status
directory.
4.6 Removing Directories (rmdir)
When you no longer need a particular directory, you can remove it from
the file system with the
rmdir
(remove directory) command.
This command removes only empty directories - those that contain no
files or subdirectories.
For information about removing files from directories,
see
Section 4.6.4
and
Section 3.9.
The format of the
rmdir
command is:
rmdir
dirname
The dirname entry is the name, or pathname, of the directory you want to remove.
Before working through the examples in the following sections, create
three subdirectories in the directory
project2
.
First, use the
cd project2
command to set
project2
as your current directory.
Next, use the
mkdir
command to create the
schedule
,
tasks
, and
costs
directories.
Then, list the contents
of the
project2
directory:
$
cd project2
$
mkdir costs schedule tasks
$
ls -F
costs/ schedule/ tasks/
$
Finally, use the
cd
command to return to your login directory:
$
cd
$
pwd
/u/uname
$
4.6.1 Removing Empty Directories
The
rmdir
command removes only empty directories.
If you try to remove a directory that contains any files or subdirectories,
the
rmdir
command displays an error message, as the following
example shows:
$
rmdir project2
rmdir: project2 not empty
$
Note
You cannot remove a directory while you are positioned in it. To remove a directory, you must be elsewhere in the directory tree. See Section 4.6.3 for more information.
Before you can remove the directory
project2
, you
must first remove the contents of that directory.
In the following example,
the
cd
command makes
project2
your current
directory, and the
ls -F
command lists the contents of
project2
:
$
cd project2
$
ls -F
costs/ schedule/ tasks/
Now remove
the directory
schedule
from the current directory, and
then list the remaining contents of the
project2
directory:
$
rmdir schedule
$
ls -F
costs/ tasks/
$
The
project2
directory still contains two subdirectories:
costs
and
tasks
.
You can remove them by using
pattern-matching characters, as described in the next section.
Once these
subdirectories are removed, you can delete the
project2
directory, as described in
Section 4.6.3.
4.6.2 Removing Multiple Directories
You can remove more than one directory at a time with the
rmdir
command by using pattern-matching characters.
See
Chapter 2
for detailed information about pattern-matching characters.
For example, suppose that you are in the
project2
directory and want to remove two subdirectories:
costs
and
tasks
.
To do so, enter the
rmdir *s?s
command.
Then, enter the
ls
command to verify that the
project2
directory contains no entries:
$
rmdir *s?s
$
ls
$
Caution
Entering the
rmdir
command with the asterisk (*) character alone removes ALL empty directories from your current directory. Use the asterisk (*) pattern-matching character with care.
4.6.3 Removing Your Current Directory
You cannot remove your current directory while you are still working
in it.
You can remove it only after you move into another directory.
You generally
enter the dot dot (..
) command to move into the parent
directory of your current directory, and then enter
rmdir
with the pathname of the target directory.
The directory
project2
is empty.
To remove
project2
, first move to your login directory, which is the parent
directory of
project2
.
Then, use the
rmdir
dirname
command to remove
project2
, and enter
ls
to confirm the removal:
$
cd
$
rmdir project2
$
ls
file1 file2 file3 project/ record1 record6 reports/
$
Your login directory no longer contains the
project2
directory.
4.6.4 Removing Files and Directories Simultaneously (rm -r)
The
rmdir
command removes only directories, not files.
You can, however, remove files and directories at the same time by using the
rm
command with the
-r
(recursive) flag.
The
rm -r
command first deletes the files from
a directory and then deletes the directory itself.
It deletes the directory
you specify as well as any subdirectories (and the files they contain) below
it on the directory tree.
This command should be used with caution.
The format of the
rm -r
command is:
rm
-r pathname
The pathname entry can either be the full pathname or the relative pathname of the directory that you want to remove. You may also use pattern-matching characters to specify files.
Caution
Be certain that you understand how the
-r
flag works before you use it. For example, entering therm -r *
command from your login directory deletes all files and directories to which you have access. If you have superuser authority and are in the root directory, this command will delete all system files. See Section 5.7 for more information about superuser authority.
When using the
rm -r
command to remove files
or directories, it is a good idea to include the
-i
flag in the command line:
rm
-ri pathname
When you enter the command in this form, the system prompts
you for verification before actually removing the specified items.
In this
way, by answering
y
(yes) or
n
(no)
in response to the prompt, you control the actual removal of a file or directory.
Keep in mind that using the
-ri
option may require
you to reply to many, many prompts (depending upon how many files you have).