Portions Copyright © 2005-06 Python Software Foundation.
A Shell in Action
Operating System
A Directory Tree
Parent Directories
$ pwd
/home/hpotter/swc
$ ls
LICENSE.txt conf data docs index.swc license.swc print.css swc.css tests
Makefile config.mk depend.mk img lec press sites swc.dtd util
$ ls data
bio elements haiku.txt morse.txt pdb solarsystem.txt
(The “$” in the examples above is the shell "prompt" character. It means the shell is waiting for keyboard input.)
$ cd data
$ pwd
/home/hpotter/swc/data
$ ls
bio elements haiku.txt morse.txt pdb solarsystem.txt
$ cd ..
$ pwd
/home/hpotter/swc
Running a Program
$ ls -F
LICENSE.txt conf/ data/ docs/ index.swc print.css swc.css tests/
Makefile config.mk depend.mk img/ lec/ sites/ swc.dtd util/
$ ls -a
. .svn Makefile config.mk depend.mk img lec sites swc.dtd util
.. LICENSE.txt conf data docs index.swc print.css swc.css tests
$ ls -a -F
. .svn/ Makefile config.mk depend.mk img/ lec/ sites/ swc.dtd util/
.. LICENSE.txt conf/ data/ docs/ index.swc print.css swc.css tests/
$ mkdir tmp
$ cd tmp
$ ls
Name: Earth
Period: 365.26 days
Inclination: 0.00
Eccentricity: 0.02
$ cp earth.txt venus.txt
$ edit venus.txt
$ ls -t
venus.txt earth.txt
$ cat venus.txt
Name: Venus
Period: 224.70 days
Inclination: 3.39
Eccentricity: 0.01
$ ls -l
total 2
-rwxr-xr-x 1 gvwilson bmi219 73 Jan 4 15:58 earth.txt
-rwxr-xr-x 1 gvwilson bmi219 73 Jan 4 15:58 venus.txt
$ wc earth.txt venus.txt
4 9 73 earth.txt
4 9 73 venus.txt
8 18 146 total
man | Documentation for commands. | mkdir | Make directories. |
cat | Concatenate and display text files. | more | Page through a text file. |
cd | Change working directory. | mv | Move (rename) files and directories. |
clear | Clear the screen. | od | Display the bytes in a file. |
cp | Copy files and directories. | pwd | Print current working directory. |
date | Display the current date and time. | rm | Remove files. |
diff | Show differences between two text files. | rmdir | Remove directories. |
echo | Print arguments. | sort | Sort lines. |
grep | Print lines matching a pattern. | tail | Display the last few lines of a file. |
head | Display the first few lines of a file. | uniq | Remove adjacent duplicate lines. |
ls | List files and directories. | wc | Count lines, words, and characters in a file. |
Makefile biography.txt data enrolment.txt programs thesis
thesis programs enrolment.txt data biography.txt Makefile
Name: Earth
Period: 365.26 days
Inclination: 0.00 degrees
Eccentricity: 0.02
Satellites: 1
You can then compare the two files like this:
$ diff earth.txt earth2.txt
3c3
< Inclination: 0.00
---
> Inclination: 0.00 degrees
4a5
> Satellites: 1
(The rather cryptic header "3c3" means that line 3 of
the first file must be changed to get line 3 of the second;
"4a5" means that a line is being added after line 4 of the
original file.)
$ ls bio/*.txt
bio/albus.txt bio/ginny.txt bio/harry.txt bio/hermione.txt bio/ron.txt
Redirecting stdin and stdout
command < input_file...reads from input_file instead of from the keyboard. You don't need to use this very often, because most Unix commands let you specify the input file (or files) as command-line arguments.
command > output_file...writes to output_file instead of to the screen. Only "normal" output goes to the file, not error messages.
command < input_file > output_file...does both.
$ cd bio
$ wc *.txt > words.len
$ cat words.len
7 66 468 albus.txt
5 46 311 ginny.txt
5 49 342 harry.txt
5 49 331 hermione.txt
6 54 364 ron.txt
28 264 1816 total
$ wc -w *.txt > words.tmp
$ sort -n words.tmp
46 ginny.txt
49 harry.txt
49 hermione.txt
54 ron.txt
66 albus.txt
264 total
$ rm words.tmp
$ wc -w *.txt | sort -n
46 ginny.txt
49 harry.txt
49 hermione.txt
54 ron.txt
66 albus.txt
264 total
$ grep 'Title' spells.txt | sort | uniq -c | sort -n -r | head -10 > popular_spells.txt
$ printenv
MANPATH=:/usr/local/sge/man
XDG_SESSION_ID=2215
HOSTNAME=watson.cgl.ucsf.edu
TERM=xterm-256color
SHELL=/usr/local/bin/bash
HISTSIZE=1000
SSH_CLIENT=169.230.11.112 63047 22
OLDPWD=/home/socr/a/tef
SSH_TTY=/dev/pts/2
SVN_EDITOR=vi
USER=tef
SSH_AUTH_SOCK=/tmp/ssh-OLrXGv99V3/agent.33503
MAIL=/var/spool/mail/tef
PATH=/home/socr/a/tef/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/sge/bin/linux-x64:.
LC_COLLATE=C
PWD=/usr/local/www/rbvi/preview/htdocs/Outreach/bmi219/slides
LANG=en_US.UTF-8
SGE_ROOT=/usr/local/sge
HISTCONTROL=ignoredups
SHLVL=1
HOME=/home/socr/a/tef
LOGNAME=tef
PRINTER=lys
SSH_CONNECTION=169.230.11.112 63047 169.230.27.29 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/20021
DISPLAY=localhost:10.0
$ echo $HOME
/home/socr/a/tef
(Don't confuse the shell prompt character (also “$”) with the “$” used to identify a variable.)
Name | Typical Value | Notes |
SVN_EDITOR | vi | Preferred editor for use with svn command |
HOME | /home/socr/a/tef | The current user's home directory |
HOSTNAME | crick.cgl.ucsf.edu | This computer's name |
LINES | 60 | The height in characters of the current display |
HISTSIZE | 1000 | Number of shell commands remembered in history |
PATH | /home/socr/a/tef/bin:/usr/local/bin:/usr/bin:/bin/ | Where to look for programs |
PWD | /home/socr/a/tef/misc | Present working directory (sometimes CWD , for current working directory) |
LOGNAME | tef | Login name |
SHELL | /usr/local/bin/bash | What shell is being run |
TERM | xterm-256color | Terminal type |
USER | tef | The current user's ID |
$ VILLAIN="Lord Voldemort"
Notice that values with spaces in them have to be quoted!
$ VILLAIN="Lord Voldemort"
$ bash
$ echo $VILLAIN
$ exit
$ VILLAIN="Lord Voldemort"
$ export VILLAIN
$ bash
$ echo $VILLAIN
Lord Voldemort
$ exit
$ export VILLAIN="Lord Voldemort"
$ bash
$ echo $VILLAIN
Lord Voldemort
$ exit
Setting a Variable Without Exporting It
Exporting a Variable's Value
# Add personal tools directory to PATH.
PATH=$HOME/bin:$PATH
# Personal settings
export EDITOR=/local/bin/emacs
export PRINTER=gryffindor-laserwriter
# Change default behavior of commands.
alias ls="ls -F"
Note: .bashrc files can become very complex...
#!/usr/bin/bash
rm -f *.junk
chmod | Change file and directory permissions. |
du | Print the disk space used by files and directories. |
find | Find files with names that match patterns, that are of a certain age or size, etc. |
grep | Print lines matching a pattern. |
gunzip | Uncompress a file. |
gzip | Compress a file. |
lpr | Send a file to a printer. |
lprm | Remove a print job from a printer's queue. |
lpq | Check the status of a printer's queue. |
ps | Display running processes. |
tar | Archive files. |
which | Find the path to a program. |
who | See who is logged in. |
xargs | Execute a command for each line of input. |
-rwxr-xr-x 1 aturing cambridge 69 Jul 12 09:17 mars.txt
-rwxr-xr-x 1 ghopper usnavy 71 Jul 12 09:15 venus.txt
According to the listing of the directory above,
who can read the file earth.txt? Who can write it (i.e.,
change its contents or delete it)? When was >earth.txt
last changed? What command would you run to allow everyone to edit or delete the file?
Name: Earth
Period: 365.26 days
Inclination: 0.00 degrees
Eccentricity: 0.02
Satellites: 1
grep can extract lines containing the text
“Period” from all the files:
$ grep Period *.txt
earth.txt:Period: 365.26 days
venus.txt:Period: 224.70 days
Search strings can use regular expressions,
which will be discussed in a later lecture. grep takes
many options as well; for example, grep -c /bin/bash /etc/passwd reports how many lines
in /etc/passwd (the Unix password file) that contain the string
“/bin/bash”, which in turn tells me how many users are using “bash“
as their shell.
Suppose all you wanted was a list of the files that contained lines matching a pattern, rather than the matches themselves --- what flag or flags would you give to grep? What if you wanted the line numbers of matching lines?