A tutorial on using Bash

 

Actual code will be in courier new font (black)

and the output of the code will be in courier new (blue)

 

Administrative commands

 

who

list all users logged onto the machine.

who

johnsmit  pts/1        2016-02-28 06:16 (123.45.678.9)

 

whoami

print your username

whoami

johnsmit

 

df -h

print disk usage (Size, Used, Available, %Used, Mount Point)

df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda1        24G  4.1G   19G  18% /

none             16G   12K   16G   1% /dev

none            3.2G  368K  3.2G   1% /run

none            5.0M     0  5.0M   0% /run/lock

none             16G  4.9M   16G   1% /run/shm

/dev/sdb1       3.6T  2.5T  1.1T  70% /home

/dev/sda5       2.3G  145M  2.1G   7% /tmp

/dev/sda6        51G  3.6G   47G   8% /var

 

du -h

print disk usage (from your current working directory forward, directory by directory). Try du -h > Usage.log

du -h

...

...

84K./templates/beez_20

40K./templates/system/css

8.0K./templates/system/html

40K./templates/system/images

112K./templates/system

2.6M./templates

16K./tmp

126M.

 

shell scripting methods

(for automating pattern-rich tasks, like looping over lists) 

 

For-loops driven by a bash function (echo)

for i in $(echo 1 2 3 4); do

echo $i

done

 

1

2

3

4

 

For-loops driven by a bash function (cat file)

contents of file:

firstline

secondline

thirdline

brasil

shark

 

code to iterate on stuff in a file line-by-line:

for i in $(cat file); do

echo $i

done

 

firstline

secondline

thirdline

brasil

shark