from Hacker News

OS X Command Line Utilities

by brianwillis on 5/15/14, 5:25 AM with 235 comments

  • by cstross on 5/15/14, 8:28 AM

    One command I can't live without: textutil.

    Basically it's a command-line front end to Apple's TextKit file import/export library. Works with a bunch of rich text/word processor formats, including OpenDoc, RTF, HTML 5, and MS Word. Critically, the HTML it emits is vastly better than the bloated crap that comes out of Microsoft Word or LibreOffice when you save as HTML ...

    Install pandoc and multimarkdown as well and you've got the three pillars of a powerful, easy-to-use multiformat text processing system.

  • by eschaton on 5/15/14, 7:30 AM

    What always surprises me is that so many don't know or use the directory stack commands, pushd and popd. I'll admit I was also ignorant of them until something like 2005, but once I learned of them I switched and never looked back. Now I can't see someone write or type "cd" without a little bit of a cringe.
  • by ggreer on 5/15/14, 8:22 AM

    I didn't know about `screencapture`. That's a fun one.

    The Linux equivalent of `open` is `xdg-open`. I usually alias it to `op`, since `/bin/open` exists.

    Another bit of terminal-sugar for OS X users:

        alias lock='/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend'
    
    And most Linux users:

        alias lock='gnome-screensaver-command -l'
    
    If you find yourself accidentally triggering hot corners, the lock command is your savior.

    I've sorta-documented this stuff over the years, but only for my own memory. https://gist.github.com/ggreer/3251885 contains some of my notes for what I do with a clean install of OS X. Some of the utility links are dated, but fixing the animation times really improves my quality of life.

  • by greggman on 5/15/14, 7:57 AM

    These are awesome. I didn't know about many of them.

    One tiny thing though, at the bottom it says

    > Recall that OS X apps are not true executables, but actually special directories (bundles) with the extension .app. open is the only way to launch these programs from the command line.

    Actually, you can launch them in other ways. Example

        /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome  --user-data-dir=/Users/<username>/temp/delmechrome --no-first-run
    
    Will start a new instance of Chrome with it's own datastore in ~/temp/delmechrome. add some URL to the end to have it auto launch some webpage. Delete ~/temp/delmechrome to start over.
  • by pling on 5/15/14, 7:38 AM

    Another one that I can't live without:

       ssh-add -k keyfile
    
    Integrates with keychain meaning you can have a passworded private key without having to play around with ssh-agent and shells and profiles. Put keychain access in the menu bar and you can lock the keychain on demand as well. Integration of ssh into the OSX workflow is absolutely awesome.

    That and some of the examples in that article really make it a killer platform for Unix bits.

  • by wink on 5/15/14, 11:55 AM

    'open' is on of the things I long for most as a Linux user. There are several ways to achieve something that are all inferior or downright broken. Usually you don't have a huge problem, until you have. xdg-open for example could've solved this, if it was universally working.

    I wrote related rant once[0] when I tried to debug an issue of a misconfigured default browser.

    [0]: http://f5n.org/blog/2013/default-browser-linux/

  • by DCKing on 5/15/14, 1:17 PM

    Could you imagine Apple would have gone for BeOS or a custom developed kernel with no significant terminal-based userland when making OS X? It would probably still be used by many casual users or those doing graphical work, but I doubt it would be used by hackers at all.
  • by nmc on 5/15/14, 7:33 AM

    /usr/local is the default location for user-installed stuff, but I personally like to have my package manager do its stuff in a separate directory.

    I like the way Fink [1] uses the /sw (software) directory.

    Does anyone have a valuable opinion on the comparison between Fink and Homebrew — or maybe MacPorts?

    [1] http://www.finkproject.org

  • by Monkeyget on 5/15/14, 9:05 AM

    This was supposed to be few lines of remarks. It expanded quickly in relation with my enthusiasm for this topic.

    I've been investing some time in the command line on my Mac. I am moving from a dilettante going to the shell on a per-need basis to a more seasoned terminal native. It pays off handesomely! It's hard to convey how nice it to have to have a keyboard-based unified environment instead of a series of disjoined mouse-based GUI experiences.

    Here are some recommendations pertaining to mastering the command line on a Mac specifically:

    -You can make the terminal start instantaneously instead of it taking several seconds. Remove the .asl files in /private/var/log/asl/. Also remove the file /users/<username>/Library/Preferences/com.apple.terminal.plist

    - Install iterm2. It possesses many fancy features but honestly I hardly ever use them. The main reason to use it instead of the default Terminal application is that It just works©.

    -Make your terminal look gorgeous. It may sound superficial but it actually is important when you spend expanded period of time in the terminal. You go from this http://i.imgur.com/cx3zZL8.png to this http://i.imgur.com/MQbx8yK.png . You become eager to go to your terminal instead of reluctant. Pick a nice color scheme https://code.google.com/p/iterm2/wiki/ColorGallery . Use a nice font (Monaco, Source Code Pro, Inconsolata are popular). Make it anti aliased.

    -Go fullscreen. Not so much for the real estate but for the mental switch. Fullscreen mode is a way to immerse yourself into your productive development world. No browser, no mail, no application notification. Only code.

    -Install Alfred. It's the command line for the GUI/Apple part of your system. Since I installed it I stopped using the dock and Spotlight. Press ⌘+space then type what you want and it comes up. In just a few keystrokes you can open an application, open gmail/twitter/imdb/..., make a webs search, find a file (by name, by text content), open a directory,... It's difficult to describe how empowering it is to being able to go from 'I want to check something out in the directory x which is somewhere deep deep in my dev folders' to having it displayed in 2 seconds flat.

    -Make a few symlinks from your home directory to the directories you use frequently. Instead of doing cd this/that/code/python/project/ you just do cd ~/project.

    -Learn the shell. I recommend the (free) book The Linux Command Line: http://linuxcommand.org/tlcl.php . It guides you gently from simple directory navigation all the way up to shell scripting.

    -Use tmux. Essential if you want to spend some time in the terminal. You can split the window in multiple independent panes. Your screen will have multiple terminals displayed simultaneously that you can edit independently. For example I'll have the code in one side and on the other side a REPL or a browser. You can also have multiple windows each with its own set of panes and switch from on to the other. With the multiple windows I can switch from one aspect of a project to another instantly. E.g: one window for the front-end dev, a second one for the backend and another for misc file management/git/whatever.

    -Pick an editor and work towards mastery. I don't care if you choose vi or emacs. You'll be surprised how simple features can make a big change in how you type. You'll be even more surprised at how good it feels

    The terminal is here to stay. It's a skill that bears a lot of fruits and that deprecate slowly. The more you sow the more you reap.

  • by runjake on 5/15/14, 3:51 PM

    9. /usr/sbin/system_profiler

    10. /System/Library/CoreServices/Applications/Wireless Diagnostics (with built-in wifi stumbler)

    11. /System/Library/CoreServices/Screen Sharing.app (Built-in VNC client with hardware acceleration)

    12. /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources (Command-line wifi configuration and monitoring tool)

    Combine with sed, awk, and cut, and these tools can provide useful monitoring.

  • by torrent-of-ions on 5/15/14, 8:23 AM

    Another thing you can do to improve speed is learn the keybindings for readline. They are the same keybindings as emacs, and lots of other things use readline too like python shell, sqlite, etc. A very useful set of keys to have in your muscle memory. See the readline manual: http://tiswww.case.edu/php/chet/readline/rluserman.html#SEC3
  • by _jsn on 5/15/14, 7:30 AM

    mdfind / Spotlight can be a fairly powerful tool. Consider this query, which finds all Xcode projects I've tagged as "Active":

      ~$ mdfind tag:Active kind:xcode
      /Users/jn/Code/xyz/xyz.xcodeproj
      ...
    
    Queries like this also work in the Cmd-Space UI, or as a Saved Search. By default each term is joined with AND, but you can specify OR too.
  • by smw on 5/15/14, 10:09 AM

    Put this in your path somewhere, find files, links, directories instantly, with globbing. Makes mdfind actually useful.

      $ mdf "*invoice*.pdf"
      /Users/smw/Downloads/Invoice-0000006.pdf
    
    https://gist.github.com/smw/a21a9f675ed3358830da
  • by barbs on 5/15/14, 8:12 AM

    I use multiple POSIX environments (OS X at work, Linux Mint and Xubuntu at home), and I find it handy to create common aliases for differently implemented commands to keep the environments consistent.

    For example, I set the letter 'o' as an alias for 'open' on OS X, and to "thunar" on Xubuntu and "nemo" on Linux Mint.

  • by milla88 on 5/15/14, 7:11 AM

    My favorite command is 'say'. You can do all kinds of silly voices.

    Try this out: say hello -v Good

  • by pirateking on 5/15/14, 11:24 AM

    After years of living on the command line, OS X specifically, and learning its quirks and tricks, I am actually ready to move on.

    Right now I am more interested in creating simple visual interfaces on top of UNIX-y tools, for my own personal use cases. The main benefit of this is the ability to better experiment with and optimize my workflows for different properties as needed through different combinations of single responsibility interfaces and single responsibility programs.

    I am sensing that there is great promise in seeing much higher APMs (actions per minute) for many tasks, even compared to the all-powerful command line. Also, there are lots of interesting possibilities for better visual representations of data to improve comprehension and usability.

  • by ansimionescu on 5/15/14, 1:10 PM

    Additions:

    * lunchy: wrapper over launchctl, written in Ruby – https://github.com/mperham/lunchy

    * brew cask: "«To install, drag this icon...» no more", as they say – https://github.com/caskroom/homebrew-cask

    * have fun with "say" – https://github.com/andreis/different

  • by allavia88 on 5/15/14, 7:15 AM

    There's been a few of these lists over the past few years, most recent one is https://news.ycombinator.com/item?id=7494100

    It seems like a large portion of HN is less experienced re sysadmin, but is interested in it nonetheless. Perhaps there's room to make a 'codecademy for unix' type course? Curious to see what folks thing.

  • by jpb0104 on 5/15/14, 2:42 PM

    Here is a very handy script that takes a screenshot, places it in Dropbox's public directory, shortens the public URL, then puts the short URL in your clipboard. Making for very quick screenshot sharing. It combines a few of these hints. https://gist.github.com/jpb0104/1051544
  • by archagon on 5/15/14, 7:24 AM

    Great list! Includes all the old favorites with clear explanations.

    This is only tangentially related, but I recently wrote a little Automator Service to gather any selected file and folder paths from Finder. I very often need to grab the path of something for programming-related stuff, and doing it from the command line or with the mini-icon drag-and-drop takes way too long. Maybe somebody here will find it useful! http://cl.ly/1a3s3g1u2Q2w

  • by shurcooL on 5/16/14, 2:56 AM

    If you do Go development, you can do this to quickly get to root folder of any Go package:

      function gocd {
          cd `go list -f '{{.Dir}}' $1`
      }
    
    It uses the same syntax as go list to specify packages, so you can do, e.g.:

      ~ $ gocd .../markdownfmt
      markdownfmt $ pwd
      /Users/Dmitri/Dropbox/Work/2013/GoLand/src/github.com/shurcooL/markdownfmt
      markdownfmt $ _
    
    So nice.
  • by hibbelig on 5/15/14, 8:31 AM

    I want "remote pbcopy"! I'd like to be able to log in to any remote host (usually Linux in my case), then tack something onto the command line I'm typing to copy it into the pastebuffer.

        ssh somehost
        cd /some/dir
        grep -lr foo . | remote_pbcopy
    
    I guess something like this is possible with GNU Screen or with Tmux, and perhaps the Tmux/iTerm interaction helps, but I've never figured it out.
  • by js2 on 5/15/14, 1:05 PM

  • by RazerM on 5/15/14, 7:58 AM

    It seems odd to have

      open /Applications/Safari.app/
    
    as an example, when

      open -a safari
    
    does the same thing.
  • by gotofritz on 5/15/14, 4:45 PM

    Also worth mentioning is dotfiles (not specific to OS X). Basically various well known "power users" share their bash, homebrew, etc settings on github so that they can easily set up a new machine with minimum of fuss. There are a lot of neat trick in those boilerplate files.

    http://dotfiles.github.io/

  • by nicksergeant on 5/15/14, 7:23 PM

    Why the hell would someone change the title from "Eight Terminal Utilities Every OS X Command Line User Should Know" to "OS X Command Line Utilities".

    The original title is clearly more accurate / useful / canonical. The overwritten title is ambiguous. This is indeed not a list of every OS X command line utility.

  • by mmaldacker on 5/15/14, 7:28 AM

    no love for macport?
  • by vladharbuz on 5/15/14, 7:53 AM

    The screencapture options are wrong.

        "Select a window using your mouse, then capture its contents without the window’s drop shadow and copy the image to the clipboard:
    
        $ screencapture -c W"
    
    -c captures the cursor, W is not an option. The real command for this is:

        $ screencapture -C -o
  • by salgernon on 5/15/14, 4:10 PM

    pbpaste and pbcopy can specify multiple clipboards; one very handy thing I do is

    "cmd-a" "cmd-c" (copy all)

    double click on a word I'm looking for, "cmd-e" to enter it into the find clipboard

    'pbpaste | fgrep --color `pbpaste -pboard find`'

    I have that aliased as 'pbg'.

  • by stretchwithme on 5/15/14, 5:51 PM

    The hot keys for screen capture are more useful for daily use. You can paste what you've capture directly into most email clients. Or go to Preview where creating a new file uses what's on your clipboard if its an image.
  • by fuzzywalrus on 5/15/14, 5:08 PM

    Notably the screen capture terminal command, while neat, is sold as "more flexible". I think the author is unaware of Command+shift+4 followed by tapping the spacebar. It'll give you the window capture.

    Otherwise good article.

  • by chrismorgan on 5/15/14, 7:59 AM

    I don’t use a Mac, but have used espeak-via-ssh to deliver a message to my sister who was near my laptop, from the comfort of my bed… I could have (a) called out, or (b) gotten up, but where would the fun have been in that?
  • by chrisBob on 5/15/14, 3:09 PM

    The biggest change I have found for my terminal was adding this to my .bash_profile:

    export CLICOLOR=1

    export LSCOLORS=GxFxCxDxBxegedabagaced

    I thought that was one of the most amazing things when I used a linux system, but OS X is black and white by default.

  • by nemasu on 5/15/14, 7:08 AM

    This is neat. I'll be getting a mac soon, and this is right up my ally.
  • by huskyr on 5/15/14, 7:19 AM

    Awesome. I knew about `pbcopy`, but i never knew you could also pipe stuff into it. That saves a lot of time saving script outputs to temporary text files and copying!
  • by RexRollman on 5/15/14, 10:17 PM

    Personally, I was surprised that there is not a command line interface to OS X's Notification system. Seems like it would be handy for long running batch jobs.
  • by guard-of-terra on 5/15/14, 9:25 AM

    I wonder if it's possible to make OS X to say "As you request, Stan" in Lexx's voice.

    That alone might be sufficient reason to migrate from ubuntu.

  • by fmela on 5/15/14, 7:35 PM

    The '-name' argument of mdfind makes it useful to find files with the query string in the name. E.g.: "$ mdfind -name resume".
  • by conradev on 5/15/14, 11:18 AM

    The one utility I can't live without is caffeinate, which prevents a Mac from sleeping.

    It's super useful for keeping long running tasks running.

  • by cormullion on 5/15/14, 10:23 AM

    If you work with images a lot, look up sips. I use it a lot, for converting images, rescaling and resizing, etc.
  • by lastofus on 5/15/14, 8:20 AM

    The article doesn't mention the fun you can have with ssh + say.

    My co-workers and I used to ssh into the iMacs of non-technical users in our office and have a good laugh from a nearby room.

  • by hackeroni on 5/24/14, 1:56 AM

    Thanks for sharing