Eight Terminal Utilities Every OS X Command Line User Should Know

Standard

http://www.mitchchn.me/2014/os-x-terminal/?x

The OS X Terminal opens up a world of powerful UNIX utilities and scripts. If you’re migrating from Linux, you’ll find many familiar commands work the way you expect. But power users often aren’t aware that OS X comes with a number of its own text-based utilities not found on any other operating system. Learning about these Mac-only programs can make you more productive on the command line and help you bridge the gap between UNIX and your Mac.

Update: Thanks to reader feedback, I’ve written about a few more commands in a follow-up post: (And eight hundred more).

1. open

open opens files, directories and applications. Exciting, right? But it really does come in handy as a command-line double-click. For instance, typing:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>open /<span class="hljs-constant">Applications</span>/<span class="hljs-constant">Safari</span>.app/
</code>

…will launch Safari as if you had double-clicked its icon in the Finder.

If you point open at a file instead, it will try to load the file with its associated GUI application. open screenshot.png on an image will open that image in Preview. You can set the -a flag to choose the app yourself, or -e to open the file for editing in TextEdit.

Running open on a directory will take you straight to that directory in a Finder window. This is especially useful for bringing up the current directory by typing open .

Remember that the integration between Finder and Terminal goes both ways – if you drag a file from Finder into a Terminal window, its full path gets pasted into the command line.

2. pbcopy and pbpaste

These two commands let you copy and paste text from the command line. Of course, you could also just use your mouse—but the real power of pbcopy and pbpaste comes from the fact that they’re UNIX commands, and that means they benefit from piping, redirection, and the ability to be in scripts in conjunction with other commands. Typing:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>ls ~ | pbcopy
</code>

…will copy a list of files in your home directory to the OS X clipboard. You can easily capture the contents of a file:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>pbcopy &lt; blogpost.txt
</code>

..or do something crazier. This hacked-up script will grab the link of the latest Google doodle and copy it to your clipboard.

<code class=" hljs coffeescript">$ curl <span class="hljs-attribute">http</span>:<span class="hljs-regexp">//</span>www.google.com<span class="hljs-regexp">/doodles#oodles/archive | grep -A5 'latest-doodle on' | grep 'img src' | sed s/</span>.*<span class="hljs-string">'&lt;img src="\/\/'</span><span class="hljs-regexp">/''/</span> | sed s<span class="hljs-regexp">/'" alt=".*'/</span><span class="hljs-string">''</span>/ | pbcopy
</code>

Using pbcopy with pipes is a great way to capture the output of a command without having to scroll up and carefully select it. This makes it easy to share diagnostic information. pbcopy and pbpaste can also be used to automate or speed up certain kinds of tasks. For instance, if you want to save email subject lines to a task list, you could copy the subjects from Mail.app and run:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>pbpaste &gt;&gt; tasklist.txt
</code>

3. mdfind

Many a Linux power user has tried to use locate to search for files on a Mac and then quickly discovered that it didn’t work. There’s always the venerable UNIX find command, but OS X comes with its own killer search tool: Spotlight. So why not tap into its power from the command line?

That’s exactly what mdfind does. Anything Spotlight can find, mdfind can find too. That includes the ability to search inside files and metadata.

mdfind comes with a few conveniences that make it stand out from its big blue brother. For instance, the -onlyin flag can restrict the search to a single directory:

<code class=" hljs ">$ mdfind -onlyin ~/Documents essay
</code>

The mdfind database should stay up to date in the background, but you can also troubleshoot it (as well as Spotlight) using mdutil. If Spotlight isn’t working the way it should, mdutil -E will erase the index and rebuild it from scratch. You can also turn off indexing entirely with mdutil -i off.

4. screencapture

screencapture lets you take many different kinds of screenshots. It’s similar to Grab.app and the keyboard shortcuts cmd + shift + 3 and cmd + shift + 4, except it’s far more flexible. Here are just a few different ways you can use screencapture:

Capture the contents of the screen, including the cursor, and attach the resulting image (named ‘image.png’) to a new Mail message:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>screencapture -<span class="hljs-constant">C</span> -<span class="hljs-constant">M</span> image.png
</code>

Select a window using your mouse, then capture its contents without the window’s drop shadow and copy the image to the clipboard:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>screencapture -c -<span class="hljs-constant">W</span>
</code>

Capture the screen after a delay of 10 seconds and then open the new image in Preview:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>screencapture -<span class="hljs-constant">T</span> <span class="hljs-number">10</span> -<span class="hljs-constant">P</span> image.png
</code>

Select a portion of the screen with your mouse, capture its contents, and save the image as a pdf:

<code class=" hljs bash">$ screencapture <span class="hljs-operator">-s</span> -t pdf image.pdf
</code>

