botbotbot 's blog

Unix

Helper

$ ~ $ home user directory
$ ~+ # same as pwd
$ ~- # previous directory
$ ls -1 $PWD/*.txt # list all .txt with full path
$ cd - # cd to previous path
$ export EDITOR=vim # set vim as default editor
$ groups #list group
$ getent group <group_name> #list members of group
$ top #check performance
$ du -sh <folder> # summary disk usage of folder
$ df -h # disk free
$ chsh - <user> # change default ssh user
$ printenv # list environment variables
$ \<cmd> # run command by  avoid alias

Command

nl #number line
xargs <cmd> #change input to arguments, use with pipe
sed #stream editor
pbcopy < <file> # OSX: copy file into clipboard

Where command

$ hash # list command and its path
$ type -a <cmd> # tell all path of commamd
$ which <cmd> # tell default path of command

Redirection

$ cmd > <file> # print output to file (overwrite)
$ cmd >> <file> # print output to file (append)
$ cmd 2> <file> # print error to file
$ cmd &> <file> # print output & error to file
$ cmd < <file> # use file as  input
$ cmd << <file> # use file as input
$ cmd <<< <any>  #use anyas input (here string not compute any tag)
# Redirection output wiht sudo
$ echo "hello" | sudo tee out > /dev/null # eq. echo "hello" > out
# echo "hello" | sudo tee -a out > /dev/null # eq echo "hello" > out

Jobs - fg bg Tutorial

$ jobs # list suspended jobs
$ fg # put the top suspended job in the foreground
$ fg %1 # put the suspended job id 1 in the foreground
Ctrl-z # Suspend a foreground job
$ bg # run suspended job in background
$ kill # kill job

Crontab Time schedule

$ crontab -e #edit crontab profile
# */5 * * * * cmd
# run cmd every five minutes
$ grep CRON /var/log/syslog #see crontablog (debian)

Zsh History expansion

should also work with bash.

$ man zshexpn # manual for look in-depth
$ history # tell history command that keep in ~/.bash_history
$ history -c # clear history
! # previous that can combine with command and argument
$ !! # call previous command
$ !<str> # call last command that start with str
$ !?<str>[?] # call last command containing str
$ !<hist_number> # call command in nth of history files
 ^ # first argument
 $ # last argument
 :n # nth argument
 :m-n # mth to nth argument
 * # all argument
$ <space><cmd> # call command without keep history

Zsh Basic Editing - Bash Shortcuts

should also work with bash.

$ bindkey -v # vi mode
$ bindkey -e # emac mode
Ctrl + a # moves the cursor to begin of line
Ctrl + e # moves the cursor to end of line
Ctrl + u # delete current to begin of line
Ctrl + k # delete until end of line
Ctrl + l # clear
Ctrl + w # delete word backwords
Ctrl + d # delete char (moves forward)
Ctrl + f # move char foreward
Ctrl + b # move char backward
Meta + f # move word foreward
Meata + b # move word backward
Ctrl + y # yank last delete word
Ctrl + t # transpoes two characters
Esc + t # transpoes two words

Meta is your Alt key, normally.
For Mac OSX user need to enable it,  Open Terminal > Preferences > Settings > Keyboard, and enable Use option as meta key.

Diff Command

$ diff --brief <file1> <file2>

HowTo: Find Out My Linux Distribution Name and Version

$ cat /etc/*-release

Timezone setting in Linux

$ export TZ=Asia/Bangkok date

How to cycle through reverse-i-search in BASH?

$ Ctrl+R grep Ctrl+R Ctrl+R

safeguard-rm

# only zsh

$ setopt noclobber # prevents overwriting an existing file.
$ setopt clobber # disable

$ unsetopt RM_STAR_SILENT # ask before rm with star (*)
$ setopt RM_STAR_WAIT # wait 10 sec before exec rm with star

Mac style copy/paste for linux machines

# copy/paste for linux machines (Mac style)
# sudo apt-get install xclip
$ alias pbcopy='xclip -selection clipboard'
$ alias pbpaste='xclip -selection clipboard -o'
$ alias pbselect='xclip -selection primary -o'

Bash

Shell/Bash Script

#!/bin/bash

VAR = "hello"
if [ "$VAR" = "hello" ]; then  # use = to equal string not ==
  echo $VAR
else
  echo "fasle"
fi

Run Script - Different ways to execute a shell script

$ ./script      #run with using shebang(#) in script ex. #!/usr/bin/python
$ . script      #run with shell (work in related bash)
$ source script #run with shell (work in both bash and csh)

Shell function - Passing argument to alias in bash

$ echo 'rm() { mkdir -p /tmp/rm; /bin/mv "$@" /tmp/rm; }' >> .zshrc

Execute combine multiple linux commands in one line

cmd1 && cmd2  # cmd 2 run only if cmd1 successful
cmd1 ; cmd2  # cmd 2 run whether failed or not

Parallel shell script

  1. Parallel batch processing in the shell
  2. Bash script processing commands in parallel
process1 &
process2 &
process3 &
process4 &
wait  # wait until process1, 2, 3, 4 done
process5 &
process6 &
process7 &
process8 &
wait  # wait until process5, 6, 7, 8 done
  1. Parallelize Bash Script
#!/bin/bash

nproc=$(($(nproc)-1))  # number of core cpu - 1

# waitproc - wait until have core slots
waitproc () {
  jobcnt=($(jobs -p))
  until [ ${#jobcnt[@]} -lt $nproc ]; do
    sleep 0.05
    jobcnt=($(job -p))
  done

Bash For Loop Array: Iterate Through Array Values

if [ $? -eq 0 ]
do
    echo "hello"
done
#!/bin/bash

array=( one two three )

for i in "${array[@]}"
do
    echo $i
done

Get file directory path from filepath

$ dirname "/home/user/dir/file.py"
/home/user/dir
$ basename "/home/user/dir/file.py"
file.py

How to find out from the logs what caused system shutdown?

last reboot | less
last -x|grep shutdown | head -1

Zsh

Bash References:

  1. Functions
  2. Bash shell expansion
  3. bash-tricks
  4. check command exitsts
  5. High-speed Bash

ETC

$ less +F log/production.log

Reference

  1. learning the shell
  2. HowTo: Add Jobs To cron Under Linux or UNIX?
  3. how to set cron job to run every 5 mins?
  4. CronHowto
  5. Checking UNIX Server Performance
  6. How do I use sudo to redirect output to a location I don’t have permission to write to?
  7. UNIX Disk Usage Command Examples
  8. Unix as IDE: Introduction
  9. หัดใช้ Command-line Interface เถอะ
  10. Pimp my command line
  11. The Art of Command Line
  12. Command Line Power User
  13. Here Strings
  14. what is the find command, and how do I use it to search
  15. Totally Tooling Mini Tip: Command-line Keyboard Shortcuts
  16. 10 examples of paste command usage in Linux
  17. Bang! Previous Command Hotkeys - Linux CLI BASH ZSH

Recover lost partition

Recover lost partition

I used TestDisk to recover my lost NTFS partition that I formatted by accidentally. You can use TestDisk by follow TestDisk Step by Step. After recovered I can see my NTFS partition that was formatted but can not access it.

Repair damaged partition

I got my lost partition back but can’t access it. So I use ‘chkdsk /r’ command on Windows to repair damaged partition. Finally after 12 hours, I can got lost partition back and accessible it normally.

  chkdsk /r <drive>

Enable NTFS write on OSX

Automatical Way

http://www.macthai.com/2012/06/06/how-to-mac-read-write-ntfs-fat32-file-osx/

Manual Way

  1. get UUID form this command

    diskutil info /Volumes/<drivename> |  grep UUID
    
  2. edit /etc/fstab

    sudo vim /etc/fstab
    
  3. add this with edit <uuid_drive>

    UUID=<uuid_drive> none ntfs rw,auto,nobrowse
    
  4. reject and plug it agian
  5. open hdd with this command

    open /Volumes
    
  6. If want to remove writable NTFS drive

    sudo rm /etc/fstab
    

References:

  1. http://coolestguidesontheplanet.com/how-to-write-to-a-ntfs-drive-from-os-x-mavericks/
  2. http://learnaholic.me/2013/11/11/enable-ntfs-write-on-mac-os-x-mavericks/
  3. วิธีทำให้ Mac อ่านและเขียนไฟล์ NTFS บน Windows ได้

How to set up this blog

This blog created by Jekyll that is ruby scripts to transforms plain text into static html website and blogs and hosted on Github Page that free hosting support Jekyll.This blog use Lanyon theme that based on Poole a minimal style of Jekyll.

Set up github page:

  1. Follow setup guilde of Github page for more information about jekyll on github look at
```
http://help.github.com/articles/using-jekyll-with-pages
```   2. Clone project from [Lanyon](http://lanyon.getpoole.com)  

```
  git clone https://github.com/poole/lanyon.git
```  
  1. Configs jekyll in _config.yml
```yaml
  #setup
  title:            botbotbot  
  tagline:          "'s blog"  
  description:      "botbotbot's blog"  
  url:              http://ibotdotout.github.io  
  paginate:         5  
```
  1. Make tags.md in root directory copy code from
```
https://github.com/LanyonM/lanyonm.github.io/blob/master/tags.html
```
  1. Let blog it.

Add Feed in Jekyll

  1. Jekyll Feed plugin - Recommend
  2. Atom Feed in Jekyll
  3. RSS Feed in Jekyll
  4. RSS for Jekyll blogs

References:

  1. http://joshualande.com/jekyll-github-pages-poole/
  2. Table not render when use Redcarpet in Jekyll GitHub Pages?
  3. Jekyll-Plugins
  4. Jekyll Stater
  5. Adding Disqus Comment to your Jekyll
  6. How can I get backtick fenced code blocks working inside lists (with kramdown)?

Vim

Tutorial

$ vimtutor

Enter to Vim

  vim . # open with file browse
  vim -c "h quickref | only"
  vim -O < file1 > < file2 > ... # open in verticle
  vim -o < file1 > < file2 > ... # open in honzital

Normal Mode

Save, Exit

Keys Description
ZZ save and quit
ZQ save without quit

Movements

Keys Description
w[ord] {letters, numbers, underscore}
W[ORD] {non-blank characters}
g{motion} work with display line ex. gj down one line, g$ go to end of display line
f{char} move to next char
t{char} move to before next char
; repeat last search
, repeat last search backward

Jump

Keys Description
% jump between open/close parentheses
<C-o>, <C-i> toggle jump position
{num}|(pipe) Go to Column
<number>G go to line number

Visual

Keys Description
v{motion} visual mode select in line
V{motion} visual mode select line
<C-v>{motion}{cIA} change/insert/append in multiple lines
<C-v>{motion}: command with multiple lines

Indent

Keys Description
== Auto Indent
= Auto Indent in selected on visual mode
J join the current line and next line together

Edit

Keys Description
cw change word
gu{motion}, gU{motion} lower/upper letter

Delete

Keys Description
daw delete a word
das delete a sentence
dap delete a paragraph
dW delete until space
dG delete until end of file
d$ delete until last of line
dbw , <C-w> in insert mode delete back one word
d^ , <C-u> in insert mode delete back to start of line

Undo

Keys Description
u redo
<C-r> Redo
Keys Description
\<word>\c search word with ignore case

Repeat

Keys Description
. repeat last action (not include any move action)

ETC

Keys Description
* highlight word
<C-a>,<C-x> addition and subtraction on numbers that also find next number automatically if current cursor not a number
<C-I> Tab
<C-[> Esc
<C-M> Enter
<C-H> Backspace

In Insert Mode

Keys Description
<C-r>=55*10<Cr> in insert mode calculate can print it

Windows Managements

Windows Split

Keys Description
<C-ws> Split windows
<C-wv> Split windows vertically
<C-wq> Quit windows
<C-ww> switch between windows

You split navigations with

nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

Tab

Command Description
:tabe[dit] {filename} open file in new tab use gt, gT to move to next, previous tab
:tabc[lose] close all tabs
:tabo[nly] keep current tab and closing all others

Vim spell

Command Description
:set spell enable spell checker
[s, ]s jump to miss spelling work
z= fix miss spelling word
zg add current word into spell dictionary
zw remove current word from spell dictionary

Repositioning the screen

Command Description
zt Move current line to top screen
zz Move current line to mid screen
zb Move current line to bottom screen

Scroll Screen

Keys Description
<C-f> scroll down one screen
<C-b> scroll up one screen
<C-d> scroll down half screen
<C-u> scroll up half screen

Text object <need to combine with cmd>

Keys Description
iw/aw inside/around word
is/as inside/around sentences
ip/ap inside/around paragraph
it/at inside/around tag ex. html tag
i” inside double quote
i) inside parents

Register

# update vim version
$ brew install vim
$ brew install reattach-to-user-namespace
Command Description
:reg[ister] check buffer register
”<register>p paste buffer
”<register>yy yank to buffer
”*yy yank to system clipboard

Vim Command

Vim Plugins

Vim Script

  1. autocmd group

Vim Performances

Open vim without plugin no settings from your .vimrc

$ vim -u NONE -N

Open vim without plugin

$ vim --noplugin

Reference:

  1. A vim Tutorial and Primer
  2. วิธีใช้ Vi และ Vim
  3. How to Use Vim’s Spell checker
  4. Vi/Vim Cheat Sheet
  5. Vim Cheat Sheet Graphical
  6. Vim & Tmux & System Clipboard
  7. vim.rc & vim plugin (video th)
  8. John Crepezzi’s Vim (video)
  9. Vi and Vim Macro Tutorial: How To Record and Play
  10. VIM Grammar-Jaa-LGyagRA
  11. Vim tips: Folding fun
  12. Learn Vimscript the Hard Way
  13. Vim as an IDE
  14. How to jump directly to a column number in Vim 17.Coming Home to Vim
  15. Vim-LanguageTool
  16. Vim Tips (KKLUG)
  17. Vim multiline editing like in sublimetext?
  18. A Good Vimrc
  19. THE VIM PRESENTATION
  20. Vim Cast
  21. vim-galore - Everything you need to know about Vim.
  22. Converting tabs to spaces