Difference Between bin and sbin

Ever been curious about the difference between bin and sbin? The ‘s’ in sbin means ‘system’. Therefore, system binaries reside in sbin directories.

As you may have noticed, there are a number of different bin directories in Linux. The best reference I’ve found for an understanding of various Linux folders is man hier. It provides a brief explanation of the Filesystem Hierarchy Standard (FHS) in Linux. I’ve included a summary of the various bin and sbin definitions below:

/bin
    This directory contains executable programs which are needed
    in single user mode and to bring the system up or repair it.

/sbin
    Like /bin, this directory holds commands needed to boot the 
    system, but which are usually not executed by normal users.

/usr/bin
    This is the primary directory for executable programs. Most
    programs executed by normal users which are not needed for 
    booting or for repairing the system and which are not
    installed locally should be placed in this directory.

/usr/local
    This is where programs which are local to the site typically
    go.

/usr/local/bin
    Binaries for programs local to the site.

/usr/local/sbin
    Locally installed programs for system administration.

If you want to create your own scripts and make them available to all users, you’re pretty safe adding them to /usr/local/bin. If you want to run scripts using cron or crontab, simply use the full path to the command (i.e. /home/user/command).

What I do is add my scripts to my local bin (~/bin) and then I create a symbolic link in /usr/local/bin to the commands I want to make public. As a result, I can manage all my scripts from the same directory but still make some of them publicly available since /usr/local/bin is added to $PATH.

Hibernate from Terminal

To hibernate from terminal you must use the pmset command. However, pmset only allows you to set the hibernate mode. It does not immediately put your machine to sleep. An example:

sudo pmset -a hibernatemode 25

There are 4 power management options:

  1. -b  —  Battery
  2. -c  —  Charger
  3. -u  —  UPS
  4. -a  —  All of the above

There are a total of 7 hibernation modes. The first 3 modes are standard. The last 4 modes may be used, though they are NOT recommended (as per the pmset man page):

  1. hibernatemode 0 — The system will not back memory up to persistent storage. The system must wake from the contents of memory; the system will lose context on power loss. This is, historically, plain old sleep. In binary, 0000 0000.
  2. hibernatemode 3 —  by default on supported portables. The system will store a copy of memory to persistent storage (the disk), and will power memory during sleep. The system will wake from memory, unless a power loss forces it to restore from disk image. In binary, 0000 0011.
  3. hibernatemode 25 — The system will store a copy of memory to persistent storage (the disk), and will remove power to memory. The system will restore from disk image. If you want “hibernation” – slower sleeps, slower wakes, and better battery life, you should use this setting. In binary, 0001 1001.
  4. hibernatemode 1 — Enables hibernation; causes OS X to write memory state to hibernation image at sleep time. On wake (without bit 1 set) OS X will resume from the hibernation image. Bit 0 set (without bit 1 set) causes OS X to write memory state and immediately hibernate at sleep time. In binary, 0000 0001 (bit 0).
  5. hibernatemode 2 — Causes OS X to maintain system state in memory and leave system power on until battery level drops below a near empty threshold (This enables quicker wakeup from memory while battery power is available). Upon nearly emptying the battery, OS X shuts off all system power and hibernates; on wake the system will resume from hibernation image, not from memory. In binary, 0000 0010 (bit 1 in conjunction with bit 0).
  6. hibernatemode 8 — Encourages the dynamic pager to page out inactive pages prior to hibernation, for a smaller memory footprint. In binary, 0000 1000 (bit 3).
  7. hibernatemode 16 — Encourages the dynamic pager to page out more aggressively prior to hibernation, for a smaller memory footprint. In binary, 0001 0000 (bit 4).

I’ve listed the binary because the command can be executed using the binary instead, which may be easier to remember for the last four options.

Option 5 is a very practical alternative and may be the way apps such as SmartSleep function (Read about SmartSleep in a previous post). However, when I set my SmartSleep setting to to “smart sleep” and then check my hibernate mode using pmset -g, the mode still reads as “0”. At any rate, the concept is similar, if not the same.

