Simple and useful OS X Terminal commands
The Terminal in OS X may seem like a cryptic tool to use, but there are some quick and useful commands that can be run in it to help troubleshoot your system, and they do not require much knowledge of the Terminal at all.
The OS X terminal is a powerful interface with your computer that can be used to script tasks, run under-the-hood routines on your system, remotely control other systems, and perform a number of other detailed and specific actions. While getting familiar with the Terminal may seem like a daunting and unnecessary task, there are a few useful terminal commands for troubleshooting the system that can be run easily without needing to know much about the Terminal.
The uptime command shows the time that has elapsed since the system was last restarted. While not directly useful, it can provide you with a measure of how long it has been since the system has reloaded everything freshly, and can help you gauge whether a problem you're experiencing only happens after a certain amount of time after booting the system. The command will output the current date, the time since last reboot, the number of users logged in, and the average system load used by those users.
This command is a little redundant with Disk Utility, but will show you all mounted volumes (even hidden ones) and information on them. By default the command outputs information in terms of blocks, but if you use the "-H" flag for the command (df -H) then it will translate the block counts to kilobytes, megabytes, gigabytes and other "human-readable" amounts. It is a good way to get an overview of all locally mounted filesystems.
This command will list all of the open files on a system and what process or program has a lock on them. When you open a file in OS X, the program first locks it, reads it, and then releases the lock. While the file is locked only that one program can access it, and while most programs lock a file only during read and write processes, there are other programs (or bugs in programs) that can maintain the lock on a file. In these instances you may run into errors in which the system either will not open a file or will not discard a file. For instance, if you move a file to the Trash and a program still has a lock on the file, when you empty the Trash the system will give you an error saying the file is in use. To see what program is using the file, run "lsof" in the Terminal, locate the file, and then see what process or program has a hold on the file. Then either quit the program or force it to quit using Activity Monitor. Granted, you can always reboot the system to force file locks to be reset, but this allows you to do the same thing without interrupting your workflow.
This command will force a program or process (NAME) to quit by specifying its name (so, for example, "killall TextEdit" will force TextEdit to quit). Even though OS X has a force-quit option, this can be convenient if you already have the Terminal open. It also can be used to target specific programs such as the Dock that do not show up in the OS X "force-quit" window.
This is a widely used network troubleshooting tool that can be used to check the connection latency between your computer and another one (HOSTNAME). If you cannot connect to a Web site, type "ping" followed by the host name or IP address of a remote computer (i.e., "ping www.cnet.com" will show latency responses from the CNET Web site). This can be used to determine if your network is not functioning, or if the problem lies with the remote computer's network. You can also see if the connection latency is smooth or if it is fluctuating.
While the ping command is useful, it is not necessarily conclusive about network problems, as many routers and host computers may be set to refuse a ping request.
The ping utility will continually test network latency until you quit it either by closing the Terminal window or by pressing Control-C.
The shutdown command can be exceptionally useful, especially in the event of a hang during shutdown. Normally, using the "Shutdown" or "Restart" commands in the Apple menu is the recommended way to manage shutdowns or restarts, as they will ensure that programs are properly quit and files are saved before the system goes down. However, there are times when a system process will hang and prevent a smooth shutdown. When this happens people often resort to pressing and holding the power button to shut off their systems, but before doing this you can try to force the system to restart by running the shutdown command in the following manner:
sudo shutdown -h now
This command will run the shutdown routine as root (the main administrator) immediately, and will halt the system when done (if you use "-r" instead of "-h" then it will reboot the system). This will force applications and processes to quit, which may be more successful than using the Apple menu's shutdown options.
Finally, there are useful commands that can be used to look up information about other commands. If you type "whatis" followed by the command name (for example, "whatis ping"), then the terminal will output a small one-line summary of the command. Sometimes if there are multiple commands that contain the name of the given command then the system will output the summaries of each on separate lines.
If you want more than the brief summaries given by whatis, you can use the "man" command in a similar manner to "whatis" in order to look up more details about a specific command (provided there is a manual page available for the command). However, while this is convenient when you have the Terminal open, the man pages for all of Apple's commands also can be found online (just type "man COMMAND" into Google to quickly access them).
Do you have any simple Terminal command recommendations? If so, then post them in the comments below.