<<

. 7
( 69 .)



>>

Example:
drwxr-xr-x 5 user staff 170 Nov 4 13:49 Sites

This directory can be read by the owner, the members of the
group staff, and anyone else using the computer. Only the
owner can write to the directory; however, anyone can move from
directory to this directory.




– The shell displays the file ¤ Type a directory name
DISPLAY FILE ATTRIBUTES SHOW THE ATTRIBUTES
OF A DIRECTORY
attributes. and press Return.
„ Type ls -l and a space.
„ Type ls -ld and a space. – The shell displays the
¤ Type a filename and press directory attributes.
Return.




19
UNIX FOR MAC




COPY A FILE
If copy-file is an existing directory, the copy will be

Y
ou can copy files with the cp command. This
placed in that directory and will be named the same as the
command allows you to save backups of important
original-file.
files or keep old versions of files while you edit them.
Making copies of important files before editing them is a
Copying a file can be dangerous if the file already exists
good practice because it assures that you can go back to
with the same name you specify for the copy. In such a
the original.
case, you write over the existing file with the new copy.
The cp command does not automatically warn you, but
To copy a file, you supply two arguments to the cp
copies the file, assuming that you want to overwrite the
command: the first is the name of the original file, and the
existing file.
second is the name of the new file. You do not have to own
the original file to copy it, but you must have permission to
To ensure that you do not accidentally write over a file, you
read it. You also need write permission in the directory to
can use the -i option with the cp command, so that it asks
where you make the copy. Your cp command should look
you to confirm that you want to write over the existing file.
like this:
If you do, you can type y for yes and press Return; if you do
not, you can press any key to cancel the process.
cp original-file copy-file



COPY A FILE




‹ Type the name of the copy ¤ Type your response and
CREATE A COPY COPY A FILE SAFELY
and press Return. press Return.
„ Type cp and a space. „ Type cp -i original-file and
– The shell copies the file. – The cp command either
press Return.
¤ Type the name of the copies the file or abandons
– The shell asks you if you
original file, and a space.
the operation, depending on
want to write over the file.
your response.


20
2
WORK WITH FILES




DESIGNATE FILES BY PATHNAME
hierarchy of folders containing files and other folders. Each

Y
ou can include a pathname to indicate a location
of these folders corresponds to a directory. You can view
outside of your current directory when you give a
the folders in the Mac OS X Finder application, or access
filename as an argument to a command such as cp.
them as directories through the Unix shell.
The file system of your Mac OS X computer is set up as a


USING PATHNAMES

A pathname specifies the place in which you can find a the Users directory, in the Documents subdirectory of
file, just as an address tells you where to find someone in the kynn directory, and that it has the filename Chapter
the physical world. For example, one might describe his 02.doc. You can write that pathname as /Users/
current location by saying that he is on the planet Earth, kynn/Documents/"Chapter 02.doc".
in the country known as the United States of America, in
When writing the pathname, you must separate each
the state of California, in the county of Orange, within
directory name with a forward slash (/). In addition,
the city of Fullerton, on Wilshire Avenue, in building
because the filename contains a space, you must enclose
number 110, on the Garden Level, in suite G-1, in the
it in quotation marks to refer to it in Unix.
back office, at the desk of Kynn.

A file, on the other hand, might say that it is on a hard
drive, in the Users directory, in the kynn subdirectory of

Relative and Full Pathnames
The desk of Kynn is in an office that also contains the The start of the Unix file system is the root directory,
desk of Laura. If you are going to describe the location and you indicate it by an initial forward slash (/). A
of Laura, you can just say the desk of Laura and not pathname beginning with a forward slash is therefore a
spell out everything starting with the planet Earth. This full pathname, and is relative only to that root directory.
is a location relative to the desk belonging to Kynn. In Starting the pathname with the root directory is like
the same way, you do not need to specify a full starting with the planet Earth when telling someone
pathname if you are already in the same directory. For where you are; the root directory corresponds to the
example, if you are working with files in the Macintosh HD icon on your desktop in Finder. You use
/Users/kynn directory and you want to copy the full pathnames to reference files that are outside of the
Chapter 02.doc file described above, you do not directory in which you are currently working. For
need to type the full pathname, only this: example, to copy a file in the /usr/share/dict directory
to your own directory, you can type:
[ferro:˜] user% cp Documents/"Chapter
02.doc" backup-ch2.doc [ferro:˜] user% cp /usr/share/dict/
propernames name-list.txt
This creates a copy of the file with the new name
backup-ch2.doc. Note that this is in the /Users/
kynn directory, not in the Documents directory.