To see more options, type screencapture --help

5. launchctl

launchctl lets you interact with the OS X init script system, launchd. With launch daemons and launch agents, you can control the services that start up when you boot your computer. You can even set up scripts to run periodically or at timed intervals in the background, similar to cron jobs on Linux.

For example, if you’d like to have the Apache web server start automatically when you turn on your Mac, simply type:

<code class=" hljs bash">$ <span class="hljs-built_in">sudo</span> launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
</code>

Running launchctl list will show you what launch scripts are currently loaded. sudo launchctl unload [path/to/script] will stop and unload running scripts, and adding the -w flag will remove those scripts permanently from your boot sequence. I like to run this one on all the auto-update “helpers” created by Adobe apps and Microsoft Office.

Launchd scripts are stored in the folllowing locations:

<code class=" hljs ruby">~<span class="hljs-regexp">/Library/</span><span class="hljs-constant">LaunchAgents</span>    
/<span class="hljs-constant">Library</span>/<span class="hljs-constant">LaunchAgents</span>          
/<span class="hljs-constant">Library</span>/<span class="hljs-constant">LaunchDaemons</span>
/<span class="hljs-constant">System</span>/<span class="hljs-constant">Library</span>/<span class="hljs-constant">LaunchAgents</span>
/<span class="hljs-constant">System</span>/<span class="hljs-constant">Library</span>/<span class="hljs-constant">LaunchDaemons</span>
</code>

To see what goes into a launch agent or daemon, there’s a great blog post by Paul Annesley that walks you through the file format. And if you’d like to learn how to write your own launchd scripts, Apple provides some helpful documentation on their Developer site. There’s also the fantastic Lingon app if you’d prefer to avoid the command line entirely.

6. say

This is a fun one: say converts text to speech, using the same TTS engine OS X uses for VoiceOver. Without any options, say will simply speak whatever text you give it out loud.:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>say <span class="hljs-string">"Never trust a computer you can't lift."</span>
</code>

You can also use say to speak the contents of a text file with the -f flag, and you can store the resulting audio clip with the -o flag:

<code class=" hljs bash">$ say <span class="hljs-operator">-f</span> mynovel.txt -o myaudiobook.aiff
</code>

The say command can be useful in place of console logging or alert sounds in scripts. For instance, you can set up an Automator or Hazel script to do batch file processing and then announce the task’s completion with say.

But the most enjoyable use for say is rather more sinister: if you have ssh access to a friend or coworker’s Mac, you can silently log into their machine and haunt them through the command line. Give ‘em a Siri-ous surprise.

You can set the voice (and language!) used by say by changing the default setting in the Dictation & Speech panel in System Preferences.

7. diskutil

diskutil is a command line interface to the Disk Utility app that comes with OS X. It can do everything its graphical cousin can, but it also has some extra capabilities—such as filling a disk with zeroes or random data. Simply type diskutil list to see the path names of disks and removable media attached to your machine, and then point the command at the volume you want to operate on. Be careful: diskutil can permanently destroy data if it’s used incorrectly.

8. brew

Alright–this isn’t technically a native command. But no OS X power user should be without Homebrew. The website calls it “The missing package manager for OS X,” and that couldn’t be truer. If you’ve ever used apt-get in Linux, you will feel right at home in Homebrew.

brew gives you easy access to thousands of free utilities and libraries from the open source community. For instance, brew install imagemagick will set you up with ImageMagick, a powerful utility that makes it possible to do anything from whipping up animated gifs to converting images between dozens of different types. brew install node will introduce you to NodeJS, the hot new tool for developing and running server-side JavaScript apps.

You can have fun with Homebrew too: brew install archey will get you Archey, a cool little script for displaying your Mac’s specs next to a colourful Apple logo. The selection in Homebrew is huge—and because it’s so easy to create formulas, new packages are being added all the time.


ARCHEY—My command line brings all the boys to the yard.

But the best part about Homebrew? It keeps all its files in a single directory: /usr/local/. That means you can install newer versions of system software, such as python and mysql, without interfering with the built-in equivalents. And if you ever want to get rid of your Homebrew installation, it’s easy to remove.

Edit—May 15: Someone suggested to me that that you should not blindly delete the contents of /usr/local/. That’s a fair precaution. To remove Homebrew safely, use the uninstall script.

For more fun with Terminal.app, here is an A-Z list of all available console commands in OS X 10.9 Mavericks.

How to create a quick ASCII banner in the OS X Terminal

Standard

From:http://www.macissues.com/2015/11/05/how-to-create-a-quick-ascii-banner-in-the-os-x-terminal/

