Saturday, December 20, 2008

Using locate and updatedb in Cygwin

updatedb --prunepaths='/proc /cygdrive/[^/]*'
locate 'filename'

Sunday, December 7, 2008

Why is my variable disappearing in my script?

It took me a while to figure this out. Sometimes my variable's value did not show up in my script in the following scenarios. It turned out I have a trailing underscore after it, like this:
export VAR1="Hello"
export VAR2="World"

# WRONG: Interpreted as $VAR1_
echo "Create a file called $VAR1_$VAR2.log"

# CORRECT: Use quotes or brackets
echo "Create a file called ${VAR1}_${VAR2}.log"
echo "Create a file called "$VAR1"_"$VAR2".log"

Monday, November 10, 2008

Linux Tasks and Processes

To list Process Status (ps):
$ ps -ef     # --everyone, --full
root         1     0  0 Nov01 ?        00:00:00 init [3]
root         2     1  0 Nov01 ?        00:00:00 [migration/0]
root         3     1  0 Nov01 ?        00:00:00 [ksoftirqd/0]

$ ps -al     # --all (same as -e), --long
     4516       1    4516       4516    ? 117771   Nov  4 /usr/bin/mintty
     5280    4516    5280       4412    2 117771   Nov  4 /usr/bin/bash
     5444    5280    5444        456    2 117771   Nov  4 /usr/bin/ssh
     6480       1    6480       6480    ? 117771   Nov  8 /usr/bin/mintty

Saturday, November 8, 2008

My Favorite Bash Settings


# source the system wide bashrc if it exists
if [ -e /etc/bash.bashrc ] ; then
  source /etc/bash.bashrc

# source the users bashrc if it exists
if [ -e "${HOME}/.bashrc" ] ; then
  source "${HOME}/.bashrc"

# Set PATH so it includes Android SDK Tools
if [ -d "/cygdrive/c/sdk/android-sdk-windows/tools" ] ; then

# Set Prompt to 'username@hostname:pwd\n'
export PS1="\[\e[0;33m\]\u\[\e[0m\]@\[\e[31m\]\h\[\e[39m\]:\w\n$ \[\e]2;\h:${PWD}\a\]"


# Default to human readable figures
alias df='df -h'
alias du='du -h'

# Misc :)
alias less='less -r'                          # raw control characters
alias whence='type -a'                        # where, of a sort
alias grep='grep --color'                     # show differences in color
alias grep='grep --color --exclude-dir=.svn'  # and exclude '.svn' folder
alias rm='rm -i'

# Some shortcuts for different directory listings
if [[ "$(uname)" = "Linux" ]]; then
  alias ls='ls -hF --color=tty'                 # classify files in colour
  alias dir='ls --color=auto --format=vertical'
  alias vdir='ls --color=auto --format=long'
alias ll='ls -l'                              # long list
alias la='ls -A'                              # all but . and ..
alias l='ls -CF'                              #

# Programs and Binaries shortcuts
alias emacs=/cygdrive/c/bin/emacs-23.2/bin/runemacs
alias firefox="cygstart /cygdrive/c/Program\ Files/Mozilla\ Firefox/firefox.exe"
alias mysql="cygstart /cygdrive/c/xampp/mysql/bin/mysql.exe"
alias mysqldump='/cygdrive/c/xampp/mysql/bin/mysqldump.exe'
alias php="/cygdrive/c/xampp/php/php.exe"
alias junction='/cygdrive/c/bin/junction.exe'

# Customized per workstation
alias xampp_start=/cygdrive/c/xampp/xampp_start.exe
alias xampp_stop=/cygdrive/c/xampp/xampp_stop.exe
alias xampp_restart=/cygdrive/c/xampp/xampp_restart.exe
alias todo="emacs ~/docs/secure/"

Thursday, September 11, 2008

Keyboard Shortcuts for Cygwin

