The Unix Commandline

By Jeetaditya Chatterjee

Your inner hacker will be released today

Press s for speaker notes

What is the Unix commandline?

Why do I need to know the Unix command line?

What do I need too know then?

A little bit of syntax

Variables

var="some value"
export ENVAR="some global value"

command_out=$(some command)
echo $var
echo $ENVAR
echo $command_out

flags

command -l # short form flags
command -la # 2 short form flags
command --long # long form flags
command --input INPUT # example of input

The main commands

Moving around

pwd

Prints the current directory

$ pwd

cd

change directory

$ cd ~/your/directory
$ pwd
/home/$USER/your/directory
$ cd ~ # cd /home/$USER
$ pwd
/home/jeet
$ cd ..
$ pwd
/home

ls

list the current directory’s contents

$ ls
$ ls -a
$ ls -l
$ ls -la

Files

touch

$ touch file

$ touch existing-file -d month/day/year

mv

$ mv SOURCE DESTINATION

$ ls
file1
$ mv file1 file2
$ ls
file2

$ ls
dir1 dir2
$ mv dir1 dir2
mv: cannot move 'dir1' ro 'dir2' : Directory not empy
$ mv dir1 dir2 -f
$ ls
dir2

cp

$ cp SOURCE DEST

$ ls
file1
$ cp file1 file2
$ ls
file1 file2

ln

$ ln source-file dest-file
$ ls -l
-rw-rw-r-- jeet jeet ... source-file
-rw-rw-r-- jeet jeet ... dest-file

$ ln source-file dest-file -s
$ ls -l
-rw-rw-r-- jeet jeet ... source-file
lrwxrwxrwx jeet jeet ... dest-file -> source-file

rm

$ rm file -i
rm: remove regular file 'file'? # your input here
# no more file
$ rm dir
rm: cannot remove 'dir' : Is a directory
$ rm dir -r
# all good

mkdir

$ mkdir dir

$ mkdir dir1/dir2/dir2
mkdir: cannot create directory 'dir1/dir2/dir3': No such file or directory
$ mkdir dir1/dir2/dir2 -p

Reading and writing files

cat

$ cat file
# contense of file
$ cat file1 file2
# contense of both files

head

$ head file -n 20

tail

$ tail file -n 20

chmod

$ ls -l
-rw-rw-r-- ... file
$ chmod +x file
$ ls -l
-rwxrwxr-x ... file

$ chmod -rwx file
---------- ... file

Transforming text

echo

echo "a string"
a string
echo -e "this is a \n multiline string "
this is a
 multiline string
echo $VAR
# contense of VAR

grep

$ cat file
fox
fax
fex
$ grep "fox" file
fox
$ grep "f.x" file

$ grep "fox" file -v
fax
fex

sed

$ sed 's/REGEX/REPLACEMENT/FLAG' file

$ cat file
the fox did fox things to say fox you to big fox
$ sed 's/fox/wolf/' file
the wolf did fox things to say fox you to big fox

$ sed 's/fox/wolf/g' file
the wolf did wolf things to say wolf you to big wolf

$ sed 's/fox/wolf/g' file -i

$ cat file
the wolf did wolf things to say wolf you to big wolf

awk

some-pattern { some action }
another-pattern { a different action }
{ all lines match this action }

$0 ~ /some regex/ {
    print $1, $2 # for example
}

$1 == 18 {
    print $2, $1
}
$ cat file
this file is has space seperated words
it has multiple lines
$ awk '{print $0}' file # we just made a slower less convenient cat!
this file is has space seperated words
$ awk '{print $1}' file # print the first element of each line
this
it
$ cat file.csv
name,age,job
Jeet,18,Being a Nerd

$ awk -F, '{printf("name:%s job:%s age:%s", $1, $3, $2)}' file.csv
name:Jeet job:Being a nerd age:18

sort

$ cat file-o-numbers
1
15
2
30
$ sort -n file-o-numbers
1
2
15
30

uniq

$ cat file
this
this
has
some
some
duplicates

$ uniq file
this
has
some
duplicates

wc

$ cat file
line 1
line 2
line 3

$ wc file
3 6 24
$ wc -l
3
$ wc -w
6
$ wc -c
24

User management

sudo

$ apt update
Permission denied
$ sudo apt update
# works

w

$ w

 02:26:18 up  8:52,  2 users,  load average: 0.94, 1.29, 1.15
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
jeet     :1       :1               17:34   ?xdm?   4:07m  0.01s /usr/libexec/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=pop /usr/bin/gnome-session --session=pop
root     tty4     -                02:24    1:52   0.02s  0.01s -bash

Process management

pgrep

$ pgrep emacs
23878
23878

$ pgrep firefox
6997

pkill

$ pkill emacs # waits for emacs to finish

$ pkill 23878 -9 # kills emacs instantly

htop

$ htop

Others

man

$ man command

Honorable mentions

nc
The networking swiss army knife
calc
like awk for numbers
lp
Have you ever needed too print something? well now you can on the command line!
less
Read files at your pleasure

Pipes

Output redirection

$ cat file1 file2 > file1+file2

$ cat non-existant-file 2> /dev/null # no errors and no output

Output appendation?

$ some-output >> some-log-file

Pipes

$ cat file
Jeet,18
Jen,19
Jeff,20
Jerome,16
Dan,18
Mark,17

$ cat file | grep -i "j." | sort -d | awk -F, '$2 >= 18 {print "name:", $1, "age:", $2}'
name: Jeet age: 18
name: Jen age: 19

An actual example

gist_list=$(gh gist list)
gist_to_find=$(echo "$gist_list" | awk '{print $2}' | fzf --layout=reverse)

gh gist edit $(echo "$gist_list" | grep $gist_to_find | awk '{print $1}')

What to do from here?

Any Questions?