Appendix B. Basic Command-Line Reference

The command-line interface has been around for a very long time. Its roots go back to the earliest computers. While there are literally hundreds of commands—and thousands of options within these commands—this appendix focuses on the commands that are an extension to the basic ones every administrator must know: cd, Is, and pwd. (Of course, the commands discussed here are by no means the only ones you should know.) You will get an overview of the following:

• Running commands to find files, manage processes, monitor usage, and manage disks and volumes

• Exchanging data between the Finder and the command line

Armed with this additional information, you should be able to better manage your Mac OS X server.

Finding Files Using locate and find

You can use both the find and locate commands to search the file system for files matching certain criteria. The locate command uses a database describing the known files on your system. The locate database is built and updated automatically as long as your system is running at the appropriate time. By default on Mac OS X and Mac OS X Server systems, the locate database is updated at 4:30 a.m. each Saturday. You can execute the script that updates the locate database using the command sudo /etc/weekly.

The locate command understands the wildcard characters that the shell uses (wildcards are discussed later in this appendix). To pass the wildcard character on to the locate command, you must escape the character so that the shell doesn’t process it. For example, the command locate "*.rtf" or locate *.rtf will print a list of all files with names ending in .rtf, while locate *.rtf results in an error.

image

The command find ~ -name *.rtf –print starts a search of the files in the user’s home directory and prints on the screen all of the files with names ending in lowercase .rtf, while the command find ~ -iname *.rtf –print starts a search of the files in your home folder and lists all of the files with names ending in .rtf, regardless of whether the rtf is in lowercase or uppercase.

File Locations

Mac OS X introduces a number of predefined folders intended to contain files of particular types. Since many applications depend on the name and location of these folders, they should not be renamed or moved.

Most applications in the Mac OS X graphical user interface (GUI) reside in /Applications, and operating system files reside in /System. By convention, UNIX programs store their configuration information in /etc while most command-line tools are installed in /bin, /sbin, /usr/bin, or /usr/sbin. Shells search these four folders to find the programs whose names you enter on the command line. Programs in other locations may be executed by specifying an absolute or relative path to the executable.

image

For example, /Developer/Tools/GetFileInfo /Users executes the GetFileInfo command installed by the developer tools. The current folder is not part of the default search path on Mac OS X. This is important for Windows users, but it is a security risk to have a shell include the current folder in the search path, because it could allow unauthorized applications to execute.

Managing Processes From the Command Line

You can determine the currently running processes from the command line using the ps or top command. Use top to see a regularly updated view of system utilization, including memory usage, page faults, and the set of currently executing processes.

image

In the leftmost column of the top tabular output, you will find the process identifier (PID) associated with that process. You can also use the ps command to determine the PID of a process. The PID is used to send a message to a particular process. For example, the command ps -auxww | grep TextEdit prints the PID# and other information for just the TextEdit process.

When you have the PID# for a process, you can send it the command kill -9 PID#, where PID# is the number associated with the TextEdit process. This command asks the process with the given PID to terminate immediately. You can send a variety of commands to running processes, such as rereading a configuration file or logging additional information.

The killall command enables you to signal processes by name rather than by PID. The command killall -KILL TextEdit force-quits all processes that belong to you with the name TextEdit.

Monitoring System Usage

Many shell commands exist to help you monitor the system. The last command shows you which users have logged in most recently or when a specified user last logged in to your system.

image

The id command enables you to determine which groups a particular user has access to, or to determine the short name for a user given their user ID (UID).

Mac OS X systems maintain many log files. Viewing log files on your system or on another system using ssh can help you troubleshoot any number of problems. The command tail -n 10 /Library/Logs/Software Update.log displays the ten most recently installed software updates. The command tail -f /var/log/system.log displays the current contents of the system log, then continues to print new lines as they are added to the file.

Managing Disks and Volumes

You can get all of the functionality presently available in Disk Utility through two commands accessible from the command line:

