• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Mac OS X Unix Basics > Basic Unix Commands

4.4. Basic Unix Commands

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 the man 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. The man 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
[localhost:~] chuck% 

Change directories?

Use the cd command:

[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).

Figure 4-1. Listing a directory's contents with ls

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).

Figure 4-2. Listing a directory's contents using ls -l

To list all of 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).

Figure 4-3. Listing all of a directory's contents—including dot files—using ls -la

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).

Figure 4-4. Listing a directory's contents with some assistance from the more command

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 the colrm (column remove) command, as follows:

[localhost:/Applications] chuck% ls -l | colrm 1 47
Acrobat Reader 5.0
Address Book.app
BBEdit 6.5 Demo
ICQ Installation Log
Image Capture.app

The numbers following colrm (1 and 47) 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?

Use the rmdir command:

[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 the rm -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?

Use the cp command:

[localhost:~] chuck% cp myfile.txt myfile2.txt

This will make a copy of myfile.txt and create myfile2.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 to myFile.txt in the same directory.

Move a file or directory?

The following will move the file myFile.txt to the Books directory:

 [localhost:~] chuck% mv myFile.txt Books

See what's inside a file?

For this, you can use either cat, more, or less:

[localhost:~/Books] chuck% cat myFile.txt
This is my file. I hope you like it.
[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 to myFile.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

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.


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 the tvfz options:

[localhost:~/Books] chuck% tar tvfz myFile.tar.gz
-rw-r--r--  1 chuck  staff  44 Feb 16 21:10 myFile.txt

The -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

The -x option is used to extract the contents of the tarball. This command will unpack the tarball and place its contents in the file myFile.txt.

If you receive a .tgz (or .tar.gz) file, that means the tarball has been compressed using gzip. To decompress that file, use the following command:

[localhost:~/Books] chuck% tar xvfz myFile.tgz

The -z option tells the tar command that the file it will decompress has been gzip'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 as root, you can issue the su 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
[localhost:~] chuck%

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint