<<

. 10
( 69 .)



>>

stuck in an unresponsive non-directory where you cannot
the directory, or directories, that you want to remove. If that
even use the ls command to list the directory contents.
directory is empty, then the command removes it. If the
You can use the cd command, with no arguments, to return
directory is not empty, then a message appears, stating that
to your Home directory.
you cannot remove the directory.

In such a case, you can go into the directory and remove
any existing files using the rm command, or any

DELETE A DIRECTORY




¤ Type the name of an ‹ Type rm -i * and press
DELETE A DIRECTORY DELETE A NON-EMPTY
DIRECTORY
empty directory and press Return.
„ Type rmdir and a space.
„ Type cd, a space, and the
Return.
› Type y and press Return
name of the non-empty
‹ Type ls “F and press for each item that you want
directory.
Return to see the results. to delete from the directory.
¤ Press Return. ˇ Type ls “F and press
Return to confirm that the
Note: You may want to use the ls
directory is empty.
command to view the contents of
the directory before you delete it. – The directory is empty.
34
3
WORK WITH DIRECTORIES

There is another way to remove an entire directory and its
contents, hidden or otherwise, using the rm command. The
-r option instructs the rm command to remove a directory
and everything within it; the -f option instructs the rm
command not to ask you about each deletion.
Although you can use the rm -rf command to delete an
entire directory at once, you should be very careful about
doing so, as a mistyped command could very easily wipe
out important files, with no way to recover them.

Example:
[ferro:˜] user% rm -rf Install/
[ferro:˜] user% rm -rf ˜/ Extra
[ferro:˜] user% ls
ls: .: Permission denied

Note that in the second line above, the user probably
means to type ˜/Extra, but accidentally puts a space before
the word Extra. This is a costly mistake, as it means that the
rm command deletes the Home directory of the user.




Á Type cd .. and press ° Type the name of the › Repeat step 3 for each
DELETE HIDDEN FILES
PREVENTING DIRECTORY
Return. directory and press Return. hidden file.
DELETION
– Your current directory – The shell deletes the empty ˇ Type cd .. to move to the
„ Type cd, a space, and the
contains the now-empty directory. parent directory.
name of the directory, and
directory.
Á Type rmdir, a space, and
press Return.
Note: You may need to find and
‡ Type rmdir and a space. delete hidden files if you see an error the name of the directory you
¤ Type ls -A and press
that says the directory is not yet want to delete.
Return.
empty.
– The shell deletes the
‹ Type rm, a space, and the directory and its contents.
name of a hidden file.
35
UNIX FOR MAC




MOVE FILES INTO A DIRECTORY
You can move multiple files using the mv command,

Y
ou can move files into or out of a directory by using
however, they must all go into the same directory. To do
the mv command. This is the same as dragging a file
this, you can simply type out each filename and specify the
in Finder to a new folder location. The mv command
destination as the final argument, as follows:
can rename a directory just as it renames a normal file, but
it can also move the location of a file or directory within the
mv fileOne fileTwo fileThree andSoOn
file structure. You can use the mv command to move a file,
location
or directory, by typing a command like this:
This command moves all of the files that you list to the
mv file location
location that you specify. If that location does not exist as a
directory, an error message appears, and the command
The argument location must be a directory. Either this
does not execute.
directory exists and is owned by you, or you must have
permission to write files in the directory. If you type the
Using the -i option with the mv command can prevent you
name of a directory that does not exist, you will not see an
from accidentally overwriting files when you move them.
error; the file will be renamed to the second name you
typed. This can also happen if you mistype the name of the
directory.


MOVE FILES INTO A DIRECTORY




