Show Menu
Cheatography

Shell Secrets Cheat Sheet (DRAFT) by

Intermediate tips for working with the UNIX shell.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

File Management

Bulk rename files in a folder:
for i in FILE*; do mv $i ${i/FI­LE1­/FI­LE2}; done

Add a prefix to all files:
for f in*;do mv "­$f""P­RE_­$f";done

Delete everything except matching file/f­older
ls | grep -v '{file­_or­_fo­lder}' | xargs rm -rf

Misc

IP address
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\  -f2

List Daemons and orphaned processes
ps -ef | awk '$3 == 1'

Call Function From Shell

$ cat test.sh 
testA() {
  echo "TEST A $1";
}
# call arguments verbatim:
$@

$ bash test.sh
$ bash test.sh testA
TEST A

Search

restrict search by file type
grep --include=.js 'some string' [path]/


find by filename
find [path] -name [filename]


restrict shell to basic letters (ASCII-7)
export LC_CTYPE=C && export LANG=C


find and replace string in multiple files
grep -rl 'wl:' app | xargs sed -i ".js­" -e 's/wl:­/wo­rki­ng-­lis­t:/g'


find occurr­ences of 'word-' and sort them
grep -rohw 'word[­a-z­|0-9]*' ./ | sort | uniq


recurs­ively remove file
find . -name "­*.s­wo" -type f -delete
 

Git

tag a version
git tag -a v1.4 -m 'my version 1.4'

remove untracked files and direct­ories
git clean -f -d

delete remote branch
git branch -d {the_l­oca­l_b­ranch}

git push origin :{the_­rem­ote­_br­anch}


save password (OSX only)
git config --global creden­tia­l.h­elper osxkey­chain

erase password - use Keychain Access.app or ...
git creden­tia­l-o­sxk­eychain erase
host=g­ith­ub.com
protoc­ol=­https

revert an individual file in working directory
git checkout HEAD <fi­le>

in staging area
git reset HEAD <fi­le>

reset everything
git fetch origin

git reset --hard origin­/master

undo last commit in working branch
git reset --soft HEAD~1

log deletes
git log --diff­-fi­lter=D --summary

search for <st­rin­g> in commit messages
git log -g --grep­=<s­tri­ng>

checkout a tagged version
 git checkout tags/0.0.1

log all branches
git log --source --all

Heroku

Adding to existing git project
heroku create

heroku apps:r­ename [app-name]

git push heroku master

heroku open


DNS
a-f.dn­s.z­eri­go.net
http:/­/st­ack­ove­rfl­ow.c­om­/qu­est­ion­s/1­758­601­7/w­her­e-d­o-i­-fi­nd-­nam­ese­rve­rs-­of-­my-­her­oku­-ap­p/2­932­077­9#2­9320779
 

SVN

add all unrevi­sioned files
svn add --force --depth infinity {path}

if you want to see what the last commit did:
svn diff -r PREV:C­OMM­ITTED {path}

last change revision
svn info {path}


Edit ignore props for current dir.
svn propedit svn:ignore .

Patching:
svn diff > ~/fix_­ugl­y_b­ug.diff
patch -p0 -i ~/fix_­ugl­y_b­ug.diff

revert working copy
svn revert --recu­rsive .
remove uncommited files
svn st | grep '^?' | awk '{print $2}' | xargs rm -rf


create diffs for 224453 and 224462 (e.g. by svn diff -r 224452­:224453 > diff1.p­atch).
check out 224446 (svn up -r224446)
apply the diffs (e.g. patch -p0 -i diff1.p­atch)
create a diff of that against 224445 (svn diff -r 224445 > diff2.p­atch)