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

[macchuck:~] chuck% man pwd

The instructions for using the pwd command (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 man command even has its own manpage, which can be viewed by using:

[macchuck:~] chuck% man man
Where am I?

Type pwd on the command line, and hit Return; this will tell you the present working directory.

[macchuck:~] chuck% pwd
/Users/chuck
[macchuck:~] chuck%

The default tcsh command prompt will show you what directory you’re in, but only to a point; for example:

[macchuck:Applications/Extras/Bluetooth] chuck% 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 pwd command 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?

Use the cd command and go to the Utilities directory:

[macchuck:~] chuck% cd /Applications/Utilities
[macchuck:/Applications/Utilities] chuck%
Go back a directory?

Use the cd command followed by two dots:

[macchuck:/Applications/Utilities] chuck% cd ..
[macchuck:/Applications] chuck%
Return to where you were before the last cd command?

Use the cd command followed by a hyphen:

[macchuck:/Applications] chuck% cd -
[macchuck:/Applications/Utilities] 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:

[macchuck:/Applications/Utilities] chuck% cd ../..
[macchuck:/] chuck%
List a directory’s contents?

This is accomplished using the ls command (see Figure 4-1).

Listing a directory’s contents with ls

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

Listing a directory’s contents with ls -l

Figure 4-2. Listing a directory’s contents with ls -l

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

Listing all a directory’s contents—including dot files—using ls -la

Figure 4-3. Listing all 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). 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 find the item you’re looking for or until you reach the end.

Listing a directory’s contents with some assistance from the more command

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

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:

[macchuck:/Applications] chuck% ls -l | colrm 1 49

Acrobat 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 colrm (1 and 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?

When you issue the clear command, the Terminal window scrolls down, placing the command prompt at the top of the display.

[macchuck:/Applications] chuck% clear

You can also use Control-L to clear the display, and if you want to reset the Terminal window, use

Listing a directory’s contents with some assistance from the more command

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

[macchuck:~] chuck% mkdir NewDirectory
Remove an empty directory?

Use the rmdir command:

[macchuck:~] chuck% rmdir NewDirectory
Remove a directory and all its contents, including subdirectories?

Use the rm command with the -rf option to force the removal of the directory and its contents:

[macchuck:~] chuck% rm -rf NewDirectory

Warning

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.

Create an empty file?

There are many ways you can do this, but one of the easiest is by using the touch command:

[macchuck:~] chuck% touch myfile.txt
Copy a file or directory?

Use the cp command:

[macchuck:~] chuck% 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 following:

[macchuck:~] chuck% cp myfile.txt Books/myfile.txt

This makes a copy of myfile.txt and places that copy in the /Books directory.

Rename a file or directory?

To rename a file, use the mv command:

[macchuck:~] chuck% mv myfile.txt myFile.txt

This renames the file myfile.txt to myFile.txt in the same directory.

Move a file or directory?

The following moves the file myFile.txt to the Books directory:

 [macchuck:~] chuck% mv myFile.txt Books
See what’s inside a file?

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

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

[macchuck:~/Books] chuck% chmod =r myFile.txt
[macchuck:~/Books] chuck% chmod 444 myFile.txt
[macchuck:~/Books] chuck% chmod a-wx,a+r myFile.txt

The chmod command has many options; for more information, see its manpage (man chmod ).

Zip up a file so I can send it to a Windows user?

To zip a file or directory, use the zip command, as follows:

[macchuck:~/Books] chuck% zip myFile.zip 
                     myFile.txt

This zips up the file and places the myFile.zip file in the same directory as the original file.

View the contents of a Zip file?

Use the unzip command with the -l option to list the contents of a Zip file, as follows:

[macchuck:~/Books] chuck% unzip -l myFile.zip
Archive:  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.zip (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 unzip command, as follows:

[macchuck:~/Books] chuck% unzip myFile.zip

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:

[macchuck:~/Books] chuck% unzip myFile.zip
Archive:  myFile.zip
replace myFile.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: myFile.txt.bak
 extracting: myFile2.txt

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 the files

  • r to 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 unzip command will extract the Zip file.

Archive a file or directory?

To archive a file or directory, use the Unix tape archive command, tar , as follows:

[macchuck:~/Books] chuck% tar cvfz myFile.tar.gz [RETURN]
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 the tvfz options:

[macchuck:~/Books] chuck% tar tvfz myFile.tar.gz
-rw-r--r--  1 chuck  staff  44 Oct 05 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 (a .tar file), use the following:

[macchuck:~/Books] chuck% tar xvf myFile.tar [RETURN]
myFile.txt

The -x option is used to extract the contents of the tarball. This command unpacks the tarball and places 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:

[macchuck:~/Books] chuck% tar xvfz myFile.tgz [RETURN]
myFile.txt

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:

[macchuck:~] chuck% su
Password: ********
[macchuck:/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:

[macchuck:/Users/chuck] root# exit
exit
[macchuck:~] chuck%

Note

Remember, for most (if not all) tasks, you should be able to get by with using the sudo command instead of logging in as root.

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

Get Mac OS X Pocket Guide, Second Edition 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.