Remove line from history with grep

Today I needed to clear history from some command. I looked into history and used grep to filtering command I needed.

for i in $(history | grep something | grep -oE "[0-9]{1,} " | sort -rn); do history -d ${i}; done

Don’t run script if it’s running

The script will start only in case if the same script is not running.

#!/bin/bash
if ps -ef | grep -v grep | grep "process name" ; then
    echo "process running"
    exit 0
else
    echo "process not running"
    exit 0
fi

Remove files / directories older than N days

Looked for the simple command today and here it is:

find <path to files> -mtime +N -type f -delete
find <path to directories> -mtime +N -type d -delete

Where +N is how many days ago.
So in case of 2 days it should be: -mtime +2

Get date with nanoseconds on MacOS

Recently I tried to run my own simple bash script (written on Linux) on my Mac and I found that results are bad because a “date” function.

So the problem is – date on MacOS doesn’t have nanoseconds option.

Solution is simple: install “coreutils” from MacPorts

$ sudo port install coreutils

that’s it!

Now GNU version of the date is available as gdate:

$ gdate +%s%N
1379413863366167000

Replace string in all textfiles in directory

I needed to find and replace string in all php files of current folder.
This is simple perl command:

find <path to directory> -type f | xargs perl -pi -e 's/old_string/new_string/g'