8 must-know command-line tools for Mac OS X

* pbcopy & pbpaste

 

Many Unix tools and scripts are commonly used under the OS X terminal. If you are migrating from Linux to OS X you will find many familiar commands and scripting tools that don't really make any difference.

 

But OS X also provides many special command-line tools that other systems don't have. We recommend 8 of these tools in the hope that they will help you be more productive in your Mac's command-line environment.

 

1. open

 

The open command is used to open a file, directory, or execute a program. It is equivalent to repeating the "double-click" action of the graphical interface in the command line mode. For example, this command is the same as double-clicking Safari in the Finder:

 

$ open /Applications/Safari.app/

If you open a file, it will be opened with the associated program. For example open screenshot.png will view the image in Preview.

 

You can use the -a option to require your own selection of the program to open, or -e to force editing of this file in TextEdit.

 

open a directory will open the directory in a Finder window. A useful trick is open . to open the current directory.

 

The interaction between the Finder and Terminal is bidirectional - dragging a file from the Finder into the Terminal is equivalent to pasting the full path of the file into the command line.

 

2. pbcopy and pbpaste

 

These two tools can open the command line and clipboard. Of course copy-paste with the mouse can also be used - but the real power of these two tools is when they are used as Unix tools. That is to say: you can use these two tools as pipes, IO redirection, and integration with other commands. E.g:

 

$ ls ~ | pbcopy

The home directory's file list can be copied to the clipboard.

 

It is also possible to read the contents of any file into the clipboard:

 

$ pbcopy < blogpost.txt

Do something even crazier: get the URL of the latest Google Memorial logo (doodle) and copy to clipboard:

 

$ curl http://www.google.com/doodles#oodles/archive | grep -A5 'latest-doodle on' | grep 'img src' | sed s/.*'<img src="\/\/'/''/ | sed s/'" alt=".*'/''/ | pbcopy

Using the pipe syntax with the pbcopy tool makes it easy to grab the output of a command without having to scroll up through the terminal window. Can be used to share standard and error output from the command line with others. pbcopy and pbpaste can also be used to automate or speed up some things. For example, to save the subject of some emails as a task list, you can copy the subject from Mail.app first, and then run:

 

$ pbpaste >> tasklist.txt

3. mdfind

 

Many Linux users find that the way to find files in Linux doesn't work well on OS X. Of course the classic Unix find command will always work, but since OS X has the killer search tool Spotlight, why not use it on the command line too?

 

This is the mdfind command. What Spotlight can do, mdfind can do too. This includes searching for file content and metadata.

 

mdfind also provides more search options. For example the -onlyin option can constrain the search to a directory:

 

$ mdfind -onlyin ~/Documents essay

mdfind's index database is automatically updated in the background, but you can also use the mdutil tool to diagnose database problems. Diagnosing mdfind problems is equivalent to diagnosing Spotlight. If Spotlight is not working correctly, the mdutil -E command can force a rebuild of the index database. You can also turn off file indexing completely with mdutil -i.

 

4. screencapture

 

The screencapture command can take screenshots. Similar to Grab.app with cmd + shift + 3 or cmd + shift + 4 hotkeys, but more flexible.

 

Grab the full screen with the mouse cursor and insert it as an image.png in an attachment to a new message:

 

$ screencapture -C -M image.png 

Use mouse to select grab window (and shadow) and copy to clipboard:

 

$ screencapture -c -W

Capture the screen after a delay of 10 seconds and open it in Preview:

 

$ screencapture -T 10 -P image.png

Use the mouse to intercept a rectangular area and save it as a pdf file after grabbing:

 

$ screencapture -s -t pdf image.pdf

See screencapture --help for more usage.

 

5. launchctl

 

launchctl manages OS X's startup scripts and controls the services that need to be started when the computer is started. It is also possible to set up scripts to perform specific tasks on a regular basis, just like Linux cron.

 

For example, to automatically start the Apache server on boot:

 

$ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Running launchctl list displays the current launch script. sudo launchctl unload [path/to/script] Stop the running startup script, plus the -w option to remove the startup. Use this method to remove all the "auto-update" daemons that come with Adobe or Microsoft Office at once.

 

Launchd scripts are stored in:

 

~/Library/LaunchAgents    

/Library/LaunchAgents          

/Library/LaunchDaemons

/System/Library/LaunchAgents

/System/Library/LaunchDaemons

The format of the startup script can refer to this blog, or the article in the Apple Developer Center. You can also use the Lingon application to completely replace the command line.

 

6. say

 

say is an interesting text-to-speech (TTS) tool that uses the same engine as OS X, VoiceOver. If no other options are added, the string you give will be simply spoken aloud:

 

$ say "Never trust a computer you can't lift."

Use the -f option to read a specific text file, and the -o option to save the result as an audio file instead of playing it:

 

$ say -f mynovel.txt -o myaudiobook.aiff

The say command can be used to play warnings or prompts in scripts. For example, you can set up Automator or Hazel scripts to process files and voice prompts with the say command when the task is complete.

 

The funniest (and guilt-ridden) use is: SSH into a friend or colleague's computer and surprise them with the say command...

 

The system's speech options and even the language of the speech can be adjusted in the Dictation & Speech option in System Preferences.

 

7. diskil

 

diskutil is the command-line version of the OS X Disk Utility application. It can not only complete all the tasks of the GUI application, but also do some additional tasks such as filling in all 0s and filling in random numbers. First use diskutil list to see a list of all disks and their paths, and then execute commands on specific disks.

 

Warning: Improper use of diskutil can accidentally destroy disk data. be careful.

 

8. brew

 

The brew provided by the Homebrew program is not strictly a native command of OS X, but any professional user of OS X will not miss it. "OS X's missing package manager" is apt. If you've ever used apt-get (or any other package manager - Translator's Note) on Linux, you'll find that Homebrew is basically the same.

 

Thousands of open source tools and libraries are easily available with brew. For example, brew install imagemagick can install ImageMagick (an image tool that can handle almost any image problem and convert any format), and brew install node can install Node.js (the current hot server-side JavaScript programming tool).

 

You can also do interesting things with Homebrew: brew install archey will install Archey (a gadget that displays the Apple logo and computer hardware parameters when you start the command line).

 

Please enter image description

The number of tools that Homebrew can install is huge, and they are constantly being updated. One of the best things about Homebrew is that all files are bound to one location /usr/local/. That is to say, you can install new versions of software through Homebrew while keeping the built-in dependency libraries or other software in the system unchanged. At the same time, if you want to completely delete Homebrew, it becomes very simple.

 

(Note: It is better not to delete /usr/local/ directly to delete Homebrew. This uninstall script should be used.)

 

Finally, here's a full list of all OS X commands.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326243006&siteId=291194637
Recommended