Git – One more chance to fix not-yet-pushed commits

Git has a really cool feature: Interactive rebase. Roughly, it allows you to “rewrite history” of your (not pushed) commits.

It allows you to:

1. Change the order of commits

2. Change commit messages

3. Merge (Squash) commits.

4. Split commits.

Below is a link to an article where you’ll find how to do these:

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


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


Bash Fork Bomb

Warning: DON’T run this code, unless you understand what it’s going to do.

This is bash fork bomb:


If you run it, it will keep forking a process and stuck you operating system. You’ll most likely have to reboot to get your computer back }:-D

Why does it work?
Well, the trick is in the colon “:”. A colon is a valid name of a bash function. So the above code is equivalent to this:

bomb() {bomb | bomb&}; bomb

and after indentation:

bomb() {
bomb | bomb&;

It’s not easier to see what is happening here:
We defined a bash function called “bomb”. It forks itself recursively, and sends one of its instances to the background.

Why run in the background?
If we run the recursive call in the foreground, the calling function will not complete until the call returns.
Since there is no stop condition for this recursion, the calling function would wait forever, and we’ll have only a single instance of the function running. Killing it would be as easy as ctrl+C.
On the other hand, when running the recursive call in the background, the calling function will complete immediately, and the recursion can continue.
(It’s almost impossible to kill all these instances of the bomb even with the kill or killall commands, because the number of processes will grow exponentially and will utilize the whole CPU in a very short time. Leaving us with the only option: Reboot)

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.


Bash – Trimming a String Stored in a Variable

Assume we have this bash variable  (The colors will be used to show which parts of the string are not removed)

x=”tic toc tic toc

We have the following manipulations available:


Removes the shortest match of the pattern “toc*” from the end of the string.


Removes the longest match of the pattern “toc*” from the end of the string.


Removes the shortest match of the pattern “*tic” from the beginning of the string.


Removes the longest match of the pattern “*tic” from the beginning of the string.


$ echo ${x%toc*}
tic toc tic
$ echo ${x%%toc*}
$ echo ${x#*tic}
toc tic toc
$ echo ${x##*tic}

Enjoy :-)

Copy a Directory Along With Its Relative Path

How to copy a directory from path A to path B, preserving its relative path?

For example, if the directory is found at:


and we want to copy it to


We want the result look like this:


The trick is to use the –parents flag of the “cp” command. So this command will do the job:

cp -r --parents old/path/to/dir new/place/.

And we’re done.

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

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

Vim – Automatically Make Scripts Executable

Add this to your vimrc:

au BufWritePost * if getline(1) =~ “^#!” | if getline(1) =~ “/bin/” | silent !chmod a+x | endif | endif

Now, whenever you save a file which has its first line starting with #! (e.g. #! /bin/python), it will become executable.

Enjoy :-)

Rerun a Bash Command With Different Parameters (Advanced Bang Bang)

You probably know that you can use “bang bang” (that is: !!) to rerun your last command.
Sometimes you use it if you forgot to prefix a privileged command with sudo.

$ touch /etc/test
touch: cannot touch ‘/etc/test’: Permission denied
$ sudo !! #rerun the last command with sudo before it.

Now assume you want to rerun the last command, but want to change a parameter you passed to it.
For example

echo 1.2 && echo 2.3 && echo 4.2

You want to run the same command but with each instance of the number 2 replaced with 5.
This is how you do it

!! :gs/2/5

That’s it. Enjoy