To make hibernate mode setting a bit easier to remember, I’ve added the following aliases to my .profile that I can execute on the command line:

alias hibernate='sudo pmset -a hibernatemode 25'
alias sleep='sudo pmset -a hibernatemode 0'
alias safesleep='sudo pmset -a hibernatemode 3'
alias smartsleep='sudo pmset -a hibernatemode 2'

Now I can easily how my system responds when I close my MacBook lid.

There is one final caveat: if you have an app like SmartSleep installed, it will override any hibernate mode settings you make. In testing this, I noticed how I would set hibernatemode 25 and SmartSleep to “smart sleep,” and then when I woke my machine up hibernatemode was set to “0”. Strange, but true.

Leverage Git Config & Autocomplete Git Commands

Git Logo

I’ve already discussed customizing your shell and command prompt. To me, it is equally important to leverage Git configuration and autocomplete Git commands. You should also check out how to show the current Git branch in your Bash prompt.

Git Config

There are a lot of cool things you can do to customize Git just the way you like it. Most of these ideas are personalized versions of the git config customizations found at the Git website.

To create succinct, efficient commands in Git, create aliases for both the shell and for Git. Add the following code snippet to your .profile or .bash_profile:

alias gst='git status'
alias gco='git checkout'
alias gci='git commit'
alias grb='git rebase'
alias gbr='git branch'
alias gpl='git pull'
alias gpu='git push'
alias gad='git add -A'
alias gmt='git mergetool'
alias bdf='git diff'
alias glg='git log --date-order --all --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'
alias glg2='git log --date-order --all --graph --name-status --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'

Next add the following code to your ~/.gitconfig file:

[alias]
st = status
co = checkout
ci = commit
rb = rebase
br = branch
pl = pull
pu = push
ad = add
mt = mergetool
df = diff
lg = log --graph --name-status --oneline

Now reload your shell and you’re good to go. I’d also recommend you configure the following settings:

  • Color UI. Adds color to commands like git status so you can read the output more easily. It’s as simple as git config --global color.ui true.
  • Code Editor. I like to use vim, but Sublime would be a great alternative. git config --global core.editor vim.
  • Diff & Merge Tool. I downloaded the DiffMerge app for my MacBook Pro and the P4Merge for my Windows box. These tools allow me to compare code or resolve code conflicts when I run into them. This is worth the time it takes to set up before hand. Read how at Customizing Git – Git Configuration: External Merge and Diff Tools.

Below is what my .gitconfig file looks like now (Updated 2012-09-28):

[user]
        name = John Doe
        email = johndoe@doe.com
[alias]
        st = status
        co = checkout
        ci = commit
        rb = rebase
        br = branch
        pl = pull
        pu = push
        ad = add
        mt = mergetool
        lg = log --graph --name-status --oneline
[core]
        editor = vim
        #autocrlf = true
[color]
        ui = true
[merge]
        tool = kdiff3
        ff = true
[mergetool "kdiff3"]
        path = /usr/local/bin/kdiff3
        #path = C:/Program Files (x86)/KDiff3/kdiff3.exe

        guitool = kdiff3
[difftool]
        path = /usr/local/bin/kdiff3
        #path = C:/Program Files (x86)/KDiff3/kdiff3.exe

Autocomplete Git Commands

To add autocomplete for your Git commands, download the git-completion.bash file. The easiest way I know to do it is by using the following curl command in the shell:

curl https://github.com/git/git/raw/master/contrib/completion/git-completion.bash -OL

The -O options tells curl to output a local file with the same name as the remote file. Thus, the name of the file is extracted from the given URL.

The -L option allows curl to redirect if the appropriate location is indicated with a Location: header and a 3XX response code. curl will redo the request using the new location.

Once you get the the git-completion.bash file, find a place to store it permanently. I put mine with the rest of my shell scripts in ~/bin. Then add the following code snippet to your .profile or .bash_profile file:

