Blog

  • Bash: Find all .hidden files but ignore .htaccess file

    A bash command to search for all hidden files recursively in the current directory but ignore apache .htaccess files.

    find . -type f \( -iname ".*" ! -iname ".htaccess" \)

  • Split and join files

    How to split a file in chunks and how to put them back together.

    ### SPLIT LONG:
    split --bytes=5m --suffix-length=2 --numeric-suffixes image.iso image.iso.part_
    split --verbose --bytes=5m --suffix-length=2 --numeric-suffixes image.iso image.iso.part_

    ### SPLIT SHORT:
    split -b 5m -a 2 -d image.iso image.iso.part_
    split --verbose -b 5m -a 2 -d image.iso image.iso.part_

    ### RESTORE
    cat image.iso.part_* > image.iso

  • Snippet to check root privileges

    Snippet to use in your scripts to execute them only if you have root privileges.
    if [ $UID -ne 0 ]; then
    echo "Superuser privileges are required to run this script."
    echo "e.g. \"sudo $0\""
    exit 1 fi

     

  • HTTP Proxy Checker in bash

    Usefull script  to check if a proxy is alive.

    #!/bin/bash
    # HTTP Proxy Server's IP Address (or URL)
    proxy_server=$1

    # HTTP Proxy Server's Port Number
    port=$2

    # We're trying to reach this url via the given HTTP Proxy Server
    # (http://www.google.com by default)
    url="http://www.google.com"

    # Timeout time (in seconds)
    timeout=20

    # We're fetching the return code and assigning it to the $result variable
    result=`HEAD -d -p http://$proxy_server:$port -t $timeout $url`

    # If the return code is 200, we've reached to $url successfully
    if [ "$result" = "200 OK" ]; then
    echo "1 (proxy works)"
    # Otherwise, we've got a problem (either the HTTP Proxy Server does not work
    # or the request timed out)
    else
    echo "0 (proxy does not work or request timed out)"
    fi

  • Fetch last.fm song from bash

    This script let you get a song from last.fm.

    curl -s http://www.last.fm/user/$LASTFMUSER | grep -A 1 subjectCell | sed -e 's#<[^>]*>##g' | head -n2 | tail -n1 | sed 's/^[[:space:]]*//g'

  • Google URL shortener script

    Maybe it’s no longer working

    #!/bin/bash
    # Google url shortener bash script
    # For information about the app:
    # http://ggl-shortener.appspot.com/instructions/

    app='http://ggl-shortener.appspot.com/?url='
    url="$1"
    protocol=`echo "$1" | sed -e "/^http:\/\//g"`

    if [ -z "$1" ]; then
    echo -e "you need to pass the url through an argument";
    echo -e "e.g. `basename $0` http://url";
    else
    if [ ! "$protocol" ]; then
    curl -s "$app$url" | sed -e 's/{"short_url":"//' -e 's/"}/\n/g'
    else
    repl=`echo "$1" | sed -e 's/^/http:\/\//g'`
    curl -s "$app$repl" | sed -e 's/{"short_url":"//' -e 's/{"error_message":"Bad request/error: bad request/' -e 's/"}/\n/g'
    fi;
    fi

  • Remove RSA key for ssh authorized hosts file

    How to remove an ssh host by name or ip from your authorized hosts file.

    ssh-keygen -R {server.example.com}
    ssh-keygen -R {ssh.server.ip.address}

  • Analyse Access Log with awk for most common ip addresses

    Some cat with awk and then sort to find out what ips are most common in your apache access.log file. Remember that all ips are include here. Bots, crawlers, clients…
    cat /path/to/log | awk '{print $3}' | sort | uniq -c | sort -rn | head -20

  • Empty a database table command

    How to empty a mysql database. Forget about delete * command
    #!/bin/bash mysql -D $DATABASE -e 'TRUNCATE TABLENAME;'
     

  • SSH Access Email Alert for bashrc

    You want to know instantly who has logged in your ssh server. You can be emailed automatically including in .bashrc file of the user you want to monitor following commands:

    echo 'ALERT - Root Shell Access (hostname) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" [email protected]