hdiutil, which handles disk-image management

diskutil, which includes the rest of the features

You can read the man pages to learn how to use the different features, or you can type the command at the command line, and the different options you can use will appear.

image

You can also use the df and du commands to determine free space and space utilization on a volume.

Working With the Command Line and the GUI

In Mac OS X, the command line and the GUI work hand in hand. Transferring data from one environment to the other and moving between the two environments is done very easily. You can select a group of files in the Finder and drag them to a Terminal window to add their paths to a command.

image

The open command enables you to open files and URLs as if you had double-clicked them in the Finder. For example, the command open ~/Documents/ReadMe.rtf launches TextEdit (or your preferred application for opening RTF files) and opens the specified ReadMe.rtf file. Similarly, the command open http://www.apple.com launches Safari (or your preferred Web browser) and opens the Apple home page.

Searching Text Files Using pipe and grep

Use the grep command to search the contents of the listed text file or files. For example, in grep domain /etc/resolv.conf, the file resolv.conf is searched for the word domain and the lines containing that word are displayed.

image

Often, the output of one command is used as input for another command. The UNIX pipe character (|) is used for this purpose. The command ps -auxww | grep Finder executes the ps command and “pipes” its output (the process list) to the grep command. The grep command reads the process list as input, looking for the word Finder, and displays any lines containing Finder in the process list.

Additional Shell Filename Wildcards

Other wildcard characters enable you to specify more complex patterns than you can with the * character. The shell wildcards supported by UNIX shells are *, ?, and [ ].

image

The ? wildcard matches any single character. The [ ] wildcard matches a single character in a list of characters appearing within square brackets. In the figure above, the list command is asked to show only those files whose names start with a capital M and end with a lowercase d, but can have any number of characters in between.

A few examples can help to build your understanding of wildcards. Consider a collection of five files with the names ReadMe.rtf, ReadMe.txt, read.rtf, read.txt, and It’s All About Me.rtf:

*.rtf matches ReadMe.rtf, read.rtf, and It’s All About Me.rtf.

????.* matches read.rtf and read.txt.

[Rr]*.rtf matches ReadMe.rtf and read.rtf.

[A-Z]* matches ReadMe.rtf, ReadMe.txt, and It’s All About Me.rtf.

Additional Mac OS X–Specific Commands

Additional Mac OS X–specific commands include:

softwareupdate: Use to view the list of available updates and install updates that you specify.

installer: Use to install certain packages and/or metapackages.

pbcopy and pbpaste: Enable you to create data in one environment and use it in the other.

atlookup: Use to view AppleTalk devices on your local network.

plutil: Permits the conversion of binary files to XML style for editing.

GetFileInfo and SetFile: Enable you to manipulate HFS files with resource forks, and get and set file attributes (such as type and creator) associated with HFS files. Both are located in the Tools folder inside the Developer folder.

What You’ve Learned

• You can use locate and find to find files.

• The last command helps you keep track of user logins.

• The id command helps you keep track of user and group IDs.

• The tail command helps you view recent activity in a log file.

• The command-line interface gives you another way to force-quit applications and processes.

References

Administration Guides

“Mac OS X Server Command-Line Administration”: http://images.apple.com/server/pdfs/Command_Line_v10.4.pdf

Apple Knowledge Base Documents

You can check for new and updated Knowledge Base documents at www.apple.com/support.

URLs

UNIX home page: www.unix.org

Lesson Review

1. The locate command is built into what script (and the location of the script) that runs automatically if the Mac OS X computer is on during that time period?

2. What does top show?

3. What command would you use to see a live continuous update of the system log file?

4. What does grep do?

Answers

1. /etc/weekly

2. The top command displays a regularly updated view of system utilization, including memory usage, page faults, and the set of currently executing processes.

3. tail –f /var/log/system.log

4. grep searches the contents of the listed text files or items.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.208.181