source ~/git-completion.bash

No Interfaces Available in Wireshark

If you can’t see any interfaces in Wireshark on your Mac, it’s probably a permissions issue. Try running Wireshark from the command line as root:

su root
open -a "Wireshark"     # assumes app is in Applications folder

If you don’t have the password to root but you are an admin user and (hopefully) you know your password, try editing the permissions on the appropriate Wireshark network sniffers:

cd /dev
sudo chown `whoami`:admin bp*
ls -la | grep bp

I didn’t have the root password so this last one worked for me.

Customize Your Shell & Command Prompt

As mentioned in a previous post, we received some new MacBooks and a Mac Mini at work. Since most of my team prefers using PCs, I was able to get my hands on one. I immediately noticed how different it was from the one I use at home, so I started customizing it right away. I found I had forgotten how to do a couple things and it took me longer than I would have liked to search the web, so I’ve decided to dedicate a short post on how to customize your shell and command prompt in Mac OS X.

If you use Linux or Windows (think cygwin or git bash) this may apply to you too. If you don’t use any sort of shell, well, then you might just want this for future reference.

Apps

Terminal is the default app that comes with Mac OS X. Another great app is iTerm 2 (Free). It adds a lot of functionality that some users find lacking in Terminal.

General Preferences

Some programs that run the shell allow you to set the window size and buffer (essentially scrolling inside the limitations of the window). This is really helpful to setup before hand since lines that are too long will word wrap if you don’t have a large window buffer. This will inevitably happen at some point and it’s really annoying when it does, so take steps to prevent it now.

If you find you navigate to a specific directory every time you open the shell, it may be a good idea to tell the app to navigate to that directory when you open the shell. In Terminal, this can be found at Preferences >> Settings >> Shell; in iTerm 2 this can be found at Preferences >> Profiles >> General. There are a lot of other cool features (like window groupings) that you should checkout.

Appearance

The next thing you’re going to want to do is customize your shell’s color. I like the traditional black background with white or light gray text and some colorful highlighting like green or even just a plain grey.

For my shell’s font, I like to use Monaco 10pt.  Smaller text let’s me see more on the screen since I usually only let my shell take up one half of the screen. I enable bold fonts and bright colors for bold fonts, but I disable anti-aliasing (smooth edges) because I like that raw hacker feel ;).

.profile, .bash_profile or .bashrc

Some of the most important customization takes place in the .profile file. Every time your shell loads, it will run the commands found in the “profiles.” There are a number profiles some system-wide (e.g. /etc/profile), others personal (e.g. .profile). The bash man page provides useful information about the differences under the “INVOCATION” section:

When bash is invoked as an interactive login shell, or as a non-
interactive shell with the --login option, it first reads and executes
commands from the file /etc/profile, if that file exists. After
reading that file, it looks for ~/.bash_profile, ~/.bash_login, and
~/.profile, in that order, and reads and executes commands from the
first one that exists and is readable. The --noprofile option may be
used when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the
file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash
reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
these files exist. This may be inhibited by using the --norc option.
The --rcfile file option will force bash to read and execute commands
from file instead of /etc/bash.bashrc and ~/.bashrc.

Then at the bottom of the man page:

FILES
       /bin/bash
              The bash executable
       /etc/profile
              The systemwide initialization file, executed for login shells
       /etc/bash.bashrc
              The systemwide per-interactive-shell startup file
       /etc/bash.bash.logout
              The systemwide login shell cleanup file, executed when a login shell exits
       ~/.bash_profile
              The personal initialization file, executed for login shells
       ~/.bashrc
              The individual per-interactive-shell startup file
       ~/.bash_logout
              The individual login shell cleanup file, executed when a login shell exits
       ~/.inputrc
              Individual readline initialization file

