Monday, December 17, 2007

My Favorite Cygwin Packages

Download Tools

aria2
High speed download utility.
lftp
ncftp
wget
wput

WWW Text Browsers

links
lynx
w3m

Wednesday, December 12, 2007

How to Add Items to the Context Menu, "Send To" in Windows

By using the Send To command, you can quickly send a file to different locations including a floppy disk, your desktop, another person using e-mail, or the My Documents folder. The SendTo folder contains the shortcuts for the destinations that are displayed on the Send To menu. Every user on the computer has a SendTo folder and can customize its contents.

Friday, November 30, 2007

How to Make a User a Sudoer

  1. Add user to wheel group:
    $ vim /etc/group
    - wheel:x:10:root,newuser
    
    or simple do this:
    $ gpasswd -a newuser wheel
    
  2. Uncomment this line in sudoers file:
    $ vim /etc/sudoers
    - ## Allows people in group wheel to run all commands
    - %wheel    ALL=(ALL)    ALL
    
    or
    $ visudo
    

Thursday, November 1, 2007

How to Make Sure MySQL Server Runs at Startup

To make sure mysqld runs at system startup time, type the following commands:
sudo /sbin/chkconfig --add mysqld
sudo /sbin/chkconfig --level 235 mysqld on
To make sure mysqld is configured properly, type the following:
sudo /sbin/chkconfig --list mysqld
The output should look like this:
mysqld           0:off 1:off 2:on 3:on 4:off 5:on 6:off

Monday, October 22, 2007

Redirect stderr to both screen and file at the same time

I recently ran into a situation where I need to run a command and have the error output recorded in a file while displaying it on the screen at the same time. It turns out the 'tee' command comes in handy in this case.
$ command 2>&1 | tee err.out

To redirect stdout and stderr to different files, and have the stderr goes to the screen as well:
$ command 2>&1 > std.out | tee err.out

To redirect stdout and stderr to the same file while having the stderr displayed on the screen, too:
$ command 2>&1 > all.out | tee -a all.out


The last trick is particularly handy when it comes to writing a cron job. The crontab will only send out email when there is stderr outputs while writing the output to the same file as the stdout.

Tuesday, September 11, 2007

How to Make Sure MySQL is Running?

To ping MySQL,
$ mysqladmin -u root ping
mysqld is alive

To check MySQL Server's version,
$ mysqladmin -u root version
mysqladmin  Ver 8.41 Distrib 5.0.77, for redhat-linux-gnu on x86_64
Copyright (C) 2000-2006 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version          5.0.77
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 22 min 25 sec

Threads: 1  Questions: 6  Slow queries: 0  Opens: 12  Flush tables: 1  Open tables: 6  Queries per second avg: 0.004

Wednesday, August 1, 2007

Standard Input and Output in Linux

Every process in Linux has 3 connections to the outside world.  They are:

  • Standard Input (stdin): default to the keyboard
  • Standard Output (stdout): default to the monitor screen
  • Standard Error (stderr): default to the monitor's screen
In Unix/Linux each of these connections is also associated with a file descriptor:
  • 0: stdin
  • 1: stdout
  • 2: stderr
To redirect a process's output to a file (only normal messages, no errors):
Standard input can also be represented by '-'.
This is equivalent to copy con outputfile in DOS.
$ command > outputfile 
$ command - > outputfile

To direct a file as standard input: 
command < inputfile

To direct standard input to a file:
cat  > outputfile

To direct a process's output to another process (command) as standard input, use pipe:
command_out | command_in
e.g. cat filename | more

To direct error to a file:
   $ command 2>errors.txt  # 2 indicates the error stream in Linux
   $ command 2>/dev/null   # /dev/null is a device where anything you send simply disappears.

To direct both standard output and error to a file:
   $ command > error.log 2>&1  # '&1' represent stdout.
To direct stdout to output.log and stderr to error.log, and print only error to the screen:
   $ command 2>&1 > output.log | tee error.log

   # append error to the same output.log file instead of error.log.
   $ command 2>&1 > output.log | tee -a output.log

Monday, July 2, 2007

Changing Passwords in MySQL

