Skip to content

Instantly share code, notes, and snippets.

View myfavouritekk's full-sized avatar

Kai KANG myfavouritekk

View GitHub Profile
@myfavouritekk
myfavouritekk / handy_shell.sh
Last active August 29, 2015 14:16
Handy Commands in Shell
# Jump back
cd -
# add sudo to last command
sudo !!
# replace a word in last command
^cmd1^cmd2
# Remove surplus carriage returns in windows text files
@myfavouritekk
myfavouritekk / large_files.sh
Last active August 29, 2015 14:15
Dealing with large number of files
# Quickly count, -f disable sorting, adding . and ..
ls -f directory | wc -l
# Quickly remove files
rsync -a --delete empty_directory/ directory/
find directory -delete
# Quickly copy files
rsync -a source/ target/
# Copy list of files
@myfavouritekk
myfavouritekk / remove_windows_carriage_returns.sh
Created December 2, 2014 03:17
Remove surplus carriage returns and end of file characters in windows files.
tr -d '\015\032' < windows_file > unix_file
@myfavouritekk
myfavouritekk / bash_rand_select.sh
Created October 6, 2014 08:52
Random select files in bash
files=(/my/dir/*)
printf "%s\n" "${files[RANDOM % ${#files[@]}]}"
@myfavouritekk
myfavouritekk / msg.scpt
Last active July 3, 2017 18:21
AppleScript: send message in Messages
tell application "Messages"
set theBuddy to buddy "[email protected]" of service "Jabber"
send "Hi" to theBuddy
end tell
@myfavouritekk
myfavouritekk / subl_scope.txt
Created September 2, 2014 06:33
Sublime Text Scope Names
ActionScript: source.actionscript.2
AppleScript: source.applescript
ASP: source.asp
Batch FIle: source.dosbatch
C#: source.cs
C++: source.c++
Clojure: source.clojure
CoffeeScript: source.coffee
CSS: source.css
D: source.d
@myfavouritekk
myfavouritekk / sd.sh
Created August 22, 2014 13:00
OS X: Change directory in Terminal, Sync folder in Finder window.
# change directory and sync Finder window
sd () {
# change terminal directory
cd "$@" || return
# sync Finder window or open new one if not exists
osascript -e "tell application \"Finder\"
if front Finder window exists then
set target of front Finder window to (""\"$PWD\""" as POSIX file)
else
open (""\"$PWD\""" as POSIX file)
@myfavouritekk
myfavouritekk / change_bash.md
Created August 4, 2014 06:35
Change Bash version on OS X

Change Bash version on OS X

  • Installation

sudo port install bash

  • Add it to your shells: add installed bash to /etc/shells

sudo vim /etc/shells

@myfavouritekk
myfavouritekk / tarzip.sh
Last active August 29, 2015 14:04
Linux/Unix: Compress and copy
# Use tar to direct output to standard output "-" and use gzip to compress
tar -cf - <source_file> | gzip -1 > <destination_file>
# Same effect, be sure to put <destination_file> directly behind -f option
tar -czf <destination_file> <source_file>
# Untar
tar -xzf <tar_file>
@myfavouritekk
myfavouritekk / batchCombine.m
Last active February 21, 2017 00:31
Matlab: Process multiple files and combine the results into a cell array.
function [combination] = batchCombine(funhandle, indir, varargin)
%BATCHCOMBINE Process multiple files and combine the results into a cell array
% BATCHCOMBINE(FUNC, INDIR) processes every file or directory in
% INDIR using processing funtion FUNC.
%
% FUNC is a function handle that takes source filename as input variables.
% For example, if FUNC = @foo, then BATCHCOMBINE calls foo(infile) to
% every infile in INDIR.
%
% BATCHCOMBINE(..., 'para1', value1, 'para2', value2, ...) takes extra