Figure 3.1: A Shell in Action
sh, is an ancestor of many of them
bash (the Bourne Again Shell) in this course
Figure 3.2: Operating System
Figure 3.3: A Directory Tree
notes.txt or home.html.txt is associated with an editor, and .html with a web browser/
"/"/home/hpotter is Harry Potter's home directory/courses/swc/wlec/shell.html is this file/courses/swc, the relative path to this file is wlec/shell.html
Figure 3.4: Parent Directories
"." (pronounced "dot") is the current directory".." (pronounced "dot dot") is the directory one level up
/courses/swc/data, .. is /courses/swc/courses/swc/data/elements, .. is /courses/swc/datapwd (short for "print working directory") to find out where you are
$ pwd/home/hpotter/swc
ls (for "listing") to see what's in the current directory
$ lsLICENSE.txt conf data docs index.swc license.swc print.css swc.css testsMakefile config.mk depend.mk img lec press sites swc.dtd util
data directory, type ls data
$ ls databio elements haiku.txt morse.txt pdb solarsystem.txt
cd data to "go into" data
datals on its owncd .. to go back to where you started$ cd data$ pwd/home/hpotter/swc/data$ lsbio elements haiku.txt morse.txt pdb solarsystem.txt$ cd ..$ pwd/home/hpotter/swc
ls, the OS:
Figure 3.5: Running a Program
ls produce more informative output by giving it some flags
"-", as in "-c" or "-l"$ ls -FLICENSE.txt conf/ data/ docs/ index.swc print.css swc.css tests/Makefile config.mk depend.mk img/ lec/ sites/ swc.dtd util/
.
ls doesn't show things whose names begin with .
. and .. don't always show up$ 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
.svn directory is for later$ 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
-v ("verbose") would tell mkdir to print a confirmation message)$ cd tmp$ ls
earth.txt with the following contents:
Name: Earth Period: 365.26 days Inclination: 0.00 Eccentricity: 0.02
venus.txt is to copy earth.txt and edit it
$ cp earth.txt venus.txt$ edit venus.txt$ ls -tvenus.txt earth.txt
-t tells ls to list by modification time, instead of alphabeticallycat (short for "concatenate")$ cat venus.txtName: VenusPeriod: 224.70 daysInclination: 3.39Eccentricity: 0.01
ls -l ("-l" meaning "long form")$ ls -ltotal 2-rwxr-xr-x 1 gvwilson bmi280 73 Jan 4 15:58 earth.txt-rwxr-xr-x 1 gvwilson bmi280 73 Jan 4 15:58 venus.txt
wc (for "word count")$ wc earth.txt venus.txt4 9 73 earth.txt4 9 73 venus.txt8 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. |
| Table 3.1: Basic Command-Line Tools | |||
|---|---|---|---|
Exercise 3.1:
ls shows you this:
Makefile biography.txt data enrolment.txt programs thesis
thesis programs enrolment.txt data biography.txt Makefile
Exercise 3.2:
cd ~ do? What about cd ~hpotter?Exercise 3.3:
Exercise 3.4:
pushd, popd,
and dirs do? Where do their names come from?Exercise 3.5:
earth.txt to the
default printer? How would you check it made it (other than
wandering over to the printer and standing there)?Exercise 3.6:
Exercise 3.7:
diff finds and displays the differences between
two text files. For example, if you modify earth.txt to
create a new file earth2.txt that contains:
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.txt3c3< Inclination: 0.00---> Inclination: 0.00 degrees4a5> 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.)
diff to tell it to
ignore changes that just insert or delete blank lines? What if
you want to ignore changes in case (i.e., treat lowercase and
uppercase letters as the same)?* matches zero or more charactersls bio/*.txt lists all the text files in the bio directory$ ls bio/*.txtbio/albus.txt bio/ginny.txt bio/harry.txt bio/hermione.txt bio/ron.txt
? matches any single characterls jan-??.txt lists text files whose names start with “jan-” followed by two charactersls jan-??.* doesls can't tell whether it was invoked as ls *.txt or as ls earth.txt venus.txtta* does not find the tabulate command
Figure 4.1: Redirecting Standard Input and Output
command < input_file reads from input_file instead of from the keyboardcommand > output_file writes to output_file instead of to the screencommand < input_file > output_file does bothwords.len:
$ cd bio$ wc *.txt > words.len
words.lencat$ cat words.len7 66 468 albus.txt5 46 311 ginny.txt5 49 342 harry.txt5 49 331 hermione.txt6 54 364 ron.txt28 264 1816 total
cat > junk.txt
cat reads from the keyboardrm junk.txt to get rid of the file
rm * unless you're really, really, REALLY sure that's what you want to do...sort words >words
sort then goes and reads the empty filewords are lostwc -w *.txt to count the words in some files, then sort -n to sort numerically$ wc -w *.txt > words.tmp$ sort -n words.tmp46 ginny.txt49 harry.txt49 hermione.txt54 ron.txt66 albus.txt264 total$ rm words.tmp
"|"
Figure 4.2: Pipes
$ wc -w *.txt | sort -n46 ginny.txt49 harry.txt49 hermione.txt54 ron.txt66 albus.txt264 total
$ grep 'Title' spells.txt | sort | uniq -c | sort -n -r | head -10 > popular_spells.txt
set at the command prompt to get a listing:$ setBASH=/usr/bin/bashBASH_VERSION='2.05b.0(1)-release'COLUMNS=120HISTFILE=/home/.bash_historyHISTFILESIZE=500HISTSIZE=500HOME=/home/rweasleyHOSTNAME=hogwartsHOSTTYPE=x86_64LINES=60NUMBER_OF_PROCESSORS=1OSTYPE=linux-gnuPATH='/usr/local/bin:/usr/bin:/bin:/Python24:/home/rweasley/bin'PWD=/home/rweasleySHELL=/bin/bashUID=1003USER=rweasley
"$" in front of its namels $HOME is the same as ls /home/rweasley (if you're Ron Weasley)echo command to print out a variable's value$ echo $HOME/home/socr/a/tef
echo $HOME, and not just $HOME?| Name | Typical Value | Notes |
|---|---|---|
COLUMNS | 80 | The width in characters of the current display window |
EDITOR | /bin/edit | Preferred editor |
HOME | /home/rweasley | The current user's home directory |
HOSTNAME | "ishad" | This computer's name |
HOSTTYPE | "i686" | What kind of computer this is |
LINES | 60 | The height in characters of the current display |
OS | "Windows_NT" | What operating system is running |
PATH | "/home/rweasley/bin:/usr/local/bin:/usr/bin:/bin:/Python24/" | Where to look for programs |
PWD | /home/rweasley/swc/lec | Present working directory (sometimes CWD, for current working directory) |
SHELL | /bin/bash | What shell is being run |
TEMP | /tmp | Where to store temporary files |
USER | "rweasley" | The current user's ID |
| Table 4.1: Important Environment Variables | ||
Figure 4.3: Setting a Variable Without Export It
$ VILLAIN="Lord Voldemort"$ VILLAIN="Lord Voldemort"$ bash$ echo $VILLAIN$ exit
Figure 4.4: Exporting a Variable's Value
$ VILLAIN="Lord Voldemort"$ export VILLAIN$ bash$ echo $VILLAINLord Voldemort$ exit
$ export VILLAIN="Lord Voldemort"$ bash$ echo $VILLAINLord Voldemort$ exit
~/.bashrc"~" is a shortcut meaning "your home directory"# 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"
.bashrc files can become very complex...ls won't show themPATH environment variables defines the shell's search pathbroom, the shell:$PATH into components to get a list of directoriesPATH is /home/rweasley/bin:/usr/local/bin:/usr/bin:/bin:/Python24/usr/local/bin/broom and /home/rweasley/bin/broom exist/home/rweasley/bin/broom will be run when you type broom at the command prompt/bin, /usr/bin: core tools like ls/usr/local/bin: optional (but common) tools, like the gcc C compiler$HOME/bin: tools you have built for yourself$HOME is your home directory. (the current working directory) in your pathwhatever, instead of ./whatevergroups command will show you which ones you are inls -l shows this informationrwx triples"-"rw-rw-r-- means:tools has permission rwx--x--x, then:ls tools, permission is deniedtools/pfoldchmodchmod u+x broom allows broom's owner to run itchmod o-r notes.txt takes away the world's read permission for notes.txtnojunk#!/usr/bin/bash rm -f *.junk
man rm to find out what the "-f" flag does#!/usr/bin/bash means "run this using the Bash shell"#!rwxr-xr-x./nojunk$HOME/bin is in your search path, move it theretest/usr/bin/test./trychmod | 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. |
| Table 4.2: Advanced Command-Line Tools | |
|---|---|
Exercise 4.1:
-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
data 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?Exercise 4.2:
a, and
have .txt as extension. What command would you use? For
example, if the directory contains three files a.txt,
abc.txt, and abcd.txt, the command should remove
abc.txt , but not the other two files.Exercise 4.3:
Exercise 4.4:
cd HOME
and cd $HOME?Exercise 4.5:
data directory that contain the word "carpentry". What
command or commands could you use?Exercise 4.6:
analyze. What
command or commands could you use to display the first ten lines of
its output? What would you use to display lines 50-100? To send
lines 50-100 to a file called tmp.txt?Exercise 4.7:
ls data > tmp.txt writes a listing of
the data directory's contents into tmp.txt. Anything
that was in the file before the command was run is overwritten. What
command could you use to append the listing to tmp.txt
instead?Exercise 4.8:
lectures directory?Exercise 4.9:
rm *.ch? What about rm
*.[ch]?Exercise 4.10:
bash are running?Exercise 4.11:
Exercise 4.12:
Exercise 4.13:
Exercise 4.14:
grep is one of the more useful tools in the
toolbox. It finds lines in files that match a pattern and
prints them out. For example, assume the files
earth.txt and venus.txt contain lines like
this:Name: Earth Period: 365.26 days Inclination: 0.00 Eccentricity: 0.02
grep can extract lines containing the text
"Period" from all the files:$ grep Period *.txtearth.txt:Period: 365.26 daysvenus.txt:Period: 224.70 days
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.grep? What if you wanted the line numbers of
matching lines?Exercise 4.15:
ls to sort its output by
filename extension, i.e., to list all .cmd files before
all .exe files, and all .exe's before all
.txt files. What command or commands would you
use?Exercise 4.16:
alias command do? When would
you use it?