To set the root's password the first time:
$ mysqladmin -u root password NEWPASSWORD
To change the root's password:
$ mysqladmin -u root -p'OLDPASSWORD' password NEWPASSWORD
Or you can use the MySQL Client:
mysql> USE mysql;
mysql> UPDATE user SET PASSWORD=PASSWORD("NEWPASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
To reset root's password, run mysqld_safe --skip-grant-tables:
$ /etc/init.d/mysqld stop
$ mysqld_safe --skip-grant-tables &
$ mysql -u root
mysql> UPDATE mysql.user SET PASSWORD=PASSWORD("NEWPASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit;

# Restart mysqld
$ /etc/init.d/mysqld stop
$ /etc/init.d/mysqld start

Sunday, June 3, 2007

How to make the 'less' command not clear screen after exit?

You can use the use option:
-X or --no-init

e.g. alias less='less -X'
If you want to use it long term, the PAGER is a useful variable to set in your .bashrc files:
export PAGER='less -X'
To make less exit if the content fits on one screen, use:
-F or --quit-if-one-screen

export ACK_PAGER='less -RFX'

Thursday, May 3, 2007

How to Stop ssh from Clearing Console on Exit?

It was not obvious but there is an explicit clear command in the logout script that clears the content of the terminal. This is what my .bash_logout file looks like:
# ~/.bash_logout

/usr/bin/clear
Commenting the line, /usr/bin/clear, will do the trick.

Monday, April 2, 2007

How to Stop Windows XP Rebooting Automatically after an Update

If you are using Windows XP, you may notice sometimes it restarts itself after an overnight update. This could be quite annoying if you were like me, always left some applications open overnight. Fortunately, this annoyance can be deactivated. Just follow these steps below:
  1. Go to the "Run Command" dialog and enter "gpedit.msc".
  2. Expand to "Local Group Policy\Computer Configuration\Administrative Template".
  3. Double click on "Windows Components".
  4. Double click on "Windows Update" on the right side.
  5. Right click on "No auto-restart for scheduled Automatic Updates Installations", and select "Properties".
  6. Select "Enable", and click "OK" to close the dialog box
  7. Click "Yes" to save this settings.
  8. Restart your computer. You should be good to go.

Tuesday, March 13, 2007

How to use the FOR command in DOS?

Oftentimes you may want to perform a repetitive operation to a list of items in the command prompt, the list could be a list of files, folders, or just any random list from a file.  This FOR command in DOS is all up for the task.

First, here is the general format of the command: FOR ... IN ... DO ....

FOR %variable IN (set) DO command [command-parameters]


%variable
a single letter replaceable parameter.
(set)
a set/list of one or more files.
command
the command to carry out for each file.
[command-parameters]
parameters or switches for the specified command.

To Iterate Local Folders Only

Use the /D option. e.g.
FOR /D %variable IN (set) DO command [command-parameters]
Example: To rename all the folders to "folder name".bak. FOR /D %i IN (*) DO ren "%i" "%i.bak"

To Iterate All Folders Recursively

Use the /R [[drive:]path] option.

Other Options

To see more options on the FOR command, type FOR /h in the command prompt, or see the Microsoft's page.

Monday, February 26, 2007

Common Unix/Linux Commands

To list the content of a file:
    $ cat filename |more
To extract .tar.gz file:
    $ tar -xzvf filename.tar.gz
To modify user specific shell setting, edit the User Dependent File: .bashrc

To create an alias:
    $ alias ll='ls -l'
    $ alias la='ls -A'
    $ alias grep='grep --color'

    # My alias in Cygwin
    $ alias emacs='/cygdrive/c/bin/emacs-23.1/bin/runemacs.exe'
    $ alias firefox='cygstart /cygdrive/c/Program\ Files\ \(x86\)/Mozilla\ Firefox/firefox.exe'
To create file/folder links:
    $ ln -s target symbolic_name    # create a symbolic link
    $ ln target hard_link_name      # create a hard link
To temporary go to the directory of an application:
    $ pushd `which gcc`             # the command inside backticks always run first.
    $ popd                          # go back the current directory.
To extract just the path from a filename use dirname:
   $ dirname /usr/bin/sort          # output "/usr/bin"
   $ dirname stdio.h                # output "."
   Script: cd "`dirname \"$0\"`"    # go to the folder that contains this script file

How to Find a file in Linux/Unix

To find all file with a '~' end recursively:
   $ find -name "*~"
To find a file 'abc*', case insensitively, starting from root:
   $ find / -iname 'abc*'
More advanced search:
   # find files with size less than 5000k
   $ find /some/folder -name '*.mov' -size -5000k

   # find files with size more than 10MB
   $ find / -size +10000k

   # find files accessed in the last 10 minutes
   $ find / -amin -10 -name '*.log'
   $ find / -mmin -10 -name '*.log'   # modified in the last 10 mins.
   $ find / -atime -5 -name '*.log'   # accessed in the last 5 hours.
   $ find / -mtime -5 -name '*.log'   # modified in the last 5 hours.

Other Tools

Cygwin Consoles:
    $ MinTTY    # a lightweight console with lots of useful features
    $ Console2/Console.exe
File Management Utilities:
    $ mc    # Midnight Commander (similar to Norton Commander.)
Games:
    $ typespeed    # a typing game

  To modify the word list used in typespeed, go to "/usr/share/typespeed/words/".

Thursday, February 1, 2007

Batch Commands: Command Line Parameters

Basic Batch File Command Line Parameters

  • %0 is the program name as it is called
  • %1...%9 are the command line parameters
  • Use SHIFT to shift parameters over, i.e. %1 will get the value in %2.
  • Use SHIFT /n to keep the first n parameters in place while shifting the rest over.  %0 is included, i.e. SHIFT /3 keeps %0...%2 the unchanged.
To iterate each parameter:

    FOR %%a IN (%*) DO (
        REM %%a will have the value of each parameter here.
    )


If the value of the parameter contains quotes, use IF '%1'=='' or IF "%~1"=="".

These delimiters: comma, semicolon, equal, tab, multiple spaces, and first forward slash are replaced by a single space.