Unix

January 01, 2012

Inbox

:- = parameter expansion, if the third argument is null or unset, replace it with what’s after :-

  • ctrl+w erase last word to left
  • ctrl+a
  • ctrl+e
  • ctrl+d end of file

Un-sudo

sudo -u $(who am i | awk '{print $1}') bower install

Vim

d$ Delete through end of line
c2/end»Return«

:h sometext
:bd
»command«!   |   without confirmation

dis
syntax enable
set syntax=apache

set number
12G go to 12th line

:w !sudo tee %
# ! shell command
# % current filename

j - down
k - up
l - right
h - left

ESC - leave mode
i - insert mode

:wq - save & quit
:q! - cancel & quit

Repices

Download sth
curl https://github.com/juzna.json > juzna.json

rename 's/(.*)/\.$1/' *   # prefix all files with .

git diff ___ ___ | mate

symbolic link
ln -s »original« »new«

Mac tips

Drag items from Finder to Terminal

Basic commands

cd

clear

open

open directory in finder, file in default, url in browser

open -a »app name«

man

touch

nano

tr

replace character tr a e

cp

ls

 ls -l » detailed list
 ls -a » all files (hidden)
 ls -t »

pwd

mkdir

mv

mv »original name« »new name« mv * »directory«/ # move all to directory

find

find . -type f # files in current dir, recursively
find . -name "joe"
find . -iname "joe" # case-insensitive
find . -name "*.txt"
find . -size +2048 # > 2MB
find . -mtime +1 # modified
find . -atime +1 # accessed
find . -ctime +1 # created
find . -name "*.md" -or -name "*.txt" -and -mtime +1
find . »exp1« -print -or »exp2« -prune   # exclude second exp2
find . -name "*eslint*" -type f -maxdepth 2

Grep

grep "needle" »file«
grep -l "needle" »file«   # write out filename with occurences
grep -l "needle" *        # in all files
grep -lr "needle" *       # recursively 
grep -i "needle" »file«

Permissions

chmod u
      g
      o
       +
       -
        r 4
        w 2
        x 1

Common 644

Piping

redirect stout 2> redirect errout

ls Documents >> log 2>&1
tr a e < »file«
cat file | grep "something"
ls -l | open -f # open in TextEdit

Processes

> top
> ps
 -c # shortname
 -x # user
 -v # extra information

> ps -cvx | grep google
signals # stop and respond to signal
 > kill »PID« # can be ignored
 > kill -s KILL »PID« # cannot be ignored

SSH

sss-keygen -t rsa # can be used without passphase
scp id_rsa.pub user@1.2.3.4:~/.ssh/authorized_keys

config in files

Remote

sftp »ssh-alias«
 > pwd
 > cd
 > lcd, lls, lpwd # do locally
 > put file1 # upload from local to remote
curl »url« > file
curl -O »url« # write to same filename on local

Functions

function hello() {
	echo "hello, $1"
}

Launch

man launchd.plist - take example from a bottom
mv com.js.dater.plist ~/Library/LaunchAgents
launchctl load com.js.dater.plist

On interval (eg. log), directory change (sync, upload, backup)