Portions Copyright © 2005-06 Python Software Foundation.
A Shell in Action
sh, is an ancestor of many of them
bash(the Bourne Again Shell) in this course
A Directory Tree
.txtis associated with an editor, and
.htmlwith a web browser
/home/hpotteris Harry Potter's home directory
/courses/swc/wlec/shell.htmlis this file
/courses/swc, the relative path to this file is
"."(pronounced "dot") is the current directory
".."(pronounced "dot dot") is the directory one level up
pwd(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
$ 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
datadirectory, type "
$ ls data bio elements haiku.txt morse.txt pdb solarsystem.txt
cd data" to 'go into' directory
ls" on its own
cd .." to go back to where you started
$ 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, the OS:
lsproduce more informative output by giving it some flags
"-", as in
$ 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/
lsdoesn't show things whose names begin with "."
$ 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
-v(verbose) would tell it to print a confirmation message
earth.txtwith the following contents:
Name: Earth Period: 365.26 days Inclination: 0.00 Eccentricity: 0.02
venus.txtis to copy
earth.txtand edit it...
$ cp earth.txt venus.txt $ edit venus.txt $ ls -t venus.txt earth.txt
lsto list by modification time, instead of alphabetically
cat(short for "concatenate")
$ cat venus.txt Name: Venus Period: 224.70 days Inclination: 3.39 Eccentricity: 0.01
$ ls -l total 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.txt 4 9 73 earth.txt 4 9 73 venus.txt 8 18 146 total
|Documentation for commands.||Make directories.|
|Concatenate and display text files.||Page through a text file.|
|Change working directory.||Move (rename) files and directories.|
|Clear the screen.||Display the bytes in a file.|
|Copy files and directories.||Print current working directory.|
|Display the current date and time.||Remove files.|
|Show differences between two text files.||Remove directories.|
|Print arguments.||Sort lines.|
|Print lines matching a pattern.||Display the last few lines of a file.|
|Display the first few lines of a file.||Remove adjacent duplicate lines.|
|List files and directories.||Count lines, words, and characters in a file.|
lsshows you this:
Makefile biography.txt data enrolment.txt programs thesis
thesis programs enrolment.txt data biography.txt Makefile
cd ~do? What about
dirsdo? Where do their names come from?
earth.txtto the default printer? How would you check if made it (other than wandering over to the printer and standing there)?
difffinds and displays the differences between two text files. For example, if you modify
earth.txtto create a new file
You can then compare the two files like this:
Name: Earth Period: 365.26 days Inclination: 0.00 degrees Eccentricity: 0.02 Satellites: 1
(The rather cryptic header
$ diff earth.txt earth2.txt 3c3 < Inclination: 0.00 --- > Inclination: 0.00 degrees 4a5 > Satellites: 1
"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.)
diffto 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)?
"ls bio/*.txt"lists all the text files in the
$ ls bio/*.txt bio/albus.txt bio/ginny.txt bio/harry.txt bio/hermione.txt bio/ron.txt
"ls jan-??.txt"lists text files whose names start with "jan-" followed by two characters
lscan't tell whether it was invoked as
"ls *.txt"or as
"ls earth.txt venus.txt"
"ta*"does not find the
Redirecting stdin and stdout
command < input_file
input_fileinstead 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
output_fileinstead of to the screen. Only "normal" output goes to the file, not error messages.
command < input_file > output_file
$ 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
cat > junk.txt
catreads from the keyboard with output sent to a file
rm 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"
sortthen reads the empty file
wordsis permantly lost
wc -w *.txt" to count the words in some files, then "
sort -n" to sort numerically.
$ 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
setat the command prompt to get a listing:
$ set BASH=/usr/bin/bash BASH_VERSION='2.05b.0(1)-release' COLUMNS=120 HISTFILE=/home/.bash_history HISTFILESIZE=500 HISTSIZE=500 HOME=/home/rweasley HOSTNAME=hogwarts HOSTTYPE=x86_64 LINES=60 NUMBER_OF_PROCESSORS=1 OSTYPE=linux-gnu PATH='/usr/local/bin:/usr/bin:/bin:/python24:/home/rweasley/bin' PWD=/home/rweasley SHELL=/bin/bash UID=1003 USER=rweasley
"$"in front of its name:
ls $HOME" is the same as "
ls /home/rweasley" (if you're Ron Weasley)
echocommand to print out a variable's value
$ echo $HOME /home/socr/a/tef
echo $HOME", and not just "
|Preferred editor for use with svn command|
|The current user's home directory|
|This computer's name|
|The height in characters of the current display|
|Number of shell commands remembered in history|
|Where to look for programs|
|Present working directory (sometimes
|What shell is being run|
|The current user's ID|
Notice that values with spaces in them have to be quoted
$ VILLAIN="Lord Voldemort"
$ 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"
.bashrcfiles can become very complex...
lswon't show them
PATHenvironment variables defines the shell's search path
broom, the shell:
$PATHinto components to get a list of directories
/home/rweasley/bin/broomwill be run when you type
broomat the command prompt
/usr/bin: core tools like
/usr/local/bin: optional (but common) tools, like the
$HOME/bin: tools you have built for yourself
$HOMEis your home directory
.(the current working directory) in your path
whatever, instead of
groupscommand will show you which ones you are in
ls -lshows this information
ls tools, permission is denied
chmod u+x broom" allows
broom's owner to run it
chmod o-r notes.txt" takes away the world's read permission for
nojunk" that contains the following two lines:
#!/usr/bin/bash rm -f *.junk
man rm" to find out what the '-f' flag does
#!/usr/bin/bash" means 'interpret the contents of this file using the Bash shell'
$HOME/binis in your search path, move it there
|Change file and directory permissions.|
|Print the disk space used by files and directories.|
|Find files with names that match patterns, that are of a certain age or size, etc.|
|Print lines matching a pattern.|
|Uncompress a file.|
|Compress a file.|
|Send a file to a printer.|
|Remove a print job from a printer's queue.|
|Check the status of a printer's queue.|
|Display running processes.|
|Find the path to a program.|
|See who is logged in.|
|Execute a command for each line of input.|
According to the listing of the
-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
datadirectory above, who can read the file
earth.txt? Who can write it (i.e., change its contents or delete it)? When was
earth.txtlast changed? What command would you run to allow everyone to edit or delete the file?
a, and have
.txtas extension. What command would you use? For example, if the directory contains three files
abcd.txt, the command should remove
abc.txt, but not the other two files.
datadirectory that contain the word
"carpentry". What command or commands could you use?
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
ls data > tmp.txtwrites a listing of the
datadirectory'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
rm *.ch" do? What about "
lsto sort its output by filename extension, i.e., to list all
.cmdfiles before all
.exefiles, and all
.exe's before all
.txtfiles. What command or commands would you use?
aliascommand do? When would you use it?
grepis 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
venus.txtcontain lines like this:
Name: Earth Period: 365.26 days Inclination: 0.00 degrees Eccentricity: 0.02 Satellites: 1
grepcan extract lines containing the text
"Period"from all the files:
Search strings can use regular expressions, which will be discussed in a later lecture.
$ grep Period *.txt earth.txt:Period: 365.26 days venus.txt:Period: 224.70 days
greptakes many options as well; for example,
grep -c /bin/bash /etc/passwdreports 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
bashas 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
What if you wanted the line numbers of matching lines?