23 November 2014

Concepts

Sector vs Block

ext4 allocates storage space in units of “blocks”. A block is a group of sectors between 1KiB and 64KiB, and the number of sectors must be an integral power of 2.

https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout

AIO

File Access Permissions

apue.2e’s section 4.5 explains file access permissions really well. And it has a clear explanation of read and execution permissions on directories.

Default Permission

File default Permissions are 666. Directory default permissions are 777.

Utilities

rsync

Compare directory tree:

rsync --delete -a -n -c -v -e ssh root@linode:~/test/linux-2.6.11/ linux-2.6.11

yum

  • yum search does not suport *. yum list supports *.

AWK

File data contains:

| a | b |

awk -F '|' '{print $2, $3}' data produces:

 a   b

Search for patterns in Java and SQL files:

ack -l --type-set  jing:ext:.java,.sql line_spec src

LVM

Extend a partition:

lvextend -L+10G /dev/mapper/vg_root-lvvar
resize2fs /dev/vg_root/lvvar

Ack-grep

ack-grep -g '.*\.md' | ack -x system

Use a type temporarily:

> mkdir test
> echo google > 1.xx
> echo google > 1.xx
> echo google > 1.yy
> ack -k google
> ack --type-set zz:ext:xx,yy google
1.xx
1:google

1.yy
1:google
> cat ~/.ackrc

Add a type permanently:

> cat ~/.ackrc
--type-set=md:ext:md,markdown

Grep

With -P, grep support \d pattern.

Ncat

Chain Ncats Together’s second code example does not work. It should be:

host3$ ncat -l
host2$ ncat -l --sh-exec "ncat host3"
host1$ ncat host2

Now host3 and host1 can talk to each other.

Tcpdump

Lsof

Examples:

lsof -r 1 -u jing -i -c chrome -a

Ping

64 bytes from 192.168.1.20: icmp_seq=264 ttl=62 time=1.12 ms

64 is the length of data wrappted in IP packet. The IP packet length is 84.

Sendip

sudo sendip -p ipv4 -is 10.0.0.8 -p udp -us 5070 -ud 5060 -d "Hello" -v 10.0.0.8
sudo tcpdump -i lo -nn -vv -X -S port 5070

Find

Prune action

find -regextype posix-egrep -regex '.*\.((h)|(c)|(cc)|(hpp)|(cpp))'
find \( -name '*.cc' -or -name '*.h' \) -exec readlink -f {} \;
find -regex ".*\.\(h\|cc\)" -exec readlink -f {} \;

Jekyll

http://jekyllbootstrap.com/usage/jekyll-quick-start.html

brew install ruby
gem install jekyll bundler

bundle exec jekyll serve -watch serving the local web site.

Dpkg

dpkg --list package status: http://ubuntu.aspcode.net/view/63540014012470517533550/what-do-the-various-dpkg-flags-like-ii-rc-mean

Nohup

nohup shell_command > log 2>&1 < /dev/null &

SSH

The following command sets up a SOCKS5 proxy server listening on 7979. I run this command on my laptop. ssh-server is a server which can access web sites which are inaccessible to my laptop.

ssh -ND 7979 ssh-user@ssh-server
+---------------+ ssh     +---------------+
| proxy         |-------->|   ssh-server  |
+---------------+         +---------------+

Programming

Function related to error: perror, strerror. errno is include in errono.h.

Web Server

Ngnix

Imagine that there are index.html and css.html in root. http://localhost/index.html can be used to access index.html. There are three a elements in index.html. Click any of them, the browser will go to css.html. ../css.html means to find css.html in a parent folder. Since index.html is at the root, .. is ignored here.

<a href="css.html"></a>
<a href="../css.html"></a>
<a href="/css.html"></a>

CentOS

CentOS’s support term is 10 years.

Iptables

Change Hostname CentOS

Centos 6.5

  • Update /etc/hosts and /etc/sysconfig/network.
  • Run /etc/init.d/network restart.

The update to hostname by hostname command does not survive system root. But the Update to /etc/sysconfig/network survives system root.

Centos 7.x

For Centos 7.x, hostname comamdn is enough.

chkconfig

  1. Redhat 6 Runlevel
  2. Redhat 7 Using the chkconfig Utility
  3. LSB init scripts
  4. Runlevel Priority
  5. Starting Your Software Automatically on Boot
  6. 15.5. chkconfig

Priority rules:

  1. service with a lower priority starts ealier
  2. service with a higher priority stops ealier

4 and 5 clearly says Rule 1. Rule 2 follows from the implication that service with a higher priority depends on service with a lower priority. This implication is from Rule 1.

A chkconfig script example:

#!/bin/bash
# description: One Start Stop Restart
# processname: one  
# chkconfig: 234 20 80  

case $1 in
  start)
		echo "one start"
    ;;
  stop)   
		echo "one stop"
    ;;
  restart)
		echo "one restart"
    ;;
esac    
exit 0

Make

Shell assignment operator is available with GNU Make version 4.0. Reference is GNU Make 4.0 released.

Bash

Regular Expression Match Test

Metacharacters in regular expression should note be quoted.

> [[ "abc" =~ "a"".""c" ]] && echo "match"
> [[ "abc" =~ "a"."c" ]] && echo "match"
match

> [[ "abc" =~ 'a''.''c' ]] && echo "match"
> [[ "abc" =~ 'a'.'c' ]] && echo "match"

match
> [[ "abc" =~ '.*' ]] && echo "match"
> [[ "abc" =~ .* ]] && echo "match"
match

Excerpt from Bash reference manual:

Any part of the pattern may be quoted to force it to be matched as a string.

History Expansion

Enable it with set -H. history returns:

...
986  exit
987  ls
988  ./foo.sh 1
989  ./foo.sh 1 " "
990  ./foo.sh " " 1
991  vi foo.sh
992  ./foo.sh 1 " "
...

History expansion:

$ echo "!987"
echo "ls"
ls

Tips

Useful tricks:

set -e -x
shopt -s extglob

Check file format (unix or dos):

find -type f -print0 | xargs -0 grep -l `printf '\r\n'`

order of redirections:

Check options set by set in $-. Check options set by shopt by shopt -p.

Resources