Top 20 OS X command-line secrets for power users

Beyond Bash: The most useful command-line utilities for Mac power users and system administrators

RELATED TOPICS
Page 3 of 6

6. locate: Quickly search file and folder names
The locate utility searches a special internal database of all publicly accessible pathnames that OS X maintains separately from the Spotlight search index. It's very fast to search, but the database is refreshed only periodically -- typically daily.

The first time you run the command, you may receive a warning that the database doesn't exist. The warning message will tell you the name of the command to activate locate's database and begin building it, a process that may take several hours. Once built, however, the database is quick and easy to search when you're trying to track down a folder, file, or application. Simply type the string you're searching for after the command name. Type man locate for full details on search syntax.

For example:
$ locate junko

might display:
/Scratch Area/junko
/Scratch Area/junko/BootCamp_3.1_32-bit.exe
/Scratch Area/junko/BootCamp_3.1_64-bit.exe
/Scratch Area/junko/EPSONPrinterDrivers2.2.dmg
/Scratch Area/junko/SecUpd2010-001.dmg
/Scratch Area/junko/SecUpdSrvr2010-001.dmg
/Users/mel/junko
/Users/mel/junko stuff
/Users/mel/junko stuff/.DS_Store
/Users/mel/junko.txt

7. lsof: Reveal open TCP and UDP ports and the applications using them
When tracking down problems, particularly security-related issues, it's often helpful to know which applications are currently communicating on the network. The lsof (list open files) command does that. With the -i option, it lists all the open connections in progress and the names of the applications using them. The list can be long, and often you're only interested in applications that have established sessions, not partially open ones (for example, someone trying to sign on). To reduce the command's output, you can pipe it to the grep command and filter on the word ESTABLISHED to list only functional connections:
$ lsof -i | grep ESTABLISHED

It might display:
ScreenShare  865 adm ... TCP 10.0.0.48:64603->www.sbwh.net:ssh (ESTABLISHED)
ScreenShare  866 adm ... TCP 10.0.0.48:26164->www.sbwh.net:ssh (ESTABLISHED)
JobServ      922 adm ... TCP 10.0.0.48:64701->renite.jobserv.com:ftp (ESTABLISHED)
aosnotify   1101 adm ... TCP 10.0.0.48:50129->aosnotify.me.com:5223 (ESTABLISHED)
Mail        7601 adm ... TCP 10.0.0.48:53529->supersonic.jet.net:https (ESTABLISHED)

8. networksetup: Retrieve or set network configuration values
Network troubleshooting often requires examining a computer's network settings and possibly making changes to them to test various theories about what may be wrong. The networksetup tool lets you view all manner of network parameters, including IP addresses, available network interfaces, and more than 50 other variables. One setting you probably have to verify frequently is the list of DNS servers, especially in light of the recent malware that changed this list to point to malicious DNS servers. You can also turn Wi-Fi off or on.

The other 49 variables are assigned as homework; here's the variable to display the list of name servers in use for an interface (in this case, Ethernet):
$ networksetup -getdnsservers ethernet

It might display:
68.238.64.12
68.238.128.12
8.8.8.8
4.2.2.1

And to turn Wi-Fi on or off:
$ networksetup -setairportpower airport on
$ networksetup -setairportpower airport off

9. open: Launch applications and open Finder windows from the command line
One of OS X's most versatile commands, open facilitates smooth interaction between a command-line shell and the Mac's graphical user interface. From the command line, you can open a directory into a Finder window, open a document into an application, open a text file into a text editor for quick changes, and more.

To open a directory in a Finder window:
$ open /Users/mel/Documents

Open an application (case doesn't matter):
$ open itunes

| 1 2 3 4 5 6 Page 3
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies