CT320: Network and System Administration

Fall 2018

Bash Interactive

See this page 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
253		CT-320-L02.csv	IQ03	  L01.txt  disk-full-lab
CT-320-L01.csv	IQ02.csv	IQ03.csv  L02.txt  whatever
$ ls ~/bin
checkin		      demo-script	  imv	old	 runner  vman
checkin-file-checker  domoss		  l	p	 save	 wikicat
checkin_prog	      e			  ll	peek	 scores  wikidiff
chit		      grade		  lsf	playpen  stats	 wikiedit
cls		      grade-busy	  moss	pwget	 tools	 wikigrep
cronedit	      grade-file-checker  new	ruler	 u	 wikiupdate
curve		      grades		  note	run	 unold	 wikiwhence
$ 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 Sep 21 07:40 now
$ chmod -w now
$ ls -l now
-r-------- 1 ct320 class 29 Sep 21 07:40 now
$ echo "hi" >now
.script: 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
Fri Sep 21 07:40:51 MDT 2018

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
Fri Sep 21 07:40:51 MDT 2018
$ cat foo | sort
Fri Sep 21 07:40:51 MDT 2018
Zeta Eta Theta
$ sort <foo
Fri Sep 21 07:40:51 MDT 2018
Zeta Eta Theta
$ sort foo
Fri Sep 21 07:40:51 MDT 2018
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
greybull	HP-Z200-Xeon3450-SAS	4x2.66G	8Gb	Linux(Fedora)	r/t	246-applin	2UA1071PQP	78:ac:c0:97:44:37

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

$ grep greybull /etc/hosts
129.82.45.189	greybull.cs.colostate.edu greybull

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

Find at most five lectures modified within a month:

$ cd ~/public_html/CurrentSemester
$ find -name 'Lecture.*' -mtime -30 -exec ls -dhog {} + | head -5
-rw------- 1  34K Aug 30 12:31 ./pmwiki/wiki.d/Lecture.Access
-rw------- 1 104K Sep 17 23:05 ./pmwiki/wiki.d/Lecture.Accounts
-rw------- 1 114K Aug 30 14:53 ./pmwiki/wiki.d/Lecture.BashScripts
-rw------- 1  84K Aug 23 13:50 ./pmwiki/wiki.d/Lecture.Filesystem
-rw------- 1  60K Sep  9 16:10 ./pmwiki/wiki.d/Lecture.Packet

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
zym
zym
zym
zym
zym
zym
zyt
zyt
zyz
zyz

Combination example

Now, count the prefixes:

$ cut -c1-3 /usr/share/dict/words | sort | uniq -c | tail
      2 zur
      1 zuu
      1 zuz
      1 zwa
      7 zwi
      2 zyd
     95 zyg
     54 zym
      2 zyt
      2 zyz

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

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-06-24T18:03

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building