Jan 23, 2014

Sort IP Address

There is time you need to sort IP address in the way human understand.
# sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 addresses.txt

Where,
  • -t . : Set field to . (dot) as our IPs separated by dot symbol
  • -n : Makes the program sort according to numerical value
  • -k opts: Sort data / fields using the given column number. For example, the option -k 2 made the program sort using the second column of data. The option -k 3,3n -k 4,4n sorts each column. First it will sort 3rd column and then 4th column.

Jan 17, 2014

Report A Snapshot of Current Processes

ps [options]

ps displays information about a selection of the active processes. It accepts several kinds of options:

  1. UNIX options, which may be grouped and must be preceded by a dash.
  2. BSD options, which may be grouped and must not be used with a dash.
  3. GNU long options, which are preceded by two dashes.

 To see every process on the system using standard syntax:
ps -eps -efps -eFps -ely

 To see every process on the system using BSD syntax:
ps axps axu

 To print a process tree:
ps -ejHps axjf

 To get info about threads:
ps -eLfps axms

 To get security info:
ps -eo euser,ruser,suser,fuser,f,comm,labelps axZps -eM

 To see every process running as root (real & effective ID) in user format:
ps -U root -u root u

To see every process with a user-defined format:
ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,commps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,commps -eopid,tt,user,fname,tmout,f,wchan

Print only the process IDs of syslogd:
ps -C syslogd -o pid=

Print only the name of PID 42:
ps -p 42 -o comm=

Jan 16, 2014

Shows Exit Code of Previous Command in zsh

To display the exit code of the previous command is very useful in zsh.
PS1='%n%m %~ %(?..[%?] )%# '

%(?..[%?] )
It's a conditional expression. The part before the 1st dot is the expression; the part between the two dots is output if it's TRUE (or 0); the part after the second dot is output if it's FALSE (or any number but 0).

See my full customization on .zshrc file.

Jan 15, 2014

Tips on GNU Screen

GNU Screen is a simple but powerful terminal multiplexer.

Two basic commands that I use often:
  • screen -r : Re-attach the only-one screen session.
  • screen -ls : To show running sessions 

Here's the features that I use often:
  • ctrl-a + d : To detach the terminal.
  • ctrl-a + c : To create a new terminal
  • ctrl-a + p : To switch to the previous terminal
  • ctrl-a + n : To switch to the next terminal
Here's the features that I use less often:
  • ctrl-a + x : To lock the current terminal
  • ctrl-a + S : To split the terminal to two separate areas/tabs.
  • ctrl-a + X : To close the current area/tab.
  • ctrl-a + [tab] : To switch to the next tab.
  • ctrl-a + H : To create a running log of the session.
Advanced usages:
  • ctrl-a + M : To start/stop monitor for activity
  • ctrl-a + _ : To start/stop monitor for silence 
  • ctrl-a + " : To quickly go to that window
References:
  • http://polishlinux.org/howtos/screen-tips-tricks/
  • http://www.softpanorama.org/Utilities/screen.shtml
  • https://wiki.archlinux.org/index.php/GNU_Screen
  • http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
Tips:
In you accidentally press "ctrl-a s" (instead of "ctrl-a S") while creating tab, which freeze the parent terminal, just press "ctrl-a q" to unfreeze it. 

Jan 9, 2014

Some Tricks in using zsh

These are part of the tricks that I like most about zsh:

ESC-. [ Escape period ]
Inserts the last argument of the previous history line, repeat to go back in history.

ESC-'  [ Escape single-quote ]
To quotes the whole line. (Useful for su -c or ssh).

ESC-q [ Escape q ]
To clears the line and inserts it again on the next prompt, allowing you to issue an interim command.

ESC-RETURN [ Escape ENTER ]
To inserts a literal newline, so you can edit longer commands easily.

References:

  • http://chneukirchen.org/blog/archive/2008/02/10-zsh-tricks-you-may-not-know.html
  • http://www.rayninfo.co.uk/tips/zshtips.html
  • http://grml.org/zsh/zsh-lovers.html