Advertisement
x95u1

Eight Terminal Utilities Every OS X Command Line User Should

Jan 20th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. Eight Terminal Utilities Every OS X Command Line User Should Know
  2. =================================================================
  3.  
  4. May 14, 2014 o Mitchell Cohen
  5.  
  6. The OS X Terminal opens up a world of powerful UNIX utilities and
  7. scripts. If you're migrating from Linux, you'll find many familiar
  8. commands work the way you expect. But power users often aren't aware
  9. that OS X comes with a number of its own text-based utilities not found
  10. on any other operating system. Learning about these Mac-only programs
  11. can make you more productive on the command line and help you bridge
  12. the gap between UNIX and your Mac.
  13.  
  14. Update: Thanks to reader feedback, I've written about a few more
  15. commands in a follow-up post: [7](And eight hundred more).
  16.  
  17. 1. open
  18.  
  19. open opens files, directories and applications. Exciting, right? But it
  20. really does come in handy as a command-line double-click. For instance,
  21. typing:
  22. $ open /Applications/Safari.app/
  23.  
  24. ...will launch Safari as if you had double-clicked its icon in the
  25. Finder.^[8]1
  26.  
  27. If you point open at a file instead, it will try to load the file with
  28. its associated GUI application. open screenshot.png on an image will
  29. open that image in Preview. You can set the -a flag to choose the app
  30. yourself, or -e to open the file for editing in TextEdit.
  31.  
  32. Running open on a directory will take you straight to that directory in
  33. a Finder window. This is especially useful for bringing up the current
  34. directory by typing open .
  35.  
  36. Remember that the integration between Finder and Terminal goes both
  37. ways - if you drag a file from Finder into a Terminal window, its full
  38. path gets pasted into the command line.
  39.  
  40. 2. pbcopy and pbpaste
  41.  
  42. These two commands let you copy and paste text from the command line.
  43. Of course, you could also just use your mouse--but the real power of
  44. pbcopy and pbpaste comes from the fact that they're UNIX commands, and
  45. that means they benefit from piping, redirection, and the ability to be
  46. in scripts in conjunction with other commands. Typing:
  47. $ ls ~ | pbcopy
  48.  
  49. ...will copy a list of files in your home directory to the OS X
  50. clipboard. You can easily capture the contents of a file:
  51. $ pbcopy < blogpost.txt
  52.  
  53. ..or do something crazier. This hacked-up script will grab the link of
  54. the latest Google doodle and copy it to your clipboard.
  55. $ curl http://www.google.com/doodles#oodles/archive | grep -A5 'latest-doodle on
  56. ' | grep 'img src' | sed s/.*'<img src="\/\/'/''/ | sed s/'" alt=".*'/''/ | pbco
  57. py
  58.  
  59. Using pbcopy with pipes is a great way to capture the output of a
  60. command without having to scroll up and carefully select it. This makes
  61. it easy to share diagnostic information. pbcopy and pbpaste can also be
  62. used to automate or speed up certain kinds of tasks. For instance, if
  63. you want to save email subject lines to a task list, you could copy the
  64. subjects from Mail.app and run:
  65. $ pbpaste >> tasklist.txt
  66.  
  67. 3. mdfind
  68.  
  69. Many a Linux power user has tried to use locate to search for files on
  70. a Mac and then quickly discovered that it didn't work. There's always
  71. the venerable UNIX find command, but OS X comes with its own killer
  72. search tool: Spotlight. So why not tap into its power from the command
  73. line?
  74.  
  75. That's exactly what mdfind does. Anything Spotlight can find, mdfind
  76. can find too. That includes the ability to search inside files and
  77. metadata.
  78.  
  79. mdfind comes with a few conveniences that make it stand out from its
  80. big blue brother. For instance, the -onlyin flag can restrict the
  81. search to a single directory:
  82. $ mdfind -onlyin ~/Documents essay
  83.  
  84. The mdfind database should stay up to date in the background, but you
  85. can also troubleshoot it (as well as Spotlight) using mdutil. If
  86. Spotlight isn't working the way it should, mdutil -E will erase the
  87. index and rebuild it from scratch. You can also turn off indexing
  88. entirely with mdutil -i off.
  89.  
  90. 4. screencapture
  91.  
  92. screencapture lets you take many different kinds of screenshots. It's
  93. similar to Grab.app and the keyboard shortcuts cmd + shift + 3 and cmd
  94. + shift + 4, except it's far more flexible. Here are just a few
  95. different ways you can use screencapture:
  96.  
  97. Capture the contents of the screen, including the cursor, and attach
  98. the resulting image (named `image.png') to a new Mail message:
  99. $ screencapture -C -M image.png
  100.  
  101. Select a window using your mouse, then capture its contents without the
  102. window's drop shadow and copy the image to the clipboard:
  103. $ screencapture -c -W
  104.  
  105. Capture the screen after a delay of 10 seconds and then open the new
  106. image in Preview:
  107. $ screencapture -T 10 -P image.png
  108.  
  109. Select a portion of the screen with your mouse, capture its contents,
  110. and save the image as a pdf:
  111. $ screencapture -s -t pdf image.pdf
  112.  
  113. To see more options, type screencapture --help
  114.  
  115. 5. launchctl
  116.  
  117. launchctl lets you interact with the OS X init script system, launchd.
  118. With launch daemons and launch agents, you can control the services
  119. that start up when you boot your computer. You can even set up scripts
  120. to run periodically or at timed intervals in the background, similar to
  121. cron jobs on Linux.
  122.  
  123. For example, if you'd like to have the Apache web server start
  124. automatically when you turn on your Mac, simply type:
  125. $ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
  126.  
  127. Running launchctl list will show you what launch scripts are currently
  128. loaded. sudo launchctl unload [path/to/script] will stop and unload
  129. running scripts, and adding the -w flag will remove those scripts
  130. permanently from your boot sequence. I like to run this one on all the
  131. auto-update "helpers" created by Adobe apps and Microsoft Office.
  132.  
  133. Launchd scripts are stored in the folllowing locations:
  134. ~/Library/LaunchAgents
  135. /Library/LaunchAgents
  136. /Library/LaunchDaemons
  137. /System/Library/LaunchAgents
  138. /System/Library/LaunchDaemons
  139.  
  140. To see what goes into a launch agent or daemon, there's a great blog
  141. post by [9]Paul Annesley that walks you through the file format. And if
  142. you'd like to learn how to write your own launchd scripts, Apple
  143. provides some helpful documentation on their [10]Developer site.
  144. There's also the fantastic [11]Lingon app if you'd prefer to avoid the
  145. command line entirely.
  146.  
  147. 6. say
  148.  
  149. This is a fun one: say converts text to speech, using the same TTS
  150. engine OS X uses for [12]VoiceOver. Without any options, say will
  151. simply speak whatever text you give it out loud.:^[13]2
  152. $ say "Never trust a computer you can't lift."
  153.  
  154. You can also use say to speak the contents of a text file with the -f
  155. flag, and you can store the resulting audio clip with the -o flag:
  156. $ say -f mynovel.txt -o myaudiobook.aiff
  157.  
  158. The say command can be useful in place of console logging or alert
  159. sounds in scripts. For instance, you can set up an Automator or
  160. [14]Hazel script to do batch file processing and then announce the
  161. task's completion with say.
  162.  
  163. But the most enjoyable use for say is rather more sinister: if you have
  164. ssh access to a friend or coworker's Mac, you can silently log into
  165. their machine and haunt them through the command line. Give `em a
  166. Siri-ous surprise.
  167.  
  168. You can set the voice (and language!) used by say by changing the
  169. default setting in the Dictation & Speech panel in System Preferences.
  170.  
  171. 7. diskutil
  172.  
  173. diskutil is a command line interface to the Disk Utility app that comes
  174. with OS X. It can do everything its graphical cousin can, but it also
  175. has some extra capabilities--such as filling a disk with zeroes or
  176. random data. Simply type diskutil list to see the path names of disks
  177. and removable media attached to your machine, and then point the
  178. command at the volume you want to operate on. Be careful: diskutil can
  179. permanently destroy data if it's used incorrectly.
  180.  
  181. 8. brew
  182.  
  183. Alright-this isn't technically a native command. But no OS X power user
  184. should be without [15]Homebrew. The website calls it "The missing
  185. package manager for OS X," and that couldn't be truer. If you've ever
  186. used apt-get in Linux, you will feel right at home in Homebrew.^[16]3
  187.  
  188. brew gives you easy access to thousands of free utilities and libraries
  189. from the open source community. For instance, brew install imagemagick
  190. will set you up with [17]ImageMagick, a powerful utility that makes it
  191. possible to do anything from whipping up animated gifs to converting
  192. images between dozens of different types. brew install node will
  193. introduce you to [18]NodeJS, the hot new tool for developing and
  194. running server-side JavaScript apps.
  195.  
  196. You can have fun with Homebrew too: brew install archey will get you
  197. Archey, a cool little script for displaying your Mac's specs next to a
  198. colourful Apple logo. The selection in Homebrew is huge--and because
  199. it's so easy to create [19]formulas, new packages are being added all
  200. the time.
  201. [archey.png] Archey--My command line brings all the boys to the yard.
  202.  
  203. But the best part about Homebrew? It keeps all its files in a single
  204. directory: /usr/local/. That means you can install newer versions of
  205. system software, such as python and mysql, without interfering with the
  206. built-in equivalents. And if you ever want to get rid of your Homebrew
  207. installation, it's easy to remove.
  208.  
  209. Edit--May 15: Someone suggested to me that that you should not blindly
  210. delete the contents of /usr/local/. That's a fair precaution. To remove
  211. Homebrew safely, use the [20]uninstall script.
  212.  
  213. For more fun with Terminal.app, here is an A-Z list of [21]all
  214. available console commands in OS X 10.9 Mavericks.
  215. 1. Recall that OS X apps are not true executables, but actually
  216. special directories (bundles) with the extension .app. open is the
  217. only way to launch these programs from the command line. It can
  218. also launch other "files" that are truly bundles, such as Pages
  219. documents. [22]&#8617;
  220. 2. https://www.youtube.com/watch?v=G0FtgZNOD44 [23]&#8617;
  221. 3. In truth, Homebrew is more similar to FreeBSD's Ports system than
  222. Linux's apt. It uses a hybrid source/binary system: if no binary is
  223. available for a particular package it will simply download the
  224. source tarball and compile it--not a problem on today's multicore
  225. Macs. [24]&#8617;
  226.  
  227. [25] [PERMALINK]
  228.  
  229. [26]´ Coding for Journalists, Part 2: HTML -- Workflows, Nesting and
  230. Paragraphs
  231.  
  232. [27](And eight hundred more) ª
  233.  
  234. References
  235.  
  236. 1. http://mitchchn.me/feed.xml
  237. 2. http://www.mitchchn.me/
  238. 3. http://www.mitchchn.me/2014/os-x-terminal/
  239. 4. https://twitter.com/share?text=Eight%20Terminal%20Utilities%20Every%20OS%20X%20Command%20Line%20User%20Should%20Know%20via%20@mitchchn&url=http://mitchchn.me/2014/os-x-terminal/
  240. 5. http://www.mitchchn.me/feed.xml
  241. 6. http://www.mitchchn.me/archive
  242. 7. http://www.mitchchn.me/2014/and-eight-hundred-more/
  243. 8. http://www.mitchchn.me/2014/os-x-terminal/#fn:bundles
  244. 9. http://paul.annesley.cc/2012/09/mac-os-x-launchd-is-cool/
  245. 10. https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
  246. 11. http://www.peterborgapps.com/lingon/
  247. 12. http://www.apple.com/accessibility/osx/voiceover/
  248. 13. http://www.mitchchn.me/2014/os-x-terminal/#fn:mac
  249. 14. http://www.noodlesoft.com/hazel.php
  250. 15. http://brew.sh/
  251. 16. http://www.mitchchn.me/2014/os-x-terminal/#fn:compiling
  252. 17. http://www.imagemagick.org/
  253. 18. http://nodejs.org/
  254. 19. https://github.com/Homebrew/homebrew/wiki/Formula-Cookbook
  255. 20. https://gist.github.com/mxcl/1173223
  256. 21. http://ss64.com/osx/
  257. 22. http://www.mitchchn.me/2014/os-x-terminal/#fnref:bundles
  258. 23. http://www.mitchchn.me/2014/os-x-terminal/#fnref:mac
  259. 24. http://www.mitchchn.me/2014/os-x-terminal/#fnref:compiling
  260. 25. http://www.mitchchn.me/2014/os-x-terminal/
  261. 26. http://www.mitchchn.me/2014/coding-for-journalists-p2/
  262. 27. http://www.mitchchn.me/2014/and-eight-hundred-more/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement