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.
- View a command’s description and its options?
The instructions for using the
pwdcommand (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 manpage. The
mancommand even has its own manpage, which can be viewed by using:
- Where am I?
pwd/Users/chuck [macchuck:~] chuck%
tcshcommand prompt will show you what directory you’re in, but only to a point; for example:
pwd/Developer/Applications/Extras/Bluetooth [macchuck:Applications/Extras/Bluetooth] chuck%
As this example shows, at first it only looks like I’m in
Applications/Extras/Bluetooth, but issuing the
pwdcommand shows that I’m really in
/Developer/Applications/Extras/Bluetooth. Your cue that you are in a deeper path is that there is no slash before the first directory in the prompt.
- Change directories?
cd /Applications/Utilities[macchuck:/Applications/Utilities] chuck%
- Go back a directory?
cdcommand followed by two dots:
cd ..[macchuck:/Applications] chuck%
- Return to where you were before the last cd command?
cdcommand followed by a hyphen:
cd -[macchuck:/Applications/Utilities] chuck%
- Go back one or more directories?
cdcommand with two dots and a slash (
../) for each directory you want to go back. For example, to go back two directories:
cd ../..[macchuck:/] chuck%
- List a directory’s contents?
This is accomplished using the
lscommand (see Figure 4-1).
By itself, the
lscommand creates a horizontal list of a directory’s contents. Add the
-loption 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 -
aoption (either with or without the
loption) (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 efficiently, pipe (
|) the command to
more, which will display the contents of the directory one screen at a time (see Figure 4-4). The word
morewill 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.
- How can I get a listing of a directory’s contents without seeing the permissions?
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.)
- Clear the display?
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.
- Create a new directory (or folder)?
- Remove an empty directory?
- Remove a directory and all its contents, including subdirectories?
rm -rf NewDirectory
Notice that the
rm -rfcommand will not prompt you before it deletes everything in the
NewDirectorydirectory. You should use the
rm -rfcommand with extreme caution, as you could delete something vital without even knowing it.
- Create an empty file?
- Copy a file or directory?
cp myfile.txt myfile2.txt
This makes a copy of
myfile2.txtwithin the same directory. If you want to copy a file and place it in another directory, use the following:
cp myfile.txt Books/myfile.txt
This makes a copy of
myfile.txtand places that copy in the
- Rename a file or directory?
mv myfile.txt myFile.txt
This renames the file
myFile.txtin the same directory.
- Move a file or directory?
The following moves the file
mv myFile.txt Books
- See what’s inside a file?
cat myFile.txtThis is my file. I hope you like it. Chuck [macchuck:~/Books] chuck%
- Make a file or directory read-only?
chmod =r myFile.txt[macchuck:~/Books] chuck%
chmod 444 myFile.txt[macchuck:~/Books] chuck%
chmod a-wx,a+r myFile.txt
chmodcommand has many options; for more information, see its manpage (
- Zip up a file so I can send it to a Windows user?
This zips up the file and places the
myFile.zipfile in the same directory as the original file.
- View the contents of a Zip 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.
- Unzip a file that I received from a Windows user?
To unzip a file or directory, use the
unzipcommand, 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):
Ato replace all the files with similar names
Nto not replace any of the files
rto rename the like-named file that already exists
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 the
unzipcommand will extract the Zip file.
- Archive a file or directory?
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.
gzipto zip, or compress, the archive.
- View the contents of a tarball?
To peek inside a tarball to see the files it contains, use the
tarcommand with the
tar tvfz myFile.tar.gz-rw-r--r-- 1 chuck staff 44 Oct 05 21:10 myFile.txt
toption is used to print the names of the files inside the tarball.
- Open a
tar xvf myFile.tar[RETURN]
xoption 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 compressed using
gzip. To decompress that file, use the following command:
tar xvfz myFile.tgz[RETURN]
zoption tells the tar command that the file it will decompress has been
- Log in as the superuser?
Some commands require you to be the superuser (or the
rootuser) before they can be issued. Rather than logging out and then logging back in as
root, you can issue the
sucommand, 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).