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 probably 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?
All of the Unix commands on your system have a manual page (or
manpage
for short). To view the manpage for any command, you use theman
command:[localhost:~] chuck%
man pwd
The instructions for using the
pwd
command (described next) is 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. Theman
command even has its own manpage, which can be viewed by using:[localhost:~] chuck%
man man
- Where am I?
Type
pwd
on the command line, and hit Return; this will tell you the present working directory.[localhost:~] chuck%
pwd
/Users/chuck [localhost:~] chuck%- Change directories?
[localhost:~] chuck%
cd /Applications
[localhost:/Applications] chuck%- Go back a directory?
Use the
cd
command followed by two dots:[localhost:/Applications] chuck%
cd ..
[localhost:~] chuck%- Return to where you were before the last cd command?
Use the
cd
command followed by a hyphen:[localhost:/] chuck%
cd -
[localhost:/Applications] chuck%- Go back one or more directories?
Use the
cd
command with two dots and a slash (../
) for each directory you want to go back. For example, to go back two directories:[localhost:/Applications/Utilities] chuck%
cd ../..
[localhost:/] chuck%- List a directory’s contents?
This is accomplished using the
ls
command (see Figure 4-1).By itself, the
ls
command creates a horizontal list of a directory’s contents. Add the-l
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 of the contents for a directory, including the dot files (described earlier), add the
-a
option (either with or without thel
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 efficiently, pipe (|
) the command tomore
, which will display the contents of the directory one screen at a time (see Figure 4-4).The word
more
will be highlighted at the bottom of the screen. To go to the next screen, hit the spacebar; continue doing so until you’ve found the item you’re looking for or until you’ve reached the end.- How can I get a listing of a directory’s contents without seeing the permissions?
Use
ls -l
and pipe the output of that listing to thecolrm
(column remove) command, as follows:[localhost:/Applications] chuck%
ls -l | colrm 1 47
Acrobat Reader 5.0 Address Book.app AppleScript BBEdit 6.5 Demo Calculator.app Clock.app Games ICQ Installation Log ICQ2.8X Image Capture.app . . .The numbers following
colrm
(1
and47
) 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 47—all of the characters preceding the file or directory name—will be deleted.)- Clear the display?
When you issue the
clear
command, the Terminal window will scroll down, placing the command prompt at the top of the display.[localhost:/Applications] chuck%
clear
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)?
Use the
mkdir
command, followed by the name of the new directory you’d like to create:[localhost:~] chuck%
mkdir NewDirectory
- Remove an empty directory?
[localhost:~] chuck%
rmdir NewDirectory
- Remove a directory and all of its contents, including subdirectories?
Use the
rm
command with the-rf
option to force the removal of the directory and its contents:[localhost:~] chuck%
rm -rf NewDirectory
Notice that this command will not prompt you before it deletes everything in the
NewDirectory
directory. You should use therm -rf
command with extreme caution.- Create an empty file?
There are many ways you can do this, but one of the easiest is by using the
touch
command:[localhost:~] chuck%
touch myfile.txt
- Copy a file or directory?
[localhost:~] chuck%
cp myfile.txt myfile2.txt
This will make a copy of
myfile.txt
and createmyfile2.txt
within the same directory. If you wanted to copy a file and place it in another directory, use the following:[localhost:~] chuck%
cp myfile.txt Books/myfile.txt
This will make a copy of
myfile.txt
and place that copy in the/Books
directory.- Rename a file or directory?
To rename a file, use the
mv
command:[localhost:~] chuck%
mv myfile.txt myFile.txt
This will rename the file
myfile.txt
tomyFile.txt
in the same directory.- Move a file or directory?
The following will move the file
myFile.txt
to theBooks
directory:[localhost:~] chuck%
mv myFile.txt Books
- See what’s inside a file?
For this, you can use either
cat
,more
, orless
:[localhost:~/Books] chuck%
cat myFile.txt
This is my file. I hope you like it. Chuck [localhost:~/Books] chuck%- Make a file or directory read-only?
For this, you’ll need to use the
chmod
(change mode) command. Any one of the following will assign read-only permission tomyFile.txt
for everyone:[localhost:~/Books] chuck%
chmod =r myFile.txt
[localhost:~/Books] chuck%chmod 444 myFile.txt
[localhost:~/Books] chuck%chmod a-wx,a+r myFile.txt
The
chmod
command has many options; for more information, see its manpage (man chmod
).- Compress a file?
To compress a file, you can use the Unix tape archive command,
tar
, as follows:[localhost:~/Books] chuck%
tar cvfz myFile.tar.gz
myFile.txt
The options used are as follows:
- c
Creates a new archive.
- v
Verbose; this option prints the filenames onscreen as files that are added to or extracted from the archive.
- f
Stores files in or extract files from an archive.
- z
Uses
gzip
to zip, or compress, the archive.
- View the contents of a tarball?
To peek inside a tarball to see the files it contains, use the
tar
command with thetvfz
options:[localhost:~/Books] chuck%
tar tvfz myFile.tar.gz
-rw-r--r-- 1 chuck staff 44 Feb 16 21:10 myFile.txtThe
-t
option is used to print the names of the files inside the tarball.- Open a
.tar
file? To unpack a tarball (
.tar
), use the following:[localhost:~/Books] chuck%
tar xvf myFile.tar
myFile.txt
The
-x
option is used to extract the contents of the tarball. This command will unpack the tarball and place its contents in the filemyFile.txt
.If you receive a
.tgz
(or.tar.gz
) file, that means the tarball has been compressed usinggzip
. To decompress that file, use the following command:[localhost:~/Books] chuck%
tar xvfz myFile.tgz
myFile.txt
The
-z
option tells the tar command that the file it will decompress has beengzip
‘d.- Log in as the superuser?
Some commands require you to be the superuser (or the
root
user) before they can be issued. Rather than logging out and then logging back in asroot
, you can issue thesu
command, followed by the superuser’s password:[localhost:~] chuck%
su
Password:********
[localhost:/Users/chuck] root#Now you have ultimate power—use it with great care as you could damage or overwrite something vital. When you are finished, issue the
exit
command to go back to being a normal user:[localhost:/Users/chuck] root#
exit
exit [localhost:~] chuck%
Get Mac OS X Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.