Category: Linux

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

  • Compress and Extract Files in Linux

    Extract:

    .tar.bz2
    To extract a tar (Tape ARchiver) file, type the following in the shell prompt:

    tar xvjf yourFileName.tar.bz2

    Which will untar it to the current directory. Depending on the file structure that being compressed, it might create sub-directories.
    Parameters:
    x – extract
    v – verbose output (lists all files as they are extracted)
    j – deal with bzipped file
    f – read from a file, rather than a tape device

    .tar.gz or .tgz

    tar xvzf file.tar.gz
    tar xvzf file.tgz

    Parameters:
    -x, –extract, –get extract files from an archive
    -v, –verbose verbosely list files processed
    -z, –gzip, –gunzip, –ungzip filter the archive through gzip
    -f, –file=ARCHIVE use archive file or device ARCHIVE

    .bz2 file

    tar jxf YOUR_FILE_NAME.bz2

    .zip file

    unzip YOUR_FILE_NAME.zip

  • Remote copy using scp

    Do you ever need to copy one file from one server to another? If you have ssh access to the remote server then you can do an scp command like so:


    $ scp remoteUsername@remoteServername:remoteFile localFile

  • Delete files older than X number of days in Linux

    Say you backup your database daily and you need to delete the old backup files.
    You can issue the command bellow which will erase any files within the specified directory that are older than 7 days.


    $ find /your/target/directory* -mtime +7 -exec rm {} \;

  • Calling a mysql query from linux or dos command line

    There are times when you want to call a mysql command from the bash or dos script then call this script in a scheduler (cron job).
    The example below will show you how to do so.

    mysql -uYOURUSERNAME -pYOURPASSWORD -DYOURDATABASE -e"CALL YOURSTOREDPROCEDURE('YOUR_SP_PARAMETER')"

    Explanation:
    -u is where you put your username, you can either put a space or no space at all after the -u (both works). As you can see I prefer not to put any space.
    -p is where you put your password.
    -D is your database name. That’s a capital D.
    -e this is where your query will go. It needs to be quoted (“). If it’s calling a stored procedure or stored function, you’ll need to use the CALL keyword, otherwise if it’s just a simple query don use the CALL keyword.

  • Remove all svn directory

    Subversion (SVN) sometimes clutter our source directory by creating .svn folder in each folder or sub folder that we version control.

    The easiest way to erase them is by using a bash command:


    find . -name ".svn" -type d -exec rm -rf {} \;

  • SVN Tricks in Linux

    To show colorized svn diff


    sudo aptitude install colordiff

    Then on your home directory there is a hidden file called .bashrc.
    Edit it.


    vim ~/.bashrc

    Append the following code at the end of the file

    svndiff()
    {
    svn diff "${@}" | colordiff
    }

    Reload the .bashrc file:

    ~/.bashrc

    Next to view the diff use svndiff instead of svn diff

    Removing all .svn subfolders


    rm -rf `find . -type d -name .svn`

    SVN Revert All


    svn st -q | awk '{print $2;}' | xargs svn revert

  • Displaying Linux Distro Version and Showing 32Bit or 64Bit OS

    Checking whether you have 32bit linux or 64bit linux. From the code below we can see that it is a 64 bit linux.


    [user1@ubuntu 17:14:37 ~/dev]$ uname -mrs
    Linux 3.2.0-30-generic x86_64

    Checking which distro you’re using:

    [user1@ubuntu 17:14:48 ~/dev]$ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description: Ubuntu 12.04.1 LTS
    Release: 12.04
    Codename: precise

  • Find and Replace File Content using Linux Command Line


    find . -name "*.pgsql" -print | xargs sed -i 's/STRINGTOREPLACE/REPLACERSTRING/g'

  • How To List All Users or Groups in Linux

    To list all users in Linux:

    cat /etc/passwd

    See only the first word before double colon (:)
    for exampple, user “root” will look like: root:x:0:0:root:/root:/bin/bash

    And to list all groups do the following

    cat /etc/group

  • Preventing Another Instance of A Program Running in Linux

    The code below is my solution for preventing a program from running more than once at the same time in Linux.
    The scenario: Consider if you have a cron job (jobA.sh) set to run at 6PM, 7PM, and 8PM.
    While the 1st instance of jobA.sh started, it might (or might not) need more than an hour to finish. If it does requires more than one hour, while the 1st instance is still running, a 2nd instance of jobA.sh started.
    Now you ended up with two instances of a same program consuming your CPU, files, etc – which will slow things down even more.

    So here how I solve it:
    When the 1st instance is started, check if there is a temporary lock file, if yes then just exits or quit. If not create the temporary lock file. And do your code/algorithm. After it finishes delete this lock file. When the 2nd instance started it will also do the same thing, checking if there is lock file.
    It will be wise to put another cron job that cleans all lock file every 00:00AM. Just to be sure the next execution (at 6PM tomorrow) will be able to run

    Since I’m no bash expert, let me know if there is a better way to do this. Cheers!


    #!/bin/bash
    # =================================
    # author: Ronald Pringadi
    # sleep command is in seconds
    # =================================
    vLockFile=/tmp/lockfile1.lock
    echo 'start'
    # If lockfile does not exist do the followings
    if [ ! -f $vLockFile ]; then
    touch $vLockFile
    echo "Lock file doesn't exists, executing code"
    sleep 7 # this can be your big process that takes longer than an hour, but just for proof of concept, I use 7 seconds
    rm $vLockFile
    else
    echo "Lock file exists do nothing"
    fi
    echo 'done'