Category: Linux

You got to love Linux. Not only that it is free, but it is also very strong, stable, and widely used.

  • MySQL Backup and Restore – Using Command Line

    Here is how you backup database using command line. The first line is to backup and the second line is to restore.
    These commands work on both Windows and Linux. In Windows you might want to setup mysql bin directory the environment PATH, you can do this easily by pressing Window + Break > Advanced System Settings > Environment > PATH. Then run the cmd or the command prompt. If you already open a command prompt before you setup the PATH, you need to restart your command prompt

    MySql doesn’t export stored procedure/function by default. It also doesn’t export triggers. If you need those, the you’ll need to add “–routines –triggers” to your command. Even if you don’t use stored procedure, function, or triggers it is safer if you issue your backup with those option enabled.


    $ mysqldump -uusername -ppassword --routines --triggers --lock-tables=false database_name > YOURSQLFILE.sql
    $ mysql -uusername -ppassword database_name < YOURSQLFILE.sql

    If you need to backup all database. The user that we usually use to backup all database is "root". This will backup all triggers, and functions. You will need to pre-create other users that has certain privilege over stored procedure or functions.

    mysqldump -uusername -ppassword --opt --events --all-databases > alldb.sql
    mysql -uusername -ppassword < alldb.sql

  • Delete All Symlink in The Current Directory

    find . -maxdepth 1 -type l -exec rm -f {} \;

  • Benchmark How Long a Program Runs In Linux Using Bash

    The following bash code might come handy for those of you who want to benchmark how long does a program runs.
    In the following example the assumption is that you want to pass along two parameters for your program to run properly.
    A time will be recorded at the beginning and at the end of the execution. And a time diff, the processing time, will be reported in seconds.


    #!/bin/bash
    ##################################################
    # Benchmark the processing time when a program
    # executes with two required parameters: parameter1 parameter2
    ##################################################

    if [[ $1 = "" || $2 = "" ]]; then
    echo "Usage: `basename $0` parameter1 parameter2"
    else
    _start_time=`date +%s`
    _parameter1=$1
    _parameter2=$2
    ### YOUR COMMAND HERE WITH parameter1 AND parameter2 ###
    _end_time=`date +%s`
    _processing_time=$((_end_time-_start_time))
    echo "Source File: $_parameter1"
    echo "Destination File: $_parameter2"
    echo "Start time: $_start_time"
    echo "End time: $_end_time"
    echo "Processing time is: $_processing_time"
    fi

  • Auto Login Using SSH Public and Private Keys

    Assume that your main server (the one you use the most) is ServerA.
    And from ServerA, you want to automatically login to ServerB.
    For illustration purpose, ServerB can be a repo server and you don’t want to keep being asked for password everytime you want to commit files.

    1. At ServerA issue: # ssh-keygen -t rsa
    This will create a public + ptivate key for ServerA
    2. Enter (empty) for paraphrase
    Your identification has been saved in /home/ronald/.ssh/id_rsa.
    Your public key has been saved in /home/ronald/.ssh/id_rsa.pub.
    3. Copy over the newly created public key from ServerA to ServerB
    scp .ssh/id_rsa.pub ronald@ServerB:~/id_rsa.pub.ServerA
    Note that once the file arrives at ServerB, it named as id_rsa.pub.ServerA (not id_rsa.pub), and it will be stored at ronald’s home directory at ServerB.

    4. Ssh to ServerB, and issue this command:
    cat id_rsa.pub.ServerA >> .ssh/authorized_keys
    ServerA public key will be merged/appended to ServerB authorized_keys file.

    This way everytime you want to access ServerB from ServerA, you won’t be asked for a password.

  • Linux Soft Symlink Folder or Directory


    $ ln -s /home/rpringad/somefolder /home/rpringad/newfolder

    #or if your current directory is already /home/rpringad:
    $ ln -s /home/rpringad/somefolder /home/rpringad/newfolder

    $ ln -s existingSourceFolder newLinkedFolder

  • How To Know Which Linux Distribution You Are Using?

    There are three commands that you can use, pick one of them:

    cat /etc/issue

    cat /proc/version

    dmesg | head -1

  • How to Set up an FTP Server in Ubuntu Linux

    Login to your Linux shell menu as root


    [root@locahost]# apt-get install vsftpd
    [root@locahost]# vim /etc/vsftpd.conf

    At VIM, 1st comment out anonymous_enable by adding a # sign at the beginning of the line
    # anonymous_enable=YES
    2nd remove comment at local enable by removing the # sign
    local_enable=YES


    [root@locahost]# /etc/init.d/vsftpd restart

  • Adding color to your tail

    Tail is a very useful tool for monitoring error stream. Sometime the output from tail can have too much information and its black and white monotone output can be hard to follow with the eyes.

    The basic: Linux terminal has the capability to colorize text.
    for example:
    $ echo -e “Sample text nicely highlighted”
    will produce a simple text saying, well:
    Sample text nicely highlighted

    But with some terminal color tagging such as:
    $ echo -e “Sample \e[36mtext\e[0m nicely highlighted”
    The \e above is identical with \033. I would suggest use \033 because it is safer to be used with a programming language such as PHP. PHP will not recognize \e but it will recognize \033, you’ll see below.
    $ echo -e “Sample \033[36mtext\033[0m nicely highlighted”
    Sample text nicely highlighted

    Some basic coloring table can be seen here: http://www.bashguru.com/2010/01/shell-colors-colorizing-shell-scripts.html

    Color Foreground Background
    Black 30 40
    Red 31 41
    Green 32 42
    Yellow 33 43
    Blue 34 44
    Magenta 35 45
    Cyan 36 46
    White 37 47

    With the help of perl, which most likely comes with all linux distros, here is a way to colorize your tail output. Lets’ assume that every time you log something you will have a date-time prefixing your log. For example:

    [22-Dec-2011 20:28:45] E_DATASOMETHING ……
    Too much information …….
    Too much information …….
    [22-Dec-2011 20:28:46] E_FATAL Something
    Too much information …….
    Too much information …….
    Too much information …….

    Lets create a script that can colorize the date portion.
    Create an executable linux bash file:


    $ touch logwatch.sh
    $ chmod 755 logwatch.sh
    $ vim logwatch.sh

    The copy and paste the following to your empty logwatch.sh

    #!/bin/bash
    vNow=$(date +"%d-%b-%Y")
    tail -f ~username/errorlogfile.txt|perl -pe "s/$vNow/\e[1;30;32m$&\e[0m/g"

    Voilà!

    [22-Dec-2011 20:28:45] E_DATASOMETHING ……
    Too much information …….
    Too much information …….
    [22-Dec-2011 20:28:46] E_FATAL Something
    Too much information …….
    Too much information …….
    Too much information …….

    If you need something more complex, say you want to highlight several words on the file you can use a scripting power of PHP, I’m rusty with my pearl.
    Anyhow, save the script below as “colorize.php”, and you can have the words: blah, na, wa, and — highlighted on the fly!

    $ tail -f test.txt|php colorize.php blah na wa --


  • How to see open ports in Linux

    How to see open ports in Linux

    nmap -sS -O 127.0.0.1

  • Find Files in Linux by Name

    To find files in linux with a certain name portion or pattern we can issue a locate locate command.
    If locate is unable to find a matching file, then you might need to update your file index database.

    # locate journal-2011-07
    # updatedb

    In the example above we are trying to find a file that has journal-2011-07 in its name which can be journal-2011-07-01, journal-2011-07-02, my-journal-2011-07-09, etc.

    You can also use the find command. The find command does not use file indexing algorithm which might be slower than locate.
    The example below will look for all files with xml extension inside /home/ronald.
    The find command by default is recursive or it will look into subfolders.

    find /home/ronald/ -name *.xml