Create Multiple Nested Directories in Linux At Once

Did you know that you can create a complete directory tree with one command?
Let’s start with a simple example:

mkdir -p a/b/c

will create this tree:

.
└── a
    └── b
        └── c

And any of these:

mkdir -p a/{b1,b2}/c

or

mkdir -p a/b{1,2}/c

Will create this tree:

.
└── a
    ├── b1
    │   └── c
    └── b2
        └── c

And a little more complicated command:

mkdir -p a/b{1,2,3/c{1,2}}/d

Will create this tree:

.
└── a
    ├── b1
    │   └── d
    ├── b2
    │   └── d
    └── b3
        ├── c1
        │   └── d
        └── c2
            └── d

Lastly, this more realistic example:

mkdir -p doc/{text/,img/{wallpaper/,photos/}}

Will create this tree:

.
└── doc
    ├── img
    │   ├── photos
    │   └── wallpaper
    └── text

:)

Add Color To Man Pages

This will make the man pages colorful and (probably) easier to follow.

Add this to bashrc


export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;34m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'

Now try it

man printf

If you still don’t see colors, add this to bash_alias

alias man=’man -P less’

Now it should work.

Enjoy.

Bash Functions to Convert Between Numeric Representations

Add these to your bashrc file:

function h2d { echo “obase=10; ibase=16; $( echo “$*” | sed -e ‘s/0x//g’ -e ‘s/\([a-z]\)/\u\1/g’ )” | bc; }
function h2b { echo “obase=2; ibase=16; $( echo “$*” | sed -e ‘s/0x//g’ -e ‘s/\([a-z]\)/\u\1/g’ )” | bc; }
function b2d { echo “obase=10; ibase=2; “$*”” | bc; }
function b2h { echo “0x$(echo “obase=16; ibase=2;”$*”” | bc)”; }
function d2b { echo “obase=2; ibase=10; “$*”” | bc; }
function d2h { echo “0x$(echo “obase=16; ibase=10; “$*”” | bc)”; }

Now use as follows:

$ h2d 0xff
255

h2d means convert hexadecimal representation to decimal.
d2b means convert decimal to binary. Etc.

Add a Separator Between Commands in Terminal

Original post in lifehacker.com here.

The code for Mac: (paste to a file, and source from ~/.bash_profile)

############################################
# Modified from emilis bash prompt script
# from https://github.com/emilis/emilis-config/blob/master/.bash_ps1
#
# Modified for Mac OS X by
# @corndogcomputer
###########################################

# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill=”— “

reset_style=’\[\033[00m\]’
status_style=$reset_style’\[\033[0;90m\]’ # gray color; use 0;37m for lighter color
prompt_style=$reset_style
command_style=$reset_style’\[\033[1;29m\]’ # bold black
# Prompt variable:

PS1=”$status_style”‘$fill \t\n'”$prompt_style”‘${debian_chroot:+($debian_chroot)}\u@\h:\w\$'”$command_style “

# Reset color for command output
# (this one is invoked every time before a command is executed):
trap ‘echo -ne “\033[00m”‘ DEBUG

function prompt_command {

# create a $fill of all screen width minus the time string and a space:
let fillsize=${COLUMNS}-9
fill=””
while [ “$fillsize” -gt “0” ]
do
fill=”-${fill}” # fill with underscores to work on
let fillsize=${fillsize}-1
done

# If this is an xterm set the title to user@host:dir
case “$TERM” in
xterm*|rxvt*)
bname=`basename “${PWD/$HOME/~}”`
echo -ne “\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007”
;;
*)
;;
esac

}
PROMPT_COMMAND=prompt_command