Steven You

A Forged Geek.

New File Default Permission and Group

A new created file’s owner will be the usr of current user id and in the group of current groupid in current session.

The fist group in the output of groups command is the primary group of current user.

$ groups                                                                           
ec2-user wheel

The primary group of a user can be set by usermod -g wheel.

newgrp wheel command can create a new shell with the changed group.

The default group for all files created in a particular directory can be fixed by setting the setgid flag on the directory (chmod g+s <dir>). New files in the directory will then be created with the group of the directory (set using chgrp <group> <dir>). This applies to any program that creates files in the directory.

Note that this is automagically inherited for new subdirectories (as of Linux 3.10), however, if sub-directories were already present, this change won’t be applied to them (use the -R flag for that).

The new files default permission is controlled by umask.

setuid(4), setgid(2) and sticky bit(1)

setuid, setgid on files allow anyone execute the file with the privilege of the owner/group, but many OS ignore setuid attribute due to security issues.

setgid on directories causes new files and subdirectoriesi created within it inherit it’s group ID. setuid on directory is ignored on UNIX and Linux.

When setgid is set the permission shows: drwxrwsrwx, and if the folder don’t have execution permission for the group, permission will be drwxrwSrwx.

The most common use of the sticky bit today is on directories. When the sticky bit is set, only the item’s owner, the directory’s owner, or root can rename or delete files.

$ ls -ld /tmp  
drwxrwxrwt   4 root     sys          485 Nov 10 06:01 /tmp

If others don’t have execution permission, it will be drwxrwxrwT.

To display the octal permissions:

stat -c "%a %n" *

Maven Effective Pom

maven-plugin-plugin version bugged me for a day. The build plugin is not specified anywhere in our poms, so there is a default version set in maven itself somewhere.

Running mvn help:effective-pom to get the “effective” pom. Maven 3.0.4 gives 2.9 and 3.2 gives 3.2.

Effective POM is composed of Super POM + Application POM(s) + settings.xml contents + plugins bound to the lifecycle. The defult bindings is in maven-core-*.jar:META-INF/plexus/components.xml.

In the last, it is always a good practice to lock the version for everything. There is also a Maven Enforcer Plugin and its Require Plugin Versions rule to enforce this practice (which means the build will fail if you don’t lock down plugins versions).

Mechanical Keyboard

Recently obssed with mechanical keyboard. There are a few popular ones:

  • Pocker II
  • Race 2
  • FILCO minila air
  • Corsair VENGEANCE-K65

And it’s a dilemma to choose between Blue Switch and Red Switch.

Bash Remove Non-ascii Charamters From File Name

ls -1 | while read file; do N=$(echo $file | tr -cd '\11\12\40-\176'); mv "$file" "$N"; done

What this does is:

  • list filename in the current directory
  • -c means complement -d delete

Find Missing Number in a Random Ordered List

Imagine you have a list of all the numbers from 1 to n in a list, except one of the numbers is missing, (therefore the list has (n-1) numbers), and the numbers are in a random order in the list.How do you find out which number is missing?



sum(xrange(1, n+1)) - sum(list)

P.S. Even better:

n * (1 + n) / 2 - sum(list)

Bash Command Line Get File Absolute(canonical) Path

To print absolute path to a file:

$ readlink -m <file>

or use python script:

import sys
import os
for path in sys.argv[1:]:
    print os.path.realpath(path)

or use bash:

echo $(cd $(dirname $1); pwd)/$(basename $1)

Vim Tricks

This post will keep accumunating the tricks that I felt useful.

Disalbe and enable mouse

enable mouse:

:set mouse=a

disalbe mouse:

:set mouse-=a

Copy text out of vim with mouse=a enalbed

Press shift while selecting using mouse.

OS X (mac): hold alt/option while selecting.

Grep in all vim buffers and show in quickfix

:call setqflist([]) | bufdo grepadd! something %

Resize splitted windows

Use :resize 60, :res + 10 :vertical resize +5 to adjust quickly.

Or Ctrl-w +, Ctrl-w -, Ctrl-w >, Ctrl-w < and 10 Ctrl-w > to adjust time by time.

The most commonly used is Ctrl-w = to resize all windows to equal size.

Simplist vimrc

set mouse=a
syntax on
filetype on
filetype indent on
filetype plugin on

map  <space> :
map  <C-j> <C-w><down>
map  <C-k> <C-w><up>
map  <C-h> <C-w><left>
map  <C-l> <C-w><right>
imap <C-j> <down>
imap <C-k> <up>
imap <C-h> <left>
imap <C-l> <right>

let mapleader=";"
map <leader>w :w!<cr>
map <leader>q :close<cr>
map <leader>[ <C-o>
map <leader>] <C-]>
map <leader><space> <C-w>

map <leader>j :bn<cr>
map <leader>k :bp<cr>
map <leader>bd :bd<cr>

map <leader>tn :tabnew<cr>
map <leader>l  :tabn<cr>
map <leader>h  :tabp<cr>
map <C-t>      :tabnew<cr>
map <C-tab>    :tabn<cr>
map <C-S-tab>  :tabp<cr>

set ts=4
set expandtab
set smarttab
set shiftwidth=4
set autoindent
set smartindent

A list of powerful vim plugins

Rsync Files Across Server Through Ssh Tunnel

The scenario is: I can ssh to Server A and Server B, but A and B cannot connect to each other. In order tot rsync file across server A and B, ssh tunnel is needed: Firstly, set up reverse ssh tunnel to make it possible to ssh to local from Server A:

ssh -R 1999:localhost:22 A_user@<A>

Then on Server A, create ssh tunnel to server B via local computer:

ssh -f -N -L 2000:<B>:22 -p 1999 localuser@localhost

The command above creates ssh tunnel in background, so you have to bother killing it after finishing rsyncing. Finally, tell rsync to connect to localhost:2000 instead of :22:

rsync -rvP -e 'ssh -p 2000' dir/ B_user@localhost:/dir/

SSH ControlMaster

Lots of people recommond using ControlMaster:

Host *
ControlMaster auto
ControlPath /tmp/%r@%h:%p

This will re-use the exiting connections instead of creating a new one, so that it can speed up the ssh connection.

To make it more convenient, this line can be added to ~/.ssh/config

ControlPersist 600

This will make the ControlMaster stay alive for 600 seconds after last existing ssh session is quit.

A regular issue I encountered using ControlMaster is that in some cases I want to kill ssh ControlMaster active connection.

For example, when I run ssh tunnel command ssh -L 8888:localhost:8888 twice, the tunnel won’t work anymore.

In order to kill the active connection, use

ssh -O check

to check status, and

ssh -O exit

to exist the active connection.

If you want to delete all connections (not just the connection to a particular host) in one fell swoop, then fuser /tmp/ssh_mux_* or lsof /tmp/ssh_mux_* will list the ssh clients that are controlling each socket. Use fuser -HUP -k tmp/ssh_mux_* to kill them all cleanly (using SIGHUP as the signal is best as it lets the clients properly remove their socket).