In essence, .bash_profile is read upon login and .bashrc is read for each new shell opened since you can have multiple shell sessions running at once without logging in again. In Mac OS X, the .bash_profile overrides the .profile. I’ve run into some problems with .profile in the past, so I’ve actually switched everything to .bash_profile.

The next four sections will discuss:

  • How to change prompt escapes (bash)
  • How to change prompt color (bash)
  • How to create your personal “bin”
  • How to create aliases

Change Prompt Escapes

First, I like to customize the prompte e. I can’t stand it when the prompt is white and blends in with the rest of the text in the shell. The appearance of the prompt is stored in the environment variable $PS1. Try typing echo $PS1 in your shell. The text you see is a string coded with the display setting for your shell’s prompt. It might look something like this:

\h:\W \u$

In this example, the \h represents the host computer, \W the working directory and \u the current user. All this information makes sense if you were to use the CLI a lot. “Back in the old days,” people would interface between various servers or computers over a network (esp. in business scenarios). When you’d change to a different server, you’d want to know the host computer you were accessing. Not all the computers had GUIs. Thus \h would let you know which computer you were on; whether you were on yours or another.

The \u is common for similar reasons. Sometimes you use the su command to substitute user and you’ll want to know which user you are acting as.

The \W should be self explanatory. You don’t want to have to type pwd or ls all the time to know where you are at in the file hierarchy.

In my prompt, I’ve gotten rid of the host symbol (I don’t switch hosts often and when I do, the other prompt is usually different enough that I can tell I’m on a different machine) and replaced it with the history number prompt escape (\!). This escape let’s you know which number in the command history you have just typed. That way if you see a previous command that you’d like to repeat a couple lines up you just type !<number>. To view your complete command history, type the command history. A simplified version of my prompt looks like this:

\! \u:\W$

Here’s a comprehensive list of prompt escapes to add to your prompt:

\a         # an ASCII bell character (07)
\d         # the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format} # the format is passed to strftime(3) and the result
           # is inserted into the prompt string an empty format
           # results in a locale-specific time representation.
           # The braces are required
\e         # an ASCII escape character (033)
\h         # the hostname up to the first '.'
\H         # the hostname
\j         # the number of jobs currently managed by the shell
\l         # the basename of the shell's terminal device name
\n         # newline
\r         # carriage return
\s         # the name of the shell, the basename of $0 (the portion following
           #   the final slash)
\t         # the current time in 24-hour HH:MM:SS format
\T         # the current time in 12-hour HH:MM:SS format
\@         # the current time in 12-hour am/pm format
\A         # the current time in 24-hour HH:MM format
\u         # the username of the current user
\v         # the version of bash (e.g., 2.00)
\V         # the release of bash, version + patch level (e.g., 2.00.0)
\w         # the current working directory, with $HOME abbreviated with a tilde
\W         # the basename of the current working directory, with $HOME
           # abbreviated with a tilde
\!         # the history number of this command
\#         # the command number of this command
\$         # if the effective UID is 0, a #, otherwise a $
\nnn       # the character corresponding to the octal number nnn
\\         # a backslash
\[         # begin a sequence of non-printing characters, which could be used
           #   to embed a terminal control sequence into the prompt
\]         # end a sequence of non-printing characters

Change Prompt Color

To color code your prompt on a Mac, use the following template:

