Archive for the ‘ bash ’ Category

Author : beriba | 03/27/2018
#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/opt/project/project.jar
PID_PATH_NAME=/tmp/${SERVICE_NAME}-pid
LOG_FILE=/var/log/${SERVICE_NAME}.log
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR >> $LOG_FILE 2>&1&
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR >> $LOG_FILE 2>&1&
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac
Author : beriba | 03/25/2018
tr ' ' "\n" < FILE | grep "string_you_want_to_count" | wc -l

Just install:

sudo apt-get install kio-extras

It works on Kubuntu 16.04 so it’ll probably also work on Ubuntu 16.04. I also found out that it may work on Ubuntu 15.04.

Author : beriba | 08/08/2016
sudo apt-get install exfat-fuse exfat-utils

Then just mount exFAT device.

There is a simple trick to specify a tab as a separator. You simply have to write it as:

$'\t'

For example:

cat in.txt | sort -t $'\t' -k 2

It should work in all commands where you can specify separator.

Author : beriba | 02/01/2014

Using virtual machines is a very common practice. I’ll not tell you here the pros and cons of using them because it’s not the point of this post. One of the most popular VM tools is VirtualBox. While using linux VM you sometimes need to share some data between host and VM. Of course VirtualBox has a functionality to do that. From host (assuming that it is Windows) this folder is instantly accessible. But from VM this folder is only accessible by root. Adding sudo to every cp or any other command using shared folder isn’t what we really want. There is a solution. You just need to add your user to vboxsf group. You can do that by executing

sudo usermod -a -G vboxsf myusername

But that’s not everything. Now you just have to re-login (or reboot). But sometimes there are reasons to not reboot the VM. There’s also solution for that. You just have to do an explicit login.

su - myusername

There’s only one thing you have to remember. Until doing a re-login, you have to do an explicit login in every terminal session you open.

Author : beriba | 09/02/2013

Have you ever had too many GIT branches? Have you ever looked for a way to remove half of them or even all? If „Yes” is an answer for any of those two questions, this tip is for you. Probably you’ll have about 3 questions about removing git branches:
1. Is that possible? Yes
2. Is it built-in? No
3. How to do that? Look below

git branch -D `git for-each-ref --format="%(refname:short)" refs/heads/bugs_\*`

A little bit of explaination. Above command will remove permanently (without asking!) any branch that starts with „bugs_” (without quotes of course). You have to put backslash before * (asterisk), ? (question mark) and probably before few more characters. If you don’t know what is „git for-each-ref” feel free to ask me in a comment.

Recently I had to check how big the directory is. But not in the obvious way. The obvious way would be

du -h /tmp

The above command will output size of the catalog in human readable way (-h param) – in kB, MB, GB and so on. This is easy. But what if you want to check the size only for certain filetype within specified directory? It’s getting a little bit harder. To do that I prepared a command, the big one. Actually it’s not one command but five commands giving output to each other. It goes like this:

find /tmp -name '*.zip' 
    | xargs du 
    | awk '{sum[$3]+= $1;}END{for (s in sum){print sum[s], s;}}' 
    | sort -k2 
    | column -t

First command looks for all *.zip files in /tmp directory. Of course you can specify any rule which is acceptable by find command (search for „man find” in your favourite search engine) especially another filetype. Second command takes all found files and returns their sizes in bytes. Third command sums all of those sizes using awk (read more about it, it’s really powerful). Then, there is sorting by second column (indexes in -k argument goes from 1, not from 0) and after that there’s some pretty-printing. All you need to change (path and file type) is in the first command. If you need more explanation or some help, please leave a comment.

Enjoy 🙂