Skip to content

Instantly share code, notes, and snippets.

View rcanepa's full-sized avatar

Renzo Canepa rcanepa

  • Santiago, Chile
View GitHub Profile
$ jot - 1 5
^
return numbers from 1 to 5
1
2
3
4
5
$ jot -b "#" - 1 5
@rcanepa
rcanepa / tarpipe.md
Last active August 12, 2016 16:28
UNIX tarpipe, gzip
```
$ (cd src && tar -c .) | (cd dst && tar -xp)
1^ 2^ 3^
```
1^ this fork a new process that execute those commands and use the writing end of the pipe as its STDOUT
2^ the pipe has two ends (to write and read), and both of them have a fd
3^ this fork a new process that execute those commands using the reading end of the pipe as its STDIN
Also, using && instead of ; to separate commands means that if the first one fails, the second one won't be executed.
@rcanepa
rcanepa / pipingbash.md
Created August 11, 2016 03:26
Piping to bash scripts

Bash accomodates piping and redirection by way of special files. Each process gets it's own set of files (one for STDIN, STDOUT and STDERR respectively) and they are linked when piping or redirection is invoked. Each process gets the following files:

STDIN - /proc//fd/0 STDOUT - /proc//fd/1 STDERR - /proc//fd/2

To make life more convenient the system creates some shortcuts for us:

STDIN - /dev/stdin or /proc/self/fd/0 STDOUT - /dev/stdout or /proc/self/fd/1

@rcanepa
rcanepa / awk.md
Last active August 10, 2016 19:51
UNIX awk

#awk In awk, a file is treated as a sequence of records, and by default each line is a record and every word is a field.

$ cat text
this is the first line
second one
third one
who cares anymore?
....
@rcanepa
rcanepa / gist:07d414b20d6f757cf6d04dc7e6ec4ebf
Last active August 8, 2016 03:58
UNIX sed (stream editor)
$ cd /tmp/
$ echo '10 tiny toes' >> text
$ echo 'this is that' >> text
$ echo '5 funny 0' >> text
$ echo 'one two three' >> text
$ echo 'tree twice' >> text
$ cat text
10 tiny toes
this is that
- Count the number of namespaces
1) find all .clj files under src/scv2backend/ directory,
2) find all the lines which contains the words 'ns scv2',
3) split the line by a white space and return the second half,
4) count the number of lines
find src/scv2backend/ -name '*.clj' |
xargs grep -h 'ns scv2' |
cut -d ' ' -f 2 |
wc -l
- diff processes outputs
$ diff -u <(ps aux) <(ps aux)
--- /dev/fd/63 2016-08-06 01:02:53.000000000 -0400
+++ /dev/fd/62 2016-08-06 01:02:53.000000000 -0400
@@ -312,12 +312,12 @@
root 44 0.0 0.0 2518564 1944 ?? Ss Wed07PM 0:45.29 /usr/sbin/syslogd
root 43 0.0 0.1 2551932 10704 ?? Ss Wed07PM 0:23.84 /usr/libexec/UserEventAgent (System)
root 1 0.0 0.1 2538696 11456 ?? Ss Wed07PM 5:36.04 /sbin/launchd
-root 20967 0.0 0.0 2435076 1016 s001 R 1:02AM 0:00.00 ps aux
+root 20967 0.0 0.0 2435232 1032 s001 R 1:02AM 0:00.00 ps aux
Grep searches strings/patterns inside other strings/text.
Basic usage:
grep some-string some-file
-> This will return all lines of the file named 'some-file' which contains the string 'some-string'.
ls | grep some-string
-> This will return all files which contains the string 'some-string' in their names
- fetch a git commit log
git log -1 <hash>
- fetch commit list (reversed: last one is first)
git rev-list HEAD
- execute git log for every commit (on the reversed commit list)
git rev-list --reverse HEAD | while read rev; do git log -1 $rev; done
- print file tree for every commit
@rcanepa
rcanepa / gist:bea682984b120ed017606caa1bd2e16c
Created August 3, 2016 22:36
Recursively find pattern in files
grep -rnw . -e ".*\.extend" --include=\*.js
-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.
More examples
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"