Cygwin is default to the same key bindings as in Emacs. To change it to Vi key bindings, run set -o vi.
Desired ActionShortcut Keys
Auto Complete[Tab]
Move to Beginning of Line[Ctrl]a
Move to End of Line[Ctrl]e
Clear Screen[Ctrl]l that's L
Clear Line before cursor[Ctrl]u
Clear Line after cursor[Ctrl]k
Clear Word before cursor[Ctrl]w
Clear Word after cursor[Alt]d
Yank/Paste from Clipboard[Ctrl]y
Capitalize Current Word[Alt]u
Change Current Word to Lower Case[Alt]l
Capitalize First Letter[Alt]c
Cancel Changes and Restore the Line as in History[Alt]r

Friday, August 8, 2008

Bash One-Liners

To tar and gzip in one line (if -z option is not available, like on HP-UX.)
# Compress and Tar
$ tar cvf -  | gzip -c > tgzfile.tar.gz

# Untar and Decompress
$ gzip -dc tgzfile.tar.gz | tar xvf -
To search and replace in multiple files,
perl -pi -w -e 's/search/replace/g;' *.html

     -e means execute the following line of code.
     -i means edit in-place
     -w write warnings
     -p loop
To convert multiple JPEG files into one PDF file, first, install ImageMagick, then run:
convert *.jpg -adjoin [file].pdf

To repeat the last command entered, except with sudo prepended to it, run:
$ sudo !!

Redirect output to file with sudo
$ sudo bash -c "echo 'Hello world' > helloworld.txt"
# -OR-
$ echo "Hello world | sudo tee helloword.txt

To find out CPU / memory information
$ cat /proc/cpuinfo
$ cat /proc/meminfo

To find out diskspace
$ df -h

A command that returns value of a symbolic link or canonical file name.
$ readlink /etc/localtime

'awk' command examples:
# Print username with ':' as field-separator, -F fs --field-separator=fs
$ awk -F: '{ print $1 }' /etc/passwd

# Calculate the sum of the first column in file
$ awk '{ sum += $1 }; END { print sum }' file

# Reverse a list of arguments
$ echo 'col1 col2 col3' | awk '{ print $3, $2, $1 }'
col3 col2 col1

$ echo 'col1:col2:col3' |awk -F: '{ print $3 ":" $2 ":" $1}'

Check if a process is running, e.g. httpd
$ ps ax | grep -v grep | grep httpd
$ ps ax | grep -v grep | grep httpd > /dev/null && echo 'httpd is running.'
# OR
$ ps ax | grep -v grep | grep httpd > /dev/null || echo 'httpd is not running.'

Thursday, July 3, 2008

Ways to Force HTTPS on HTTP

  1. Add this to your httpd.conf:
    SSLRequire %{HTTP_HOST} eq ""
  2. Or try this:
    # Turn on Rewriting
    RewriteEngine on 
    # Apply this rule If request does not arrive on port 443
    RewriteCond %{SERVER_PORT} !443 
    # RegEx to capture request, URL to send it to (tacking on the captured text, stored in $1), Redirect it, and this is last rule.
    RewriteRule ^(.*)$$1 [R,L]
  3. Append the following line to httpd.conf:
    Redirect permanent /
  4. Or try this:
        Redirect permanent /
  5. Add the following lines to .htaccess:
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    Make sure you have this line in your httpd.conf file:
    LoadModule rewrite_module modules/
  6. Or try this:
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$$1 [R,L]
Note: Restart Apache for new configuration to take effect.
$ /etc/init.d/httpd restart

Sunday, June 8, 2008

Create MySQL Dump and Restore It

To Dump

A Single Database:
$ mysqldump -u root -p database_name > dumpfile.sql
 - OR -
$ mysqldump -u root -p --databases database_name > dumpfile.sql
All Databases:
$ mysqldump -u root -p -A > dumpfile.sql
 - OR -
$ mysqldump -u root -p --all-databases > dumpfile.sql
Selected Databases:
$ mysqldump -u root -p -D database_1 database_2 database_3 > dumpfile.sql
 - OR -
$ mysqldump -u root -p --databases database_1 database_2 database_3 > dumpfile.sql
A Table in a Database:
$ mysqldump -u root -p --databases database_name --tables table_name > dumpfile.sql

Zip it at the same time:
$ mysqldump -u root -p --opt database_name | gzip > dumpfile.sql.gz

See here for explanations of "--opt" option.

