Top 20 OS X command-line secrets for power users

For local files, the file descriptor is an ordinary path to the file, which if in the current directory and consists of just the file name. The remote file descriptor has three parts, in the form of userID@remotesystem:filepath, where userID is the name of the user on the remote system, remotesystem is the name or IP address of the remote system, and filepath is a path to the file.

A neat feature of scp is that you can copy in either direction: Give the local file descriptor first to copy from local to remote; give the remote file descriptor first to copy from remote to local. The examples below show both methods, as well as how to copy entire folder hierarchies.

To copy from a local current directory to the remote system:

$ scp filename userID@remotesystem:/path/to/filename

To copy from the remote system to the local current directory:

$ scp userID@remotesystem:/path/to/filename ./

To copy an entire directory (the -r option indicates recursion):

$ scp -r directoryname userID@remotesystem:/path/to/directory

14. scutil: Set the computer host name

Sometimes you want to change the name of a computer, but there doesn't seem to be an easy way to do this through any system preference or other graphical utility. You can do it with the very powerful scutil tool, although you must run the command three times to change the name in the three places where it resides.

You can do a lot more with scutil in the same way you can do a ton more than drive your car with a gallon of gasoline. Unless you know what you're doing, it's best to let scutil's other abilities stay in drawer.

$ sudo scutil --set ComputerName "newname"

$ sudo scutil --set LocalHostName "newname"

$ sudo scutil --set HostName "newname"

15. shutdown: Restart the Mac

System administrators sometimes have to restart computers when the owner isn't around. Sharing the screen isn't always convenient -- or even enabled. If you can get a remote command-line access to the target machine (such as via the ssh Secure Shell command), rebooting remotely is a cinch using the shutdown command. The -r option indicates you want to restart; without it, the Mac will power off. The now argument means just what it says: Do it now. You can also specify a date and time, in the form yymmddhhmm, to preset a delayed reboot.

$ shutdown -r now

16. sysctl: Get CPU information and other internal secrets

The official purpose of the sysctl utility is to get or set kernel state values. Unless you know what you're doing, you don't want to set kernel state values. But looking at them is harmless -- and can be informative.

For example, the example below displays the machine CPU type, which can be useful to know for certain system administration chores. You can use sysctl to control decisions in a Bash script as well. Run sysctl -a to get a list of all the kernel variables available for inspection. You might find some others you'd like to inspect.

For example:

$ sysctl -n machdep.cpu.brand_string

might display:

Intel(R) Core(TM) i5 CPU 750 @ 2.67GHz

17. systemsetup: Perform various system configuration operations

The systemsetup command lets you retrieve and alter a wide range of configuration values normally set from the graphical System Preferences application. Run systemsetup help to get a complete list of options.

One popular setting is to configure a system to set its clock based on a network time source, as shown below. You'll undoubtedly find useful reasons to set other values.

$ systemsetup -setnetworktimeserver

18. textutil: Convert between various text file formats

Converting text file formats is a black art. In the ancient days before OS X, back in the 1990s, Apple included a text conversion program with what was then called System or Mac OS. In OS X, that program was replaced by textutil, which is even more useful because it can convert files with today's complex HTML, docs, and other formats.

To convert a file, just specify the -convert option, the new file type, the path to the source file, and the path to the output file using the -output option.

The example below shows how to convert a Microsoft Word file into HTML. textutil figures out the source file type automatically. If you want to see what textutil thinks your file's type is, use the -info option, also shown below. Though textutil isn't perfect, you gotta remember -- it's also not the 1990s.

To convert a Word document into HTML:

$ textutil -convert html MyWordFile -output /tmp/webfile.html

To display textutil's interpretation of a document's file format:

$ textutil -info MyWordFile

It might display:

Type: Word format

Size: 45568 bytes

Length: 4354 characters


Author: Mel Beckman

Last Editor: Mel Beckman



Created: 2012-07-08 09:11:00 -0700

Last Modified: 2012-09-13 11:52:00 -0700

Contents: Q. I have a question about wri...

19. top: Find the CPU hogs in your system

The top command is familiar to Linux and Unix users. It lists the busiest programs running on a system, helping you determine why a system might be running slowly. Apple's graphical Activity Monitor application does the same, but top gives you a quick look without leaving the command-line environment.

By default, top displays the first 20 programs in its list. The problem is that, for some reason, OS X's top sorts its list not in order of descending CPU usage but in order of descending process ID. To get the list in proper CPU usage order, add the -o cpu) option, as shown below. The top list also shows which programs are running and which are "sleeping" -- waiting for input/output operations to complete. When a system is slow, top usually reveals the culprit.

20. uptme: Show the time since last reboot and how busy the system is

If you need to know how long it's been since a Mac rebooted, uptme is the command for you. It shows the current time of day, plus the elapsed time since last reboot in days, hours, minutes, and seconds. For some reason, Windows doesn't have this command, but it probably isn't up long enough to matter.

Also, uptme shows the number of users logged in and the load averages (the number of processes waiting to run) of the system over the last one, five, and 15 minutes. What values are good or bad for load average depend on the number of CPUs available. A load average of 4 isn't bad for a quad-core Mac, but it would indicate a very busy single-core system.

For example:

$ uptime

might display:

16:04 up 721 days, 15:37, 2 users, load averages: 0.72 0.81 0.81

These are the 20 OS X command-line utilities you can get the most value from. Now it's up to you to do so!

This story, "Top 20 OS X command-line secrets for power users" was originally published by InfoWorld.

| 1 2 Page 9
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon