Show Lecture.BashInteractive as a slide show.
CT320: Bash Interactive
Interactive Bash use
- Linux Commands
- Linux Shells: sh, csh, bash
- Redirection
- Pipes
- History and Editing
- Filters
Linux Commands
- Terse abbreviations!
- command options arguments
man
command
Paths
Path | Explanation |
alpha | a file in your current directory |
beta/gamma | a file in the beta directory, which is in your current directory |
/delta/epsilon | a 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) |
~zeta | User 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
cd
path — change current directory
ls
path — list current (specified) directory
pwd
— show current directory
mkdir
path — create a new directory
rmdir
path — remove a (empty) directory
File Related
cat
, less
, more
file — display file contents
vi
, gedit
file — edit file contents
cp
path path — copy file or directory
mv
path path — move (rename) file or directory
rm
path — remove file or directory
diff
path path — compare file or directory
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
chmod
permissions path — change permissions
chown
owner path — change (user) ownership
- If you have sufficient permissions.
chgrp
group path — change (group) ownership
- If you have sufficient permissions.
Process Related
ps
— display running processes
kill
— terminate running processes
bg
, fg
, nice
— process control commands
System Related
shutdown
time — system shutdown
reboot
— system reboot
date
— system date and time
time
command — measures command timing
whereis
command — find command instances
dmesg
— system boot messages
Linux Shells
- What is a shell?
- Command-Line Interpreter
- Operating System Interface
- Primitive Language!
- Bourne Shell — sh
- Bourne (1977): Pipeline, Redirection, Variables, Wildcards
- C Shell — csh
- Joy (1978): History, Editing, Aliases, Stacks, Tilde Notation
- Bash Shell — bash
- Fox (1989): Currently used for Unix, Linux, and Mac
Redirection
- Streams: stdin, stdout, and stderr
- Can be redirected to terminal, file, other programs
- Use
>
to create/overwrite with stdout, >>
to append, <
for stdin
$ 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.
- In U.S. law, the name John Doe is often used to
indicate an unknown person.
- A citizen might be described as John Q. Public,
from Anytown, U.S.A.
- You might call an unknown object a widget, a thingie,
or a whatchamacallit.
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
- Connect stdout to stdin
- Example:
ls | less
- Example:
find . -name temp.dat | grep -v zulu
Shell Features
- Editing mode:
set -o vi
or set -o emacs
- History — navigates command history
history
— shows previous commands
- up/down arrows move in command history
- Backspace and Delete remove characters
- Searching is esc-/ (vi mode) or ctrl-R (emacs mode)
Filter Commands
- Read stdin and write stdout
sort
— sort input lines to output lines
-b
: ignore white space
-f
: case insenstive sorting
-n
: compare fields as numbers
-r
: reverse sort order
-k
: specify sorting column
tee
— copy input to two places
head
, tail
— print beginning or head of file
grep
"regular-expression" — search for text
find
directory conditions — look for files
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
does not change the input file.
- It produces a sorted version of the file on its standard output
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
- Here, we saved the sorted version in the file
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