Even if you are not a Terminal wizard, there are some fun tools and features of it that can be amusing. For instance, some online services are available for you to log into with Telnet and watch a text-based version of Star Wars, or you can play odd games that folks have coded into the “emacs” editor, among others. If you are ever sitting at your Mac and suddenly have the urge to print out a massive banner of a text phrase, then you can do that to.

First, open the Terminal, then type the following:

banner "MacIssues"

This will create a vertical banner in hash characters of the word “MacIssues” and you can change the text to whatever you would like, to have a banner of that printed. By default this will output to the Terminal window, but you can quickly route the output to a TextEdit document by piping the output with the following approach:

banner "MacIssues" | open -fe

In the text document that opens, if the banner appears garbles, then resize the window until the message is accommodated, and then print to whatever printer you have.

Granted there are plenty of free tools or those in common word processing and design programs that can do this, and often with greater style (drop shadows, colors, fonts, etc.); however, this is a quick way to punch out a banner on pretty much any Mac, at any time. Note that being a Terminal-based option, this approach is doable on many other Unix and Linux distributions as well, so if you’re ever indulging in your geek side and suddenly have the urge to make a banner, this is likely how you would do it.

TextEdit printing banner from the Terminal

Create Hidden Administrative Accounts In OS X From The Terminal

Standard

If you have a system that is used by other people, you may want to give them managed user accounts and then reserve a separate administrative account for installing apps and changing system settings. This is especially true for situations where many people may be using one computer, such as in classrooms. While you can always create an administrative account, by default such accounts will show up along with others at the login window, in the Fast User Switch menu, and other locations; however, you can set this up to be hidden from most of these locations.

The root account

One approach for a hidden user account is to enable the root account. However this being fully unrestricted comes with inherent risks. Even though admin accounts may authenticate for root access, this ability is time- or session-limited, meaning that authentication is regularly required to ensure administrative actions are truly desired. However, with the root account no such checks are done, and faulty administrative actions can be harmful. Since practically every root action can be done from an administrative account, its best to avoid enabling root unless some action cannot be done otherwise.

Dedicated administrative accounts

To create a special hidden user account that has administrative rights, you can go one of two routes based on the version of OS X that you have, but first you must create the account. This can either be done in the Users & Groups system preferences, or by using the command line (useful for scripting or remote-access approaches). For the second approach, open the Terminal and then run the following set of commands (replace USERNAME with the corresponding name of your account):

  1. Get a list of current User ID numbers:
    dscl . list /Users UniqueID
  2. Create the user’s account in the local directory:
    sudo dscl . create /Users/USERNAME
  3. Set the user’s password:
    sudo dscl . passwd /Users/USERNAME
  4. Set the user’s full name:
    sudo dscl . create /Users/USERNAME RealName "USER NAME"
  5. Set the user’s default shell:
    sudo dscl . create /Users/USERNAME UserShell /bin/bash
  6. Add the user to the “admin” group:
    sudo dscl . append /Groups/admin GroupMembership USERNAME

Now the following commands will create and assign the user’s home folder, which by default is in the /Users directory, but since this is a hidden account we are putting it in the hidden /var directory:

  1. Create the folder:
    sudo mkdir /var/USERNAME;
    sudo chown USERNAME /var/USERNAME
  2. Set the home directory:
    sudo dscl . create /Users/USERNAME NFSHomeDirectory /var/USERNAME
  3. Set the user’s ID to a value unique from the list of User IDs you found in the first step above (change NUM to reflect the value of your selected ID):
    sudo dscl . create /Users/USERNAME UniqueID NUM

If your version of OS X is prior to Yosemite, then you can set this unique value to something less than 500, and OS X should hide it. Otherwise, run the following command to have the login window hide users under 500:

sudo defaults write /Library/Preferences/com.apple.loginwindow Hide500Users -bool TRUE

Note that if you have created the user account in the Users & Groups system preferences, then you can change the User ID and home folder location in the system preferences by right-clicking the user and choosing the advanced options, then adjusting the values accordingly.

If your version of OS X is Yosemite or later, then you have additional approaches available to you for hiding the user account. Instead of being forced to use a User ID value under 500, you can use any ID you want and then set a special attribute for the user account that will hide it:

sudo dscl . create /Users/USERNAME IsHidden 1

To undo this change, re-run the command with “0” instead of “1,” or run the following command to remove the attribute altogether:

sudo dscl . delete /Users/USERNAME IsHidden

Finding hidden user accounts

While these approaches will hide a user account from the login window, the Users & Groups system preferences, and the Fast User Switching menu, you can still view the account. The following command will list the users on the system and then filter out system-based accounts, so you will see the short usernames of all the current users:

dscl . list /Users | grep -v "_\|nobody\|root\|daemon"

From:http://www.macissues.com/2015/11/17/how-to-create-hidden-administrative-accounts-in-os-x/