Aliases for Special Pathnames
Three pathnames refer to special directories. The alias directory. The alias tilde (˜) refers to your Home
dot (.) refers to the current directory in which you are directory, where you store your personal files. If you
working. The alias dot-dot (..) refers to the parent give the name of another user immediately after the
directory ” the directory which contains the current tilde, then it refers to the Home directory of that user.



21
UNIX FOR MAC




RENAME A FILE
To avoid this problem, you can use the -i option with the

Y
ou can change the name of a file with the mv
mv command. If moving your file will overwrite another file,
command. In Aqua, renaming files and moving files
the shell asks you to confirm that you want to proceed. If
are two different functions. You move a file in Aqua
you do, you can type y for yes, or any other key for no. To
by dragging it to a new location, and you rename it by
be safe, you should make a habit of using the -i option
clicking the name field of the file and typing a new name.
with the mv command. You can also set up a shell alias to
However, within Unix, when you rename a file, you are
automatically use the -i option with the mv command.
simply moving the file to a new place within the file system;
even if you are within the same directory, you are moving
You can use the mv command to move a file from one
the file to a new name.
location to another. When you move a file with a command
such as mv file /directory, the resulting file retains its
However, if you move a file to another file that already
original name, but resides in a different directory. It is only
exists, you write over that second file. This is not a good
when you move a file to both a new location and filename
practice unless you are certain that you want to overwrite
that you rename the file.
the second file.


RENAME A FILE




‹ Type the new filename and ¤ Type your response and
RENAME A FILE RENAME A FILE SAFELY
press Return. press Return.
„ Type mv and a space. „ Type mv -i oldname
– The shell renames the file. – The mv command either
newname and press Return.
¤ Type the current filename renames the file or abandons
– The shell asks you if you
and a space.
the operation, depending on
want to overwrite the file.
your response.




22
2
WORK WITH FILES




DELETE A FILE
By default, the rm command only removes ordinary files,

Y
ou can use the rm command to delete files you no
although you can remove an empty directory with the
longer want to keep. When you delete a file using
rmdir command; for a directory containing files, you can
the Mac OS X Finder in Aqua, the Finder does not
use the rm “r command. You cannot remove directories
immediately delete the file, but stores it in the Trash.
unless they belong to you or you have write permission to
However, when you delete a file using the rm command,
the files. However, there is no special access permission
the file completely and instantly disappears. This means that
required to delete a file.
if you mistakenly delete a file from the shell, you cannot
recover it by simply dragging it back out of the Trash.
It is possible to delete a file whose name begins with a
dash, such as -foo or -bar. These are extremely hard to
As with the cp and mv commands, deleting files can be
remove, because the rm command tries to interpret the
dangerous because people can make mistakes. You can use
filenames as an option, because options begin with dashes.
the -i option to tell the rm command to confirm that you
In this situation, you can use the option -- (a double dash).
want to delete a file; if you do, you can type y for yes, and
This means that the command does not check the rest of
press Return. If you do not, you can press any other key or
the line for more options, making this your last option flag.
simply the Return key.

DELETE A FILE




¤ Type the name of the file ‹ Type your response and
DELETE A FILE DELETE A FILE SAFELY
and press Return. press Return.
„ Type rm and a space. „ Type rm -i and a space.
– The shell deletes the file. – The rm command either
¤ Type the name of the file deletes the file or abandons
and press Return.
the operation, depending on
– The shell asks you if you your response.
want to delete the file.



23
UNIX FOR MAC




<<

. 7
( 69 .)



>>