Create and zip remotely, pipe it and unzip
(create .my.cnf in user's home directory for password)
$ ssh user@host "mysqldump database_name | gzip -9" | gzip -d > dumpfile.sql

To Restore

$ mysql -u root -p database_name < dumpfile.sql

# For compressed dump
$ gunzip < dumpfile.sql.gz | mysql -u root -p database_name

Monday, June 2, 2008

Setting Up Users on MySQL

Create a New User:
mysql> CREATE USER 'user1'@'localhost' IDENTIFIED BY 'password';
mysql> CREATE USER 'user1'@'%' IDENTIFIED BY 'password';
If you know the password hash, you can add a PASSWORD prefix to your call like this:
mysql> CREATE USER 'user1'@'%' IDENTIFIED BY PASSWORD '*93840205958...';
Make sure you know what is the default password encoding in use:
mysql> SHOW VARIABLES LIKE 'old_passwords';
| Variable_name | Value |
| old_passwords | OFF   |
1 row in set (0.00 sec)

Grant Privileges to a User:
-- Grant everything to user1 from any host
-- You can shorten ALL PRIVILEGES with ALL
mysql> GRANT ALL ON *.* TO 'user1'@'localhost';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'user1'@'localhost';

Create a New User and Grant Privileges in One Line:

mysql> GRANT ALL ON *.* TO 'user1'@'localhost' IDENTIFIED BY 'password';
To check privileges for an account:
mysql> SHOW GRANTS FOR 'user1'@'localhost';
| Grants for user1@localhost                                         |
| GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'user1'@'localhost' |

To reset password:
mysql> SET PASSWORD FOR 'user1'@'localhost' = PASSWORD('mypassword');

mysql> SET PASSWORD FOR 'user1'@'localhost' = OLD_PASSWORD('mypassword');

mysql> SET PASSWORD FOR 'user1'@'localhost' = 'password-hash-string';

Remove User's Privilege

mysql> REVOKE ALL PRIVILEGES ON *.* FROM 'user1'@'localhost';

Wednesday, May 14, 2008

How to Set up Password-less SSH'ing?

Create RSA key pairs:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/joe/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/joe/.ssh/id_rsa.
Your public key has been saved in /home/joe/.ssh/
The key fingerprint is:
00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 joe@mycomputer
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
Copy your public key to host server:
$ scp ~/.ssh/
Then, log into your host server and create/append the public key to a file named, authorized_keys:
$ cat ~/.ssh/ >> ~/.ssh/authorized_keys
$ chmod go-w ~/. ~/.ssh/authorized_keys
$ chmod go-rwx ~/.ssh
NOW, logout and log back in. You won't be asked for the password again!

Tuesday, May 6, 2008

Bash Script Exit Codes

Exit Code NumberMeaningExampleComments
1catchall for general errorslet "var1 = 1/0"miscellaneous errors, such as "divide by zero"
2misuse of shell builtins, according to Bash documentationSeldom seen, usually defaults to exit code 1
126command invoked cannot executepermission problem or command is not an executable
127"command not found"possible problem with $PATH or a typo
128invalid argument to exitexit 3.14159exit takes only integer args in the range 0 - 255
128+nfatal error signal "n"kill -9 $PPID of script$? returns 137 (128 + 9)
130script terminated by Control-CControl-C is fatal error signal 2, (130 = 128 + 2, see above)
255*exit status out of rangeexit -1exit takes only integer args in the range 0 - 255

According to the table, exit codes 1 - 2, 126 - 165, and 255 [1] have special meanings, and should therefore be avoided as user-specified exit parameters. Ending a script with exit 127 would certainly cause confusion when troubleshooting (is the error a "command not found" or a user-defined one?). However, many scripts use anexit 1 as a general bailout upon error. Since exit code 1 signifies so many possible errors, this might not add any additional ambiguity, but, on the other hand, it probably would not be very informative either.
There has been an attempt to systematize exit status numbers (see /usr/include/sysexits.h), but this is intended for C and C++ programmers. A similar standard for scripting might be appropriate. The author of this document proposes restricting user-defined exit codes to the range 64 - 113 (in addition to 0, for success), to conform with the C/C++ standard. This would allot 50 valid codes, and make troubleshooting scripts more straightforward.

Friday, May 2, 2008

How to Change the Thumbnail Picture of a Folder in Windows 7 and Vista?

In the Windows Explorer window, when user chooses to display the files and folders listing in Extra Large Icons, Large Icons, Medium Icons and Tiles view, Windows 7/Vista can show a preview thumbnail on the icons of folders itself using content inside the folders, normally image and photo or file type icon if there is no graphic file inside the folder.

The thumbnail preview picture on folders’ icon listing in Windows Explorer window allows user to have a quick yet comprehensive glance about the file contents that may contains inside the folders. However, sometimes, the thumbnail preview image is not the one that user wants. In this case, user can customize or self define a picture to be used, placed and showed on the folder icon.
  1. In Windows Explorer, at folder parent level, right click on the folder that want to change its preview image on folder icon, and then select Properties.
  2. Click on Customize tab.
  3. Under Folder pictures section, click on Choose File button.
  4. In “Browse” dialog box appears, select an image file to represent the folder and to show on the folder icon when viewing directory listing in supported views, and click Open.
  5. The selected picture will be used as thumbnail preview image on folder icon with immediate effect.
  6. To revert the setting (remove the user-set folder picture, click on Restore Default button instead.

Alternative Method

  1. Copy an image in .JPG or .GIF format that user want to use as folder picture into the folder itself.
  2. Rename the picture file name to Folder. For example, Picture1.jpg should be renamed to Folder.jpg and Picture1.gif should be renamed to Folder.gif.
  3. Optionally, set the folder picture file (Folder.img) with hidden attribute to prevent it from displaying when listing folder’s contents.
  4. To revert and restore the original Windows 7/Vista automatic assigned folder pictures, just delete the Folder.img file.
  5. Note that for alternative method, .BMP and .PNG image formats is not supported.

Tuesday, April 29, 2008

Bash Programming Samples

To declare script to use bash shell,
To produce some interesting debugging output information,
#!/bin/bash -x
Set the passed arguments to an array, e.g. $ bar
args = ("$@")   # args = bar
Use "$#" to get the number of arguments, e.g.
if [ "$#" -ne 1 ]; then
  echo "Must have exactly one argument."
  exit 1
Find out if a file exists
if [ -f "foo.txt" ]; then
  cat foo.txt

# Check if file does not exist
if [ ! -f "foo.txt" ]; then
  echo "foo.txt not found!"
To get the return code from previous command, use $?

if [ $? -eq 0 ]
  echo "command ran successfully."
  echo "somecommand failed." >&2
  exit 1

if [ $? -ne 0 ]; then
  # output to stderr
  echo "[`date`] [error]somecommand failed." >&2
  exit 1
# output to stdout
echo "[`date`] [notice]somecommand succeeded."
To add timestamp to a command's output:
$ somecommand | sed "s/^/[`date`] /"

# Timestamp the error message, too
$ somecommand 2>&1 | sed "s/^/[`date`] /"
To check to see if an environment variable is set, use -z to check for zero-length string:
if [ -z "$TEST_VAR" ]
    export $TEST_VAR="some value"
To unset an environment variable:
$ unset TEST_VAR

To print a newline with echo, use these option: (Note: Linux/Unix does not interpret carriage return char: \r)
       -n     do not output the trailing newline
       -e     enable interpretation of backslash escapes
$ echo -e "Hello\nWorld "
$ echo -ne "Hello\nWorld "
World $
To get the script's base name and directory:
SCRIPTNAME=`basename $0`
SCRIPTDIR=`dirname $0`

Tuesday, April 1, 2008

Configuring color scheme for the 'ls' command

I have been having a hard time with the blue color in a Cygwin terminal (mintty) when I 'ssh' to other servers. It turns out I could fix this problem on a Linux server pretty easily by running this command:
$ dircolors -p > ~/.dircolors
I can also edit this file, '.dircolors', to customize my color scheme. Remember to re-login to see the changes.

As I research a little deeper, I found out how this file is being loaded. I see this code in "/etc/bashrc":
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . $i
                . $i >/dev/null 2>&1
And under "/etc/profile.d/", I see a file called, In it I see the following code:
# color-ls initialization

alias ll='ls -l' 2>/dev/null
alias l.='ls -d .*' 2>/dev/null

[ -e "/etc/DIR_COLORS.$TERM" ] && COLORS="/etc/DIR_COLORS.$TERM"
[ -e "$HOME/.dircolors" ] && COLORS="$HOME/.dircolors"
[ -e "$HOME/.dir_colors" ] && COLORS="$HOME/.dir_colors"
[ -e "$HOME/.dircolors.$TERM" ] && COLORS="$HOME/.dircolors.$TERM"
[ -e "$HOME/.dir_colors.$TERM" ] && COLORS="$HOME/.dir_colors.$TERM"
[ -e "$COLORS" ] || return

eval `dircolors --sh "$COLORS" 2>/dev/null`
[ -z "$LS_COLORS" ] && return

if ! egrep -qi "^COLOR.*none" $COLORS >/dev/null 2>/dev/null ; then
        alias ll='ls -l --color=tty' 2>/dev/null
        alias l.='ls -d .* --color=tty' 2>/dev/null
        alias ls='ls --color=tty' 2>/dev/null
So, the 'ls' color scheme file could be .dircolors, .dir_colors, or dircolors.xterm etc.

Wednesday, March 5, 2008

How to Disable and Enable (Reset) Thumbnails in Windows XP?

To Reset Thumbnail Generation for Picture Files

To disable thumbnails for picture files in Windows Explorer, simply issue the following command in the Command Prompt window. To open the Command Prompt window, type "Cmd" in Start->Run box.
    regsvr32 /u shimgvw.dll
After running the above command, all photos and images will lose their thumbnails.
To enable the preview thumbnail for pictures again, run the following command:
    regsvr32 shimgvw.dll

To Reset Thumbnail Generation for Video or Movie Files

To disable video media thumbnail preview in Windows Explorer, simply issue the following command in the Command Prompt window. To open the Command Prompt window, type "Cmd" in Start->Run box.
    regsvr32 /u shmedia.dll
Once the above command is run, the thumbnail preview will be disabled for all video, movie and other media files. That includes: AVI, WMA, WMV, ASF, and WAV files.

To enable video media thumbnail again, use this command:
    regsvr32 shmedia.dll

Monday, February 4, 2008

Manager's Must-Know: Wonderful Task Management in Outlook 2007

Microsoft Office is actually a suite of very powerful tools but not many people know how to use them. Take Outlook as an example, on top of emailing, you can use it to keep track of projects or tasks. You can create, prioritize, delegate, and track any task item like an email. Take a look at this video, you will know what I'm talking about.

The Office Team at Microsoft had spent so much time on this product and it is not hard to use at all but unfortunately not many companies are willing to spend a little time to train their employees to fully utilize it. Instead, many go for the hype of cloud computing with less features, slower performance, and higher security risk. And with their agile developing approach, it's not surprise to see a feature or a link that appeared on the screen yesterday disappeared today. The reason is simple, you're part of an experiment everyday whether you want it or not. So, if you see your favorite features disappearing every day, just stop being productive that day and send them an email. They will be glad to add that feature back in a few days, as their agile development cycle is short. (Your boss may not be very happy though.)

Saturday, January 12, 2008

Security Hole in Windows Help File

This is probably why Microsoft stopped supporting CHM help file back in 2002.  Thanks Nick Douglas for the demo here.  I believe this was Windows 2000.

Friday, January 4, 2008

How to find out what Shell I am in?

% ps -p $$
2222 pts/1 0:00 csh
To find out what OS you're using:
$ uname

Usage: uname [OPTION]...
Print certain system information.  With no OPTION, same as -s.

  -a, --all                print all information, in the following order,
                             except omit -p and -i if unknown:
  -s, --kernel-name        print the kernel name
  -n, --nodename           print the network node hostname
  -r, --kernel-release     print the kernel release
  -v, --kernel-version     print the kernel version
  -m, --machine            print the machine hardware name
  -p, --processor          print the processor type or "unknown"
  -i, --hardware-platform  print the hardware platform or "unknown"
  -o, --operating-system   print the operating system
      --help     display this help and exit
      --version  output version information and exit