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
/usr/share/zoneinfo/America/Los_Angeles

'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}'
col3:col2:col1

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 "www.mydomain.com"
    
  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 ^(.*)$ https://www.mydomain.com/$1 [R,L]
    
  3. Append the following line to httpd.conf:
    Redirect permanent / https://www.mydomain.com/
    
  4. Or try this:
    <NameVirtualHost 122.123.124.1:80>
        ServerName mywebsite.com:80
        ServerAlias http://www.mywebsite.com:80
        ServerAlias 122.123.124.1:80
        Redirect permanent / https://www.mywebsite.com/
    </VirtualHost>
    
  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/mod_rewrite.so
    
  6. Or try this:
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R,L]
    
Note: Restart Apache for new configuration to take effect.
$ /etc/init.d/httpd restart