Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

The Swiss Army knives for Linux and Mac - they’ve thought of everything


Overview

This is a guided tour of Linux utilities, presented in as logical a sequence as I can imagine.

MacOS/Mac OS X uses the BSD version command line tools, which are different from the Linux version, while they are both compliant with POSIX standards.

See https://en.wikipedia.org/wiki/GNU_Core_Utilities.

Uptime

uptime
   
 6:35  up 9 days, 11:56, 7 users, load averages: 2.09 2.08 2.06
   

Load averages are also shown in the top command.

### Top processes

  1. To list the top hungry processes, and refersh the screen:

    top
    
  2. To cancel the display, press control+C.

    That works with any process you want to kill.

Processes

  1. So that we can kill it for fun, create a background process (by specifying &) which sleep for 999 seconds:

    sleep 999 &
    
  2. Get the process identifier:

    pgrep sleep
    
  3. List background processes:

    jobs
    
    [1]+  Running                 sleep 999 &
    

    The + shows the focus.

  4. To list all processes with a niceness (NI) column:

    ps -l
    

    Niceness of priority

    Default niceness of zero, but can be -20 to +19.

    PROTIP: A niceness of +19 is a priority of 99, which is lowest.

    nice -n 5 sleep 1000&
    

    root permissions are needed to set nice below zero.

  5. Reset nicer:

    renice -n 5 sleep 1000&
    

    Kill

  6. To kill a single progam by name, such

    pkill sleep
    
  7. To kill several progams by name, such

    killall sleep
    

Coreutils

Get the set of utilities from GNU Linux, but for Mac:

brew install coreutils
   

Then you can:

ln -s /usr/local/bin/gtac /usr/local/bin/tac
   

Cron Launchd Background Jobs

This mentions that Apple has deprecated cron in favor of launchd (a daemon running under the System context). Since it’s a background process, it doesn’t present a user interface.

If the system is turned off or asleep, cron jobs do not execute until the next designated time occurs.

However, launchd job will run when the computer wakes up if the computer is asleep when the job should have run (if the StartCalendarInterval key has been set).

Grep Utilities

My version of the Grep utility that filters what is piped into it:

grep –version

is

grep (BSD grep) 2.5.1-FreeBSD

NOTE: The Mac is a combination of BSD and Linux goodness.

Grep filters what is piped into it:

grep “my string of text” -R

Find the word “server” with case -insensitive in a file:

grep -i Server /etc/ntp.conf
   

The response contains the word “server” searched::

server time.apple.com.
   

Find lines that don’t (-v to reverse search) begin with # (specified by a ^) or blank lines (specified by a ^) to the end of line $:

grep -ve'^#' -ve'^$'/etc/ntp.conf
   

The response:

server time.apple.com.
   

Regular Expressions

We’ll use the spelling dictionary of English words that comes with Linux.

  1. Search for words ending with “fine”:

    grep 'fine$' /usr/share/dict/words
    

    $ (Shift+4) specifies search from the end of string.

    Responses include “refine”.

  2. Search for words beginng with “fine”:

    grep '^fine' /usr/share/dict/words
    

    ^ (Shift+6) specifies search from the beginning of string.

    Responses include “refine”.

  3. Search for “fine” anywhere within the line:

    grep 'fine' /usr/share/dict/words
    
  4. Search for lines containing “fine” anywhere within words:

    grep 'fine' /usr/share/dict/words
    

    Whitespace

  5. Search for a space before “system” in lines, such as either “system” or “file system”:

    grep '\ssystem' /etc/ntp.conf
    
  6. Search for any word boundary after “server” in lines, such as “servers” or “serverless”:

    grep 'server\b' /etc/ntp.conf
    

    PROTIP: The response “server time.apple.com” is the server used for Network Time Protocol used to update your machine’s clock.

  7. Return lines that do not start with # for comment:

    grep -v '^\s*#' /etc/hosts
    
  8. Search for specific characters C or c:

    grep '[Cc]'
    

    Quantifiers

  9. Search for “color” or “colour” in any line, using a combination, including a ? to specify the previous character u as an optional character:

    grep '\b[Cc]olou?r\b' /usr/share/dict/words
    

    u+ matches one or more occurrences.

    u* matches zero or more times.

    u{4} matches exactly four occurrences.

    enhanced grep

  10. Return words with five consequtive vowel characters:

    grep -E '[aeiou]{5}' /usr/share/dict/words
    

    BTW, “euouae” (pronounced your-you-ee”) consists only of vowels.

    Regex ranges

  11. Search for characters, upper and lower case ranges from A to Z, plus underscores:

    grep '[A-Za-z_]' ???
    
  12. Search for just numbers range 1 through 9:

    grep '[0-9]' ???
    

Sed

The Linux sed utility replaces contents within a file.

To delete empty lines and comment lines:

sed -i.bak ‘/^s*#/d;/^$/d’ somefile

The -i generates a backup.

The semicolon separates multiple specifications.

Install appium_console gem

gem uninstall -aIx appium_lib
gem uninstall -aIx appium_console
gem install --no-rdoc --no-ri appium_console

Install flaky gem:
https://github.com/appium/flaky
(posix-spawn)

gem uninstall -aIx flaky
gem install --no-rdoc --no-ri flaky

More on OSX

This is one of a series on Mac OSX: