If you’ve never used Unix before, this section will serve as a quick introduction to issuing Unix commands from the Terminal. Experienced Unix users can skip over this section. For each of the following, you will need to be using the Terminal application. The commands you need to type are shown in bold.
The instructions for using the
(described next) are then displayed one screen at a time. If there is
more than one screen for a command’s description,
you will see a percentage at the lower-left corner of the Terminal
window telling you how much of the manpage has been viewed. To scroll
to the next screen, hit the spacebar; you will be returned to the
command prompt when you’ve reached the end of the
man command even has its own
manpage, which can be viewed by using:
pwd/Users/chuck [macchuck:~] chuck%
tcsh command prompt will show you
what directory you’re in, but only to a point; for
pwd/Developer/Applications/Extras/Bluetooth [macchuck:Applications/Extras/Bluetooth] chuck%
As this example shows, at first it only looks like
Applications/Extras/Bluetooth, but issuing the
pwd command shows that I’m
cue that you are in a deeper path is that there is no slash before
the first directory in the prompt.
cd /Applications/Utilities[macchuck:/Applications/Utilities] chuck%
cd command followed by two dots:
cd ..[macchuck:/Applications] chuck%
cd command followed by a hyphen:
cd -[macchuck:/Applications/Utilities] chuck%
cd command with two dots and a slash
../ ) for each directory you want to go back.
For example, to go back two directories:
cd ../..[macchuck:/] chuck%
This is accomplished using the
By itself, the
ls command creates a horizontal
list of a directory’s contents. Add the
option to create a vertical list of a
directory’s contents, which also reveals more
details about the file, directory, or application (see Figure 4-2).
To list all the contents for a directory, including the dot files
(described earlier), add the -
a option (either
with or without the
l option) (see Figure 4-3).
When you issue a command like
ls -la, the
contents of some directories will scroll up, and you
won’t be able to see everything. One solution to
this is to just issue the command and then use the Terminal
window’s scrollbar to go back up. Or, more
|) the command to
more, which will display the contents of the
directory one screen at a time (see Figure 4-4).
more will be highlighted at the bottom
of the screen. To go to the next screen, hit the spacebar; continue
doing so until you find the item you’re looking for
or until you reach the end.
ls -l | colrm 1 49Acrobat Reader 5.0 Address Book.app AppleScript Art Directors Toolkit X 2.3 BBEdit 6.5 Backup.app Calculator.app Canon Utilities Chess.app Clock.app Cocoa Browser DVD Player.app . . .
The numbers following
49) are used by the
command to specify a range of columns to remove. (A column in the
Unix world is a single character. In this example, the column range
of 1 through 49—all the characters preceding the file or
directory name—are deleted.)
You can also use Control-L to clear the display, and if you want to reset the Terminal window, use
-K to clear the window’s scrollback.
rm -rf NewDirectory
Notice that the
rm -rf command will not prompt
you before it deletes everything in the
NewDirectory directory. You should use the
rm -rf command with extreme caution, as you
could delete something vital without even knowing it.
cp myfile.txt myfile2.txt
This makes a copy of
myfile.txt and creates
myfile2.txt within the same directory. If you
want to copy a file and place it in another directory, use the
cp myfile.txt Books/myfile.txt
This makes a copy of
myfile.txt and places that
copy in the
mv myfile.txt myFile.txt
This renames the file
myFile.txt in the same directory.
The following moves the file
myFile.txt to the
mv myFile.txt Books
cat myFile.txtThis is my file. I hope you like it. Chuck [macchuck:~/Books] chuck%
chmod =r myFile.txt[macchuck:~/Books] chuck%
chmod 444 myFile.txt[macchuck:~/Books] chuck%
chmod a-wx,a+r myFile.txt
chmod command has many options; for more
information, see its manpage (
man chmod ).
This zips up the file and places the
file in the same directory as the original file.
unzip -l myFile.zipArchive: myFile.zip Length Date Time Name ------ ---- ---- ---- 0 09-18-102 20:20 myFile.txt ------ ------- 0 1 file
This shows that there is one file in
myFile.txt), along with the size of the file
(in kilobytes), and the date and time that file was created.
To unzip a file or directory, use the
command, as follows:
This unzips the file and places its contents in the current directory. If a file with the same name is already in that directory, you will be asked what to do:
unzip myFile.zipArchive: myFile.zip replace myFile.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:
You will be given the following options to replace the existing file(s):
y for yes
n for no
A to replace all the files with similar names
N to not replace any of
r to rename the like-named file that already
If you choose to rename the existing file (as shown in the example),
you will be prompted to enter a new name for that file; enter a
filename and that file’s name will be changed and
unzip command will extract the Zip file.
tar cvfz myFile.tar.gz[RETURN]
The options used are as follows:
Creates a new archive.
Verbose; this option prints the filenames onscreen as files that are added to or extracted from the archive.
Stores files in or extract files from an archive.
gzip to zip, or compress, the archive.
To peek inside a tarball to see the files it contains, use the
tar command with the
tar tvfz myFile.tar.gz-rw-r--r-- 1 chuck staff 44 Oct 05 21:10 myFile.txt
t option is used to print the names of the
files inside the tarball.
tar xvf myFile.tar[RETURN]
x option is used to extract the contents of
the tarball. This command unpacks the tarball and places its contents
in the file
If you receive a
.tar.gz) file, that means the tarball has been
gzip. To decompress that file,
use the following command:
tar xvfz myFile.tgz[RETURN]
z option tells the tar
command that the file it will decompress has been
Some commands require you to be the
before they can be issued. Rather than logging out and then logging
back in as
root, you can issue the
followed by the superuser’s password:
exitexit [macchuck:~] chuck%
For more information about using the Unix side of Mac OS X, pick up a
copy of Learning Unix for Mac OS X
(O’Reilly). To learn more about the
tcsh shell, pick up a copy of Using
csh & tcsh (O’Reilly).