Tuesday, February 23, 2016

Thursday, February 18, 2016

Find In Files Recursively

Its helpful to be able to search for a string recursively in a directory. Here is the basic way:

grep -rnw '/path/to/somewhere/' -e "pattern"
-r or -R is recursive, -n is line number and -w stands match the whole word. -l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude or --include parameter could be used for efficient searching. Something like below:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
Above will exclude searching all the files ending with .o extension. Just like exclude file it's possible to exclude/include directories through --exclude-dir and --include-dir parameter; for example, the following shows how to integrate --exclude-dir:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"


Replace Recursively


find /home/www -type f -print0 | xargs -0 sed -i 's/subdomainA.example.com/subdomainB.example.com/g'

Wednesday, February 17, 2016

Truncating Git History

Look, sometimes you want to be a revisionist, and the only way to get rid of a pesky diff that is taking a lot of commit time is to truncate the commit log. So in the rare occasion you need to do it here is how.

Create a Truncate Script


Use the Commit Sha to Truncate To

source ~/data/bin/git-truncate a71f4386f9d5e3b707067e66fd5f76a1cc06c11c

Force the Push to Origin

git push origin master --force