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.)
    $ 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.