‹ Type the name of the › Type ls “F and a space
MOVE A FILE INTO A DIRECTORY MOVE MULTIPLE FILES
INTO A DIRECTORY
destination directory and followed by the name of the
„ Type mv and a space.
„ Type mv and a space.
press Return. directory, and press Return.
¤ Type the name of a file – The shell shows the files in
› Type ls “F and a space, ¤ Type the names of the
and a space.
followed by the name of the the new directory.
files separated by spaces.
directory, and press Return.
Note: Use quotes around the
‹ Type the name of the
filename only if it contains spaces.
– The shell shows the file in destination directory and
the new directory. press Return.
36
3
WORK WITH DIRECTORIES




COPY A DIRECTORY
However, if you try to copy a directory as you would copy a

Y
ou can use the cp command to make a copy of a
file, you see an error message because directories are not
directory by using the -R option. Individual files can
normal files:
be copied using the cp command with no options,
and as with the mv command, you can give a list of files and
[ferro:˜] user% cp Documents "Backup of Docs"
a destination directory in order to copy a number of files
cp: Documents is a directory (not copied).
into the same directory:
To copy a directory, you must use the -R option, which tells
cp fileOne fileTwo fileThree andSoOn location
the cp command to copy the directory and its contents to
the new location. For example, to create a copy of the
You can use wildcards to copy files. For example, to copy all
Documents directory, you can type cp -R Documents
of your .doc files into your Documents directory, type cp
"Backup of Docs".
*.doc ˜/Documents.
This will copy the entire directory at once, creating the new
When you copy files into a directory, the new versions of
directory and duplicating all of the files. The names of the
each file will be named with their original names. The
original files will be the same, although the directory names
directory needs to exist, as the cp command will not create
will be different.
a new directory for you. You can use the mkdir command
to create the directory first.

COPY A DIRECTORY




– The new directory is a
„ Type cp -R and a space. ‹ Type the name of the new › Type ls “F and a space
copy of the directory and followed by the name of the duplicate of the original
¤ Type the name of the press Return. original directory, and press directory.
original directory and a Return.
space.
ˇ Repeat the ls “F
command in step 4 to show
the contents of the new
directory.
37
UNIX FOR MAC




DETERMINE DIRECTORY SIZE
directories or files. Additionally, the space used by all

Y
ou can use the du command to display the size and
subdirectories also appears, along with subdirectories of
contents of a directory. The name of the du
subdirectories. This can make the output of the du
command stands for disk usage, and it tells you how
command quite extensive and hard to read; if you want a
much space each file or directory uses on your hard drive.
single answer to summarize the size of a directory, you can
This information is also available from the Finder using the
use the option -s with the du command.
Get Info menu option. When you use the ls -l command,
you see a value for the size of the directory just as you do
When the du command displays sizes for files and
for other files. However, this value does not represent the
directories, it measures these sizes in disk blocks; a block
size of the contents of the directory; it represents the size
on a Unix file system disk represents 512 characters. This is
of the directory entry itself, a list of the files stored in the
an easy number for computers to work with but somewhat
directory. To obtain the size of the contents of the directory,
difficult for us humans to grasp. You can use the option -k
including subdirectories, you can use the du command.
to make the du command list sizes in kilobytes, which are
units of 1,024 bytes.
If you type du alone, you get a listing of disk usage in the
current working directory; if you give one or more
arguments, you see the space taken up by each of those



DETERMINE DIRECTORY SIZE




– The shell displays a list of – The final number in the list
LIST DIRECTORY SIZES
directory sizes. is the total disk usage for your
„ Type du and press Return. current directory, in blocks.




38
3
WORK WITH DIRECTORIES



You can use the df command to check the total disk usage on
your computer. Like du, df measures sizes in 512-character blocks,
but you can use the -k option to make the results easier to read.
The df -k command lists all file systems on your computer, and
gives you a percentage indicating how full your disk is.

Example:
[ferro:˜] user% df -k
Filesystem 1K-blocks Used Avail Capacity Mounted on
/dev/disk0s5 5865644 4027644 1779344 69% /
devfs 90 90 0 100% /dev

<<

. 10
( 69 .)



>>