Chris IJ Hwang

I am a Quantitative Analyst/Developer and Data Scientist with backgroud of Finance, Education, and IT industry. This site contains some exercises, projects, and studies that I have worked on. If you have any questions, feel free to contact me at ih138 at columbia dot edu.

View My GitHub Profile



Contents

Linux Command Quick Reference

find

find is inherently recursive and is intended to descent through directories looking for files with certain attributes or executing commands. cf) -type f --> plain file; d --> directory p --> fifo or named pipe 1.) Find all files modified between 50 ~ 100 days

	find . -mtime +50 -mtime -100
2.) Find all files permission 777

	find . -type f -perm 0777
3.) Find all read-only file

	find . -perm /u=r
4.) Find all excutable file

	find . -perm /a=x
5.) Find all empty file

	find . -type f -empty
6.) Find all files basedon user

	find . -user "username"
7.) Find all c files then change mode
find . -name "*.c" -exec chmod g+w {} \;


grep

grep [options] pattern [files] -c ::count. With -v, count nonmatching lines -d read ::read directories like ordinary files (default) skip ::skip directories recurse:: recursively ... same as -r examples: 1.)
$ cat a
this is line 1 UNIX UNIX
this is line 2 unix
this is line 3 Unix Unix
this is line 4 hello
2.)
$ grep unix a
this is line 2 unix
$ grep -c -a unix a
1
3.)Ignore upper and lower case
$ grep -i unix a
this is line 1 UNIX UNIX
this is line 2 unix
this is line 3 Unix Unix
4.)Add line number
$ grep -i -n unix a
1:this is line 1 UNIX UNIX
2:this is line 2 unix
3:this is line 3 Unix Unix
$ grep -i -n hello a
4:this is line 4 hello
5.)Print only patter parts
$ grep -o unix a
unix
$ grep -oi unix a
UNIX
UNIX
unix
Unix
Unix
6.)Print lines not containing patterns
$ grep -vin unix a
4:this is line 4 hello
7.)Search file for multiple patterns or for pattern beginning with hyphen
grep -n -e unix -e hello a
2:this is line 2 unix
4:this is line 4 hello
8.)Multiple files
$ cat a
this is line 1 UNIX UNIX
this is line 2 unix
this is line 3 Unix Unix
this is line 4 hello
$ cat b
this is line 1 hello
this is line 2 Unix
this is line 3 UNIX UNIX
this is line 4 unix unix
$ grep -in unix a b
a:1:this is line 1 UNIX UNIX
a:2:this is line 2 unix
a:3:this is line 3 Unix Unix
b:2:this is line 2 Unix
b:3:this is line 3 UNIX UNIX
b:4:this is line 4 unix unix
$ find .
.
./a
./b
$ grep -r unix .
./a:this is line 2 unix
./b:this is line 4 unix unix

Print 3 lines before, 2 lines after term "unix".
$ grep -B 3 -A 2 unix .


## Serching pattern
What it does Pattern
The string bag bag
"bag" at the begining of line or string. ^bag
"bag" at the end of line or string. bag$
"bag" as the only text on line ^bag$
"Bag" or "bag" [Bb]ag
Second character is vowel b[aeiou]g
Second character is NOT vowel b[^aeiou]g
Second character is any character except new line b.g
Any line containing exactly three characters. ^...$
Any line that begins with a dot ^\.
Any line that doesn't begin with a dot ^[^.]
"bug", "bugs", "bugss", etc bugs*
A word in quotes "word"
A word, with or without quotes "*word"*
One or more uppercase letters [A-Z][A-Z]*
An uppercase letter, followed by zero or more characters [A-Z].*
Zero or more uppercase letters. [A-Z]*
Any letter. [a-zA-Z]
Any alphanumeric sequence [0-9A-Za-z]+

Showing large files

1.) Using head and tail command default : read 10 lines
$ cat a
this is line 1 UNIX UNIX
this is line 2 unix
this is line 3 Unix Unix
this is line 4 hello
$ head -n 2 a
this is line 1 UNIX UNIX
this is line 2 unix
$ head -n +2 a
this is line 1 UNIX UNIX
this is line 2 unix
read last 2 lines
$ tail -n 2 a
this is line 3 Unix Unix
this is line 4 hello
read from 2nd line to last
$ tail -n +2 a
this is line 2 unix
this is line 3 Unix Unix
this is line 4 hello
read 3rd line only
$ tail -n +3 a | head -n 1 
this is line 3 Unix Unix
2.)Using "cut"
[options]
+ -d --> delimiter, special character should be quoted " ". with -f1 --> 1st column
+ -c --> print characters in list columns
$ cat a
this is line 1 UNIX UNIX
this is line 2 unix
this is line 3 Unix Unix
this is line 4 hello
$ cut -c 1 a
t
t
t
t
$ cut -c 1,4 a
ts
ts
ts
ts
$ cut -c 1-4 a
this
this
this
this
$ cut -d" " -f1 a
this
this
this
this
3.)Using sed
Read 2nd and 4th line
$ sed -n -e 2p -e 4p a
this is line 2 unix
this is line 4 hello
Read 2nd to 4th line
$ sed -n 2,4p a
this is line 2 unix
this is line 3 Unix Unix
this is line 4 hello

sed

Syntax

sed [options] 'command' file(s) sed [options] -f scriptfile file(s) From lynda sed 's/a/d'

s: substitution

a: search string

b: replacement string

ex)
$ echo 'upstarem' | sed 's/up/down/'
downstarem
$ cat a | sed 's/is/are/'
thare is line 1 UNIX UNIX
thare is line 2 unix
thare is line 3 Unix Unix
thare is line 4 hello
That is, it is not global default. 'g' should be added. However, remember that each line is a stream. So, 'is' in each stream (line) is changed not second one.
$ cat a | sed 's/is/are/g'
thare are line 1 UNIX UNIX
thare are line 2 unix
thare are line 3 Unix Unix
thare are line 4 hello
$ cat a | sed -e 's/this/that/g' -e 's/is/are/g' -e 's/line/line number/g'
that are line number 1 UNIX UNIX
that are line number 2 unix
that are line number 3 Unix Unix
that are line number 4 hello

options

  • -n Suppress the default output
  • -e editing command are followed.

commands

[address [,address]][!]command[arguments]

  • a\ Append text after a line
  • c\ Replace text(usually text block)
  • i\ Insert text before a line.
  • d Delete lines.
  • s Make substitutions
  • y Translate characters