Category: bash

  • Convert svn repository to git

    mkdir ~/Sites/repo_tmp cd ~/Sites/repo_tmp git-svn init https://svn.example.com/repo/trunk/ –no-metadata git config svn.authorsfile ~/Sites/git-users.txt git-svn fetch git-svn show-ignore > .gitignore git add .gitignore git commit -m “Convert svn ignored files to .gitignore” cd ~/Sites git clone repo_tmp repo

  • use grep for either of two strings

    Options to look for several reg exp with grep. -i for case insensitive and -E for reg exp. grep -iE “(jpg|gif)” your-file-with-text.txt

  • Bash menu in bash with getopts

    A little more sophisticated script than the former bash menu example to get some arguments from command line. #!/bin/bash while getopts “u:p:” opt; do case $opt in u) echo “-u was triggered, Parameter: $OPTARG” dbuser=”$OPTARG” ;; p) echo “-p was triggered, Parameter: $OPTARG” dbpass=”$OPTARG” ;; \?) echo “Invalid option: -$OPTARG” exit 1 ;; 🙂 echo…

  • Shell script for domain searching binge

    This is an easy script to find out if a domain is available. It works with generic tld. #!/bin/bash while read i; do echo -n “$i is “; whois $i | grep -F “$(echo -e ‘No match\nNOT FOUND\n AVAIL’)” -q && echo ‘AVAILABLE! :D’ || echo ‘taken :(‘; done

  • How to rename a mysql database or table

    Here is a dirty script to rename a mysql database or table from command line. Dont for get to stop mysql service! cd /var/lib/mysql/ /etc/init.d/mysql stop # rename database… mv olddbname newdbname # …or table cd database/ mv oldname.frm newname.frm mv oldname.MYD newname.MYD mv oldname.MYI newname.MYI /etc/init.d/mysql start

  • Find latest file in a directory methods

    Sometimes there are so many files in a directory that it’s hard to find the last file in the directory. We show you here 2 ways for finding it out. With awk we print the last argument of the last line: $ ls -lrt | awk ‘{ f=$NF }; END{ print f }’ Reverse ordering…

  • how to batch file rename in bash shell

    This is a quick way of renaming a batch of files matching a reg exp in bash. ls foo*.jpg | awk ‘{print(“mv “$1” “$1)}’ | sed ‘s/foo/bar/2’ | /bin/sh

  • Vars expansion

    Some examples of how to work with variables in bash. ### Vars expansion with ${} ### ## Default value # If a variable is not set a default value will be used. If it’s set, # its value is used echo ${NAME:-Pepe} # -> Pepe NAME=”Juanje” echo ${NAME:-Pepe} # -> Juanje ## Assign a default value…

  • Bash cron job for ipfw

    With this lines in a cronjob,usually (/etc/cron.d/yourcronjob) in gnu/linux, you can add some rules to your firewall so no one can go out of your lan in the intervals specified, and then back to normal. In this case, ip 10.0.0.4 would be blocked. 0 21 * * 0-4 /sbin/ipfw add 1 deny all from 10.0.0.4…

  • Shell script menu

    This is an example of how to create a bash menu using while and read. #!/bin/sh echo “Use one of the following options” echo ” d : run date command” echo ” : ” echo ” q: quit this menu” echo “enter your choice or type “q” to quit : \c” read option while […