The
tree structure of the Unix filesystem makes it easy to organize your
files. After you make and edit some files, you may want to copy or
move files from one directory to another, or rename files to
distinguish different versions of a file. You may want to create new
directories each time you start a different project. If you copy a
file, it’s worth learning about the subtle
sophistication of the cp
and
CpMac
commands: if you copy a file to a directory,
it automatically reuses the filename in the new location. This can
save lots of typing!
A directory tree can get cluttered with old files you don’t need. If you don’t need a file or a directory, delete it to free storage space on the disk. The following sections explain how to make and remove directories and files.
It’s handy to group related files in the same directory. If you were writing a spy novel, you probably wouldn’t want your intriguing files mixed with restaurant listings. You could create two directories: one for all the chapters in your novel (spy, for example), and another for restaurants (boston.dine).
To create a new directory, use the mkdir
program.
The syntax is:
mkdir dirname(s)
dirname
is the name of the new directory.
To make several directories, put a space between each directory name.
To continue our example, you would enter:
% mkdir spy boston.dine
If
you’re about to edit a file, you may want to save a
copy first. That makes it easy to get back the original version. You
should use the cp
program when copying plain files
and directories containing only plain files. Other files having
resource forks should be copied with CpMac
(available only if you have installed Apple’s Mac OS
X Developer Tools).
The cp
program can
put a copy of a file into the same directory or into another
directory. cp
doesn’t affect the
original file, so it’s a good way to keep an
identical backup of a file.
To copy a file, use the command:
cp old new
where old
is a pathname to the original
file and new
is the pathname you want for
the copy. For example, to copy the /etc/passwd
file into a file called password in your working
directory, you would enter:
% cp /etc/passwd password
%
You can also use the form:
cp old olddir
This puts a copy of the original file old
into an existing directory olddir
. The
copy will have the same filename as the original.
If there’s already a file with the same name as the
copy, cp
replaces the old file with your new copy.
This is handy when you want to replace an old copy with a newer
version, but it can cause trouble if you accidentally overwrite a
copy you wanted to keep. To be safe, use ls
to
list the directory before you make a copy there. Also, the Mac OS X
version of cp
has an
-i
(interactive) option that asks you before
overwriting an existing file.
You can copy more than one file at a time to a single directory by listing the pathname of each file you want copied, with the destination directory at the end of the command line. You can use relative or absolute pathnames (see Section 2.1) as well as simple filenames. For example, let’s say your working directory is /Users/carol (from the filesystem diagram in Figure 2-1). To copy three files called ch1, ch2, and ch3 from /Users/john to a subdirectory called work (that’s /Users/carol/work), enter:
% cp ../john/ch1 ../john/ch2 ../john/ch3 work
Or, you could use wildcards and let the shell find all the
appropriate files. This time, let’s add the
-i
option for safety:
%cp -i ../john/ch[1-3] work
cp: overwrite work/ch2?n
There is already a file named ch2 in the
work directory. When cp
asks,
answer n
to prevent copying
ch2. Answering y
would
overwrite the old ch2.
As you saw in Section 2.1.5.2, the shorthand
form .
puts the copy in the working directory, and
..
puts it in the parent directory. For example,
the following puts the copies into the working directory:
% cp ../john/ch[1-3] .
cp
can
also copy entire
directory trees. Use the option
-R
, for
“recursive.” There are two
arguments after the option: the pathname of the top-level directory
you want to copy from and the pathname of the place where you want
the top level of the copy to be. As an example,
let’s say that a new employee, Asha, has joined John
and Carol. She needs a copy of John’s
work directory in her own home directory. See
the filesystem diagram in Figure 2-1. Her home
directory is /Users/asha. If
Asha’s own work directory
doesn’t exist yet (important!), she could type the
following commands:
%cd /Users
%cp -R john/work asha/work
Or, from her home directory, she could have typed cp -R ../john/work
work
. Either way,
she’d now have a new subdirectory
/Users/asha/work with a copy of all files and
subdirectories from /Users/john/work.
If
you give cp
-R
the wrong
pathnames, it can copy a directory tree into itself — running
forever until your filesystem fills up!
If the copy seems to be taking a long time, stop
cp
with Control-Z, then explore the filesystem
(ls
-RF
is handy for this). If
all’s okay, you can resume the copying by putting
the cp
job in the background (with
bg
) so it can finish its slow work. Otherwise,
kill cp
and do some cleanup — probably with
rm
-r
, which we mention in
Section 3.4.5.2 later in this
chapter. (Also, see Section 9.1 and Section 9.3.)
If the copy is in the same directory as the original, the filenames must be different.
The system can’t find the file you want to copy. Check for a typing mistake. If a file isn’t in the working directory, be sure to use its pathname.
You may not have permission to copy a file created by someone else or
to copy it into a directory that does not belong to you. Use
ls -l
to find the owner and the permissions for
the file, or ls -ld
to check the directory. If you
feel that you should be able to copy a file, ask the
file’s owner or use sudo
(see
Section 2.3) to change its access modes.
The
cp
program works on plain files and
directories, but the Macintosh system stores applications with
resource information. These attributes are known as
resource forks
, and are used extensively in Classic Mac
OS applications and documents. (You will also find them in various
places on the Mac OS X filesystem). A file’s
resource fork, if it exists, can be seen by looking at a special file
called
filename/rsrc
.
For example, Microsoft Word has a resource fork:
%ls -l Microsoft Word
-rwxrwxrwx 1 bjepson admin 10568066 Sep 26 14:26 Microsoft Word %ls -l Microsoft Word/rsrc
-rwxrwxrwx 1 bjepson admin 2781434 Sep 26 14:26 Microsoft Word/rsrc
If you copy Microsoft Word with
cp
, it won’t copy the contents of
the resource fork (/tmp is a directory used to
hold temporary files):
%cp Microsoft Word /tmp
%ls -l /tmp/Microsoft Word
-rwxr-xr-x 1 bjepson wheel 10568066 Nov 10 14:35 /tmp/Microsoft Word %ls -l /tmp/Microsoft Word/rsrc
-rwxr-xr-x 1 bjepson wheel 0 Nov 10 14:35 /tmp/Microsoft Word/rsrc
A special version of cp
is used to copy files with
resource forks. The program,
CpMac
, is included with the Mac OS X
Developer Tools.
CpMac
is found in
/Developer/Tools
. To copy Microsoft
Word and its resources, invoke the following:
%/Developer/Tools/CpMac Microsoft Word /tmp
%ls -l /tmp/Microsoft Word
-rwxrwxrwx 1 bjepson wheel 10568066 Nov 10 14:37 /tmp/Microsoft Word %ls -l /tmp/Microsoft Word/rsrc
-rwxrwxrwx 1 bjepson wheel 2781434 Nov 10 14:37 /tmp/Microsoft Word/rsrc
In addition to resource forks, some files may include HFS metadata. A legacy of the earlier Mac OS, HFS metadata holds useful information about a file within the first several bytes of the file itself. The Mac OS X Finder will still make use of some of this data, including creator and type codes that, if a document doesn’t have a dot extension such as .mp3, dictate the file’s icon as well as which application should launch when you double-click it. A document file that loses this metadata might display only a generic icon, and the Finder wouldn’t know which application to launch it with.
To
rename a file, use mv
(move). The
mv
program can also move a file from one directory
to another.
The mv
command has the same syntax as the
cp
command:
mv old new
old
is the old name of the file and
new
is the new name. mv
will write over existing files, which is handy for updating old
versions of a file. If you don’t want to overwrite
an old file, be sure that the new name is unique. The Mac OS X
version of mv
has an -i
option
for safety:
%mv chap1 intro
%mv -i chap2 intro
mv: overwrite `intro'?n
%
The previous example changed the file named
chap1 to intro, and then
tried to do the same with chap2 (answering
n
aborted the last operation). If you list your
files with ls
, you will see that the filename
chap1 has disappeared, but
chap2 and intro are
preserved.
The mv
command can also move a file from one
directory to another. As with the cp
command, if
you want to keep the same filename, you only need to give
mv
the name of the destination directory.
There’s also a
MvMac
command, analogous to the
CpMac
command explained earlier. Again, check by
looking for a /rsrc
resource file before moving
and use MvMac
if needed.
If
you find yourself using MvMac
or
CpMac
a lot, it’d save you lots
of typing to add /Developer/Tools
to your
PATH
. PATH
is one of a set of
environment variables that help the shell keep track of your
particular session. Information on customizing your path is found in
Section 4.2.
If
your account has lots of files, organizing them into subdirectories
can help you find the files later. Sometimes you may not remember
which subdirectory has a file. The
find
program can search for files in many
ways; we’ll look at two.
Change to your home directory so find
will start
its search there. Then carefully enter one of the following two
find
commands. (The syntax is strange and
ugly — but find
does the job!)
%cd
%find . -type f -name "chap*" -print
./chap2 ./old/chap10b %find . -type f -mtime -2 -print
./work/to_do
The first command looks in your working directory
(.
) and all its subdirectories for files
(-type
f
) whose names start
with chap. (find
understands
wildcards in filenames.
Be sure to put quotes around any
filename pattern with a wildcard in it, as we did in the example.)
The second command looks for all files that have been created or
modified in the last two days (-mtime
-2
). The relative pathnames that
find
finds start with a dot
(./
), the name of the working directory, which you
can ignore. Worth noting is that -print
displays
the results on the screen, not on your printer.
Mac OS X has the 4.4 BSD Unix
locate
program to find files quickly. You can
use locate
to search part or all of a filesystem
for a file with a certain name.[4]
For instance, if you’re looking for a file named alpha-test, alphatest, or something like that, try this:
% locate alpha
/Users/alan/alpha3
/usr/local/projects/mega/alphatest
You’ll get the absolute pathnames of files and
directories with alpha in their names. (If you
get a lot of output, add a pipe to less
. See Section 6.2.3.) locate
may
or may not list protected, private files; its listings usually also
aren’t completely up to date. The fundamental
difference between the two is that find
lets you
search by file type, contents, and much more, while
locate
is a simple list of all filenames on the
system. To learn much more about find
and
locate
, read your online documentation (see Chapter 10) or read the chapter about them in
Unix Power Tools (O’Reilly).
You may have finished work on a file or directory and see no need to keep it, or the contents may be obsolete. Periodically removing unwanted files and directories frees storage space.
The rm
program
removes files. Unlike moving an item to the trash, no opportunity
exists to recover the item before you “empty the
trash” when using rm
.
The syntax is simple:
rm filename(s)
rm
removes the named files, as the following
example shows:
%ls
chap10 chap2 chap5 cold chap1a.old chap3.old chap6 haha chap1b chap4 chap7 oldjunk %rm *.old chap10
%ls
chap1b chap4 chap6 cold oldjunk chap2 chap5 chap7 haha %rm c*
%ls
haha oldjunk %
When you use wildcards with
rm
, be sure you’re deleting the
right files! If you accidentally remove a file you need, you
can’t recover it unless you have a copy in another
directory or in your backups.
Do not enter rm *
carelessly. It deletes all the files in your working
directory.
Here’s another easy mistake to make: you want to
enter a command such as rm c*
(remove all
filenames starting with “c”) but
instead enter rm c *
(remove the file named
c
and all files!).
It’s good practice to list the files with
ls
before you remove them. Or, if you use
rm
’s -i
(interactive) option, rm
asks you whether you want
to remove each file.
Just as you can create new directories,
you can remove them with the rmdir
program. As a
precaution, rmdir
won’t let you
delete directories that contain any files or subdirectories; the
directory must first be empty. (The rm -r
command
removes a directory and everything in it. It can be dangerous for
beginners, though.)
The syntax is:
rmdir dirname(s)
If a directory you try to remove does contain files, you get a message like “rmdir: dirname not empty.”
To delete a directory that contains some files:
Enter cd
dirname
to get
into the directory you want to delete.
Enter rm *
to remove all files in that directory.
Enter cd ..
to go to the parent directory.
Enter rmdir
dirname
to
remove the unwanted directory.
Use ls -a
to check that there are no hidden files (names that start with a
period) other than .
and ..
(the working directory and its parent). The following command is good
for cleaning up hidden files (which aren’t matched
by a simple wildcard such as *
). It matches all
hidden files except for . (the current
directory) and .. (the parent directory):
% rm -i .[^.]*
Chapter 7 includes Section 7.2, which explains ways to transfer files across a network — possibly to non-Unix operating systems. Mac OS X has the capability of connecting to a variety of different filesystems remotely, including Microsoft Windows, other Unix systems, and even Web-based filesystems.
If the Windows-format filesystem is
mounted with your other filesystems,
you’ll be able to use its files by typing a
Unix-like pathname. If you’ve mounted a remote
Windows system’s C: drive over
a share named winc, you can access the Windows
file C:WORDREPORT.DOC
through the pathname
/Volumes/winc/word/report.doc
. Indeed, most
external volumes are automatically mounted within the
/Volumes
directory.
[4] The script that
updates the locate
database is run only once a
week (late at night), and your computer must be on and running for
that to occur. If you want to update the database by hand, you can
use sudo
periodic weekly
.
3.135.247.188