CT320: Network and System Administration

Fall 2019

Bash Interactive

Show Lecture.BashInteractive as a slide show.

CT320: Bash Interactive

Interactive Bash use

Linux Commands

Paths

PathExplanation
alphaa file in your current directory
beta/gammaa file in the beta directory, which is in your current directory
/delta/epsilona file in the delta directory, which is inside the root directory.
.the current directory
..the parent directory, one level up
/the root directory
~your home directory (only known to the shell, not in a program)
~zetaUser zeta’s home directory (only known to the shell, not in a program)

Directory Navigation

$ cd ~/tmp
$ pwd
/s/bach/a/class/ct320/tmp
$ ls
backup-2019  final.csv	IQ04.csv      IQ07.csv	IQ11.csv  IQ15.csv   TLD-save
ch	     IQ01.csv	IQ04-old.csv  IQ08.csv	IQ12.csv  Music.ico  vars
enrolled     IQ02.csv	IQ05.csv      IQ09.csv	IQ13.csv  r7	     z
FINAL	     IQ03.csv	IQ06.csv      IQ10.csv	IQ14.csv  st
$ ls ~/bin
checkin		      curve		  imv	p	 scores   wikidiff
checkin-checker       demo-script	  l	peek	 stats	  wikiedit
checkin-file-checker  domoss		  ll	playpen  tools	  wikigrep
checkin_prog	      e			  lsf	pwget	 u	  wikiupdate
chit		      grade		  moss	ruler	 unold	  wikiwhence
cls		      grade-busy	  new	run	 untar
code		      grade-file-checker  note	runner	 vman
cronedit	      grades		  old	save	 wikicat
$ mkdir zulu
$ cd zulu
$ pwd
/s/bach/a/class/ct320/tmp/zulu
$ cd
$ pwd
/s/bach/a/class/ct320
$ rmdir tmp/zulu

File Related

Protection Related

$ date >now
$ ls -l now
-rw------- 1 ct320 class 29 Nov 23 05:15 now
$ chmod -w now
$ ls -l now
-r-------- 1 ct320 class 29 Nov 23 05:15 now
$ echo "hi" >now
/tmp/pmwiki-bash-scriptfluYNt: line 18: now: Permission denied

Process Related

System Related

Linux Shells

Redirection

$ echo "hello" >foo
$ echo "Zeta Eta Theta" >foo
$ date >>foo
$ cat foo
Zeta Eta Theta
Sat Nov 23 05:15:38 MST 2024

What’s the deal with foo?

The word “foo” is used as a general-purpose placeholder in Computer Science.

It’s much the same as how a mathematicians use x as a parameter:

let f(x) be x/2 + 3

No mathematician would ask “What does x mean? Why x? Why isn’t it Ω or q?” We all understand that x is merely a placeholder—it’s just a name.

What’s the deal with foo?

Similarly, in Computer Science, foo is just a placeholder. It has no particular meaning—it’s just a name. CS people also often use bar and, together, Foobar.

Pipes

$ echo "hello" >foo
$ echo "Zeta Eta Theta" >foo
$ date >>foo
$ cat foo
Zeta Eta Theta
Sat Nov 23 05:15:38 MST 2024
$ cat foo | sort
Sat Nov 23 05:15:38 MST 2024
Zeta Eta Theta
$ sort <foo
Sat Nov 23 05:15:38 MST 2024
Zeta Eta Theta
$ sort foo
Sat Nov 23 05:15:38 MST 2024
Zeta Eta Theta

Shell Features

Filter Commands

sort example

$ cd ~ct320/pub
$ cat ducks
Huey (red)
Dewey (blue)
Louie (green)
$ sort ducks
Dewey (blue)
Huey (red)
Louie (green)
$ cat ducks
Huey (red)
Dewey (blue)
Louie (green)

sort example

$ cd ~ct320/pub
$ sort ducks >foo
$ cat ducks
Huey (red)
Dewey (blue)
Louie (green)
$ cat foo
Dewey (blue)
Huey (red)
Louie (green)
$ rm foo

sort example

Sort in reverse order:

$ cd ~ct320/pub
$ sort -r ducks
Louie (green)
Huey (red)
Dewey (blue)

Sort by the second field:

$ cd ~ct320/pub
$ sort -k2 ducks
Dewey (blue)
Louie (green)
Huey (red)

tee example

The tee program, like a pipe fitting, sends the output to two places—standard output, and the given file.

$ sort ~ct320/pub/ducks | tee foo
Dewey (blue)
Huey (red)
Louie (green)
$ echo "I wonder what’s in the file foo?"
I wonder what’s in the file foo?
$ cat foo
Dewey (blue)
Huey (red)
Louie (green)

head, tail examples

$ cat ~ct320/pub/dwarfs
Bashful
Doc
Dopey
Grumpy
Happy
Sleepy
Sneezy
$ head -3 ~ct320/pub/dwarfs
Bashful
Doc
Dopey
$ tail -3 ~ct320/pub/dwarfs
Happy
Sleepy
Sneezy

grep examples

What’s the name of Jack’s computer?

$ grep -i Applin ~info/machines

Oh, that’s right. What’s its IP address?

$ grep greybull /etc/hosts

find examples

Find all the files in a given directory hierarchy whose names contain “bash”:

$ cd
$ find public_html/CurrentSemester/ -iname '*bash*' | sort
public_html/CurrentSemester/pmwiki/wiki.d/Lab.BashI
public_html/CurrentSemester/pmwiki/wiki.d/Lab.BashII
public_html/CurrentSemester/pmwiki/wiki.d/Lecture.BashInteractive
public_html/CurrentSemester/pmwiki/wiki.d/Lecture.BashScripts
public_html/CurrentSemester/pub/HW5-solution-bash.html
public_html/CurrentSemester/pub/HW6-solution-bash.html

Find at most five lectures modified within a month:

$ cd ~/public_html/CurrentSemester
$ find -name 'Lecture.*' -mtime -30 -exec ls -dhog {} + | head -5

Combination example

Linux has a list of words:

$ grep -i applin /usr/share/dict/words
Appling
appling
dappling
grappling
intergrappling
ungrappling
$ tail /usr/share/dict/words
Zythia
zythum
Zyzomys
Zyzzogeton
zyzzyva
zyzzyvas
ZZ
Zz
zZt
ZZZ

Combination example

Let’s get just the first three letters (prefixes):

$ cut -c1-3 /usr/share/dict/words | tail
Zyt
zyt
Zyz
Zyz
zyz
zyz
ZZ
Zz
zZt
ZZZ

Combination example

They’re probably already in order, but let’s make sure:

$ cut -c1-3 /usr/share/dict/words | sort | tail
zyt
Zyt
zyz
zyz
Zyz
Zyz
Zz
ZZ
zZt
ZZZ

Combination example

Now, count the prefixes:

$ cut -c1-3 /usr/share/dict/words | sort | uniq -c | tail
      3 Zyr
      1 Zys
      2 zyt
      1 Zyt
      2 zyz
      2 Zyz
      1 Zz
      1 ZZ
      1 zZt
      1 ZZZ

Combination example

Which the prefixes are the most common?

$ cut -c1-3 /usr/share/dict/words | sort | uniq -c | sort -n | tail
   2628 und
   2719 ant
   2814 sub
   2842 int
   3044 dis
   3210 con
   3523 pro
   4070 ove
   5497 pre
   7804 non

Resources