\[\033[COLOR_CODE_HERE\]PROMPT_ESCAPE_OR_TEXT_HERE\[\033[0m\]

Most Linux distributions use a little different format:

\e[COLOR_CODE PROMPT_ESCAPE\e[0m

The first portion before the desired prompt escape or text only begins painting the chosen color (e.g., \[\033[1;34m\]). To stop painting a color, you have to reset to another color or turn color off (e.g., \[\033[0m\]).

Here’s a comprehensive list of color encoding:

# Regular Colors
\[\033[0;30m\] # Black
\[\033[0;31m\] # Red
\[\033[0;32m\] # Green
\[\033[0;33m\] # Yellow
\[\033[0;34m\] # Blue
\[\033[0;35m\] # Purple
\[\033[0;36m\] # Cyan
\[\033[0;37m\] # White

# High Intensty
\[\033[0;90m\] # Black
\[\033[0;91m\] # Red
\[\033[0;92m\] # Green
\[\033[0;93m\] # Yellow
\[\033[0;94m\] # Blue
\[\033[0;95m\] # Purple
\[\033[0;96m\] # Cyan
\[\033[0;97m\] # White

# Background
\[\033[40m\] # Black
\[\033[41m\] # Red
\[\033[42m\] # Green
\[\033[43m\] # Yellow
\[\033[44m\] # Blue
\[\033[45m\] # Purple
\[\033[46m\] # Cyan
\[\033[47m\] # White

# High Intensty backgrounds
\[\033[0;100m\] # Black
\[\033[0;101m\] # Red
\[\033[0;102m\] # Green
\[\033[0;103m\] # Yellow
\[\033[0;104m\] # Blue
\[\033[10;95m\] # Purple
\[\033[0;106m\] # Cyan
\[\033[0;107m\] # White

#Replace any leading leading 0; with 1; for bold colors
#Replace any leading 0; with 4; to underline

Once you’ve decided on the appropriate prompt add export PS1=”<custom prompt>” to your .profile. For example, this is what the line in my .profile looks like:

export PS1="\[\033[1;34m\]\!\[\033[0m\] \[\033[1;35m\]\u\[\033[0m\]:\[\033[1;35m\]\W\[\033[0m\]$ "

Add Personal “bin” to the PATH Variable

Every now and again you may want to create your own custom commands, scripts or programs for the CLI. Instead of mixing these in with the rest of the OS’s, just create your own personal “bin” folder and add it to your PATH variable so that you can run those commands from any folder in the shell.

export PATH=$PATH:/Users/Taylor/bin

Create & Use Aliases

Aliases are really nifty. They can save you a lot of extra effort for frequently used and/or lengthy commands. For example, I found that I liked to use ls -lhaG a lot more than just ls as follows:

alias ls='ls -lhaG'

Alias long commands that you’d forget or never want to type. I use Git to version my code. The git log command is very powerful and can include a lot of options. Instead of typing the various options every time, I use an alias called glg:

alias glg='git log --date-order --all --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'

Conclusion

At the end of the day, this is what my .bash_profile looks like:

UPDATED 2014-04-02

##################
### MY ALIASES ###
##################

# git command autocompletion script
source ~/bin/git-completion.bash

# git commamands simplified
alias gst='git status'
alias gco='git checkout'
alias gci='git commit'
alias grb='git rebase'
alias gbr='git branch'
alias gad='git add -A'
alias gpl='git pull'
alias gpu='git push'
alias glg='git log --date-order --all --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'
alias glg2='git log --date-order --all --graph --name-status --format="%C(green)%H%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'

# ls alias for color-mode
alias lh='ls -lhaG'

# lock computer
alias lock='/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend'

# hibernation and sleep settings
alias hibernate='sudo pmset -a hibernatemode 25'
alias sleep='sudo pmset -a hibernatemode 0'
alias safesleep='sudo pmset -a hibernatemode 3'
alias smartsleep='sudo pmset -a hibernatemode 2'

# up 'n' folders
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

# simple ip
alias ip='ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2'
# more details
alias ip1="ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'"
# external ip
alias ip2="curl -s http://www.showmyip.com/simple/ | awk '{print $1}'"

# grep with color
alias grep='grep --color=auto'

# proxy tunnel
#alias proxy='ssh -D XXXX -p XXXX USER@DOMAIN'
# ssh home
#alias sshome='ssh -p XXXX USER@DOMAIN'

# processes
#alias ps='ps -ax'

# refresh shell
alias reload='source ~/.bash_profile'

###############################
### ENVIRONMENTAL VARIABLES ###
###############################

# Add homebrew sbin to PATH variable
export PATH=$PATH:/usr/local/sbin

# Add personal bin to PATH variable
export PATH=$PATH:/Users/Taylor/bin    # May be redundant; check ~/.bash_profile, /etc/profile, /etc/paths, /etc/bashrc

# Show dirty state in prompt when in Git repos
export GIT_PS1_SHOWDIRTYSTATE=1

# Change prompt
PS1_OLD=${PS1}
export PS1='\[\033[1;34m\]\!\[\033[0m\] \[\033[1;35m\]\u\[\033[0m\]:\[\033[1;35m\]\W\[\033[0m\] \[\033[1;92m\]$(__git_ps1 "(%s)")\[\033[0m\]$ '

What have you done to customize your shell or change your command prompt?

Hide Desktop Icons on Mac

Ever wanted to hide your desktop icons briefly and easily on Mac OS X for a presentation, screencast or just to hide everyday clutter? Here’s a simple Bash script you can use.

#!/bin/bash

# checks visibility and stores value in a variable
isVisible="$(defaults read com.apple.finder CreateDesktop)"

# toggle desktop icon visibility based on variable
if [ "$isVisible" = 1 ]; then
	defaults write com.apple.finder CreateDesktop -bool false
else
	defaults write com.apple.finder CreateDesktop -bool true
fi

# force changes by restarting Finder
killall Finder

Paste that into a text editor and save it without a suffix/filetype as something like desktop. Then execute the following command while in the folder where you saved the script (preferably in your personal bin directory): chmod 755 desktop. You should be good to go as long as the directory you saved it in is mapped to your $PATH (If you aren’t sure what that means, read the 4th and 5th paragraphs of Clock In, Clock Out).

You can find some simple apps to do this if you are repelled by the Terminal or love extra menubar buttons; just search Google. Some of them cost money (CamouFlage – $1.99), though I don’t know who would pay for something so simple. There are some other free alternatives.

Clock In, Clock Out

I’ve worked (and presently work) a number of jobs where I have to keep track of my own hours. As with any repetitive task, the answer is to automate. So tracking your hours isn’t hard or time consuming, but having your computer do it for you while learning a little scripting appeals to me more. Thus, I concocted a nice little bash shell script with a pinch of Ruby–for the heavy lifting.

The script/program is simple. It’s called clock. All you do is type clock into the Terminal (mac) and…WAHLA! You’re clocked in. When you want to clock out, type clock again and you’re clocked out. Each time you clock in or out, the program records a timestamp with the date and time. After each clock out, it records the number of hours worked during that session. When it’s time to submit your timecard so you can receive your paycheck, simply type clock -f to finalize your timecard. This will sum your session hours and rename the timecard with the start and end date so you know the period.

I’ve included a zip archive of the files (clock, timeadd.rb, timediff.rb) so if you find yourself in the same boat you can give the program a shot. I suggest you add them to your own “bin” folder in your home directory like I’ve done. If you aren’t familiar with this concept, let me give a short explanation.

Dedicating a directory in your home folder is convenient if you like to write various little programs/scripts. It allows you to…segregate these from the built in programs/scripts that come with your OS so they are easily accessible and you remember which ones are yours. To make these programs work just like the rest of the programs (e.g. ls, rm, less, pwd, etc.) add the directory to your $PATH as follows:

export PATH=$PATH:/Users/USERNAME/bin

Of course, be sure to replace USERNAME with the obvious. You can type this command into the CLI or you can just add it to your .profile file in your home directory. If you don’t have a .profile file, go ahead and create one. Every time you open Terminal (or another CLI), the .profile file is read and executed, thus you will never have to type in those commands again manually.

Two more things to make this work:

  1. Make sure to open clock in a text editor and change the path where the timecard-current.txt will be saved (in addition to any other paths).
  2. Make sure to install the appropriate gems for the Ruby scripts. Simply type the following into Terminal:
    gem install chronic_duration time_diff

If you have any thoughts, suggestions or questions, please comment. I value feedback.