| prev | Draft Version 534 (Wed Oct 5 12:49:21 2005) | next |
$PATH is?rwxr-xr-x means?sh was the first for Unixbash (the Bourne again shell) in this courseCygwin)notes.txt or home.html.txt is associated with an editor, and .html with a web browser/
C:\home\gvwilson\notes.txt is different from J:\home\gvwilson\notes.txtC:\home\gvwilson as c:/home/gvwilson/cygdrive/c/home/gvwilson
":" a special meaning, so Cygwin needed a way to write paths without it…"/"/home/gvwilson is my home directory, and /courses/swc/lec/shell.swc is this file/courses/swc, the relative path to this file is lec/shell.swc"." means “the current directory”".." means “the directory immediately above this one/courses/swc/data, .. is /courses/swc/courses/swc/data/elements, .. is /courses/swc/datapwd (short for "print working directory”) to find out where I ampwd
/home/gvwilson/swc
ls (for “listing”) to see what's in the current directoryls
LICENSE.txt admin data graphics lec pdf scraps util Makefile cgi-bin etc img mp3 publ src web
ls is:
ls to produce more informative output by giving it some flags
"-", as in "-c" or "-l"ls -F
LICENSE.txt admin/ data/ exer/ lec/ publ/ soln/ util/ Makefile cgi-bin/ etc/ img/ pdf/ scraps/ src/ web/
.
ls -a
. .. .svn admin data exer img lec publ soln src util tmpl README.txt license.txt todo.txt
ls doesn't show anything whose name begins with ..svn directory: this is where Subversion keeps administrative informationmkdir temp
-v (“verbose”) flag tells mkdir to print a confirmation messagecd temp
pwd
/home/gvwilson/swc/temp
ls -a
. ..
earth.txt with the following contents:
Name: Earth Period: 365.26 days Inclination: 0.00 Eccentricity: 0.02
venus.txt is to copy the one we havecp earth.txt venus.txt
ls -t
venus.txt earth.txt
-t option tells ls to list newest firstcat (short for “concatenate”)
cat venus.txt
Name: Earth Period: 365.26 days Inclination: 0.00 Eccentricity: 0.02
Name: Venus Period: 224.70 days Inclination: 3.39 Eccentricity: 0.01
wc (for “word count”)
wc earth.txt venus.txt
4 9 69 earth.txt 4 9 69 venus.txt 8 18 138 total
* matches zero or more charactersls *.f77 lists all the Fortran-77 files in a directorywc *.txt
4 9 69 earth.txt 4 9 69 venus.txt 8 18 138 total
? matches any single characterls ??.txt lists all the text files with two-letter prefixesls ??.* lists all the files with two-letter prefixes, and any extension~ on its own means “my home directory”
~harry means “Harry's home directory”ls to know whether it was invoked as ls *.txt or rm earth.txt venus.txtcommand < inputFile reads from inputFile instead of from the keyboardcommand > outputFile writes to outputFile instead of to the screencommand < inputFile > outputFile does bothwords.len
$ wc -w *.txt > words.len
words.len$ ls -t
words.len venus.txt earth.txt
$ cat words.len
4 9 69 earth.txt 4 9 69 venus.txt 3 12 62 words.len 11 30 200 total
cat > junk.txt
cat reads from the keyboardrm junk.txt to get rid of the filerm * unless you're really, really sure that's what you want to do…wc -w *.* to count the words in some files, then sort -n to sort numericallywc *.txt > temp sort -n < temp
"|"wc -w *.* | sort -n
9 earth.txt 9 venus.txt 12 words.len 30 total
wc *.txt | sort -n | head -5 > shortest.files
set at the command prompt to get a listing:
$ set
ANT_HOME=C:/apache-ant-1.6.2 BASH=/usr/bin/bash COLUMNS=80 COMPUTERNAME=ISHAD HISTFILESIZE=500
"$" in front of its name"$HOME" with the current user's home directoryecho command to print this out$ echo $HOME
/home/gvwilson
echo $HOME, and not just $HOME?$ export VARNAME=value
~/.bashrc
"~" is a shortcut meaning “your home directory”
~/.bashrc is /home/gvwilson/.bashrc| Name | Typical Value | Notes |
|---|---|---|
HOME | /home/gvwilson | The current user's home directory |
HOMEDRIVE | C: | The current user's home drive (Windows only) |
HOSTNAME | "ishad" | This computer's name |
HOSTTYPE | "i686" | What kind of computer this is |
OS | "Windows_NT" | What operating system is running |
PATH | "/home/gvwilson/bin:/usr/local/bin:/usr/bin:/bin:/Python24/" | Where to look for programs |
PWD | /home/gvwilson/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 | "gvwilson" | The current user's ID |
PATH
tabulate, the shell:
$PATH on colons to get a list of directoriesPATH is /home/gvwilson/bin:/usr/local/bin:/usr/bin:/bin:/Python24/usr/local/bin/tabulate and /home/gvwilson/bin/tabulate exist/home/gvwilson/bin/tabulate will be run when you type tabulate at the command prompt. in your pathwhatever, instead of ./whateverwhich program_name, which will tell youPATH include:
/bin, /usr/bin: core tools like ls
/usr/local/bin: optional (but common) tools, like the gcc$HOME/bin: tools you have built for yourself$HOME means “the user's home directory”~/bin/cygdrive/c/somewhere instead of Windows' c:/somewhere
c:/somewhere would clash with the colons in the PATH variablec:/cygwin as the root of its file system/home/aturing is a synonym for c:/cygwin/home/aturingman | Documentation for commands. |
cat | Concatenate and display text files. |
cd | Change working directory. |
chmod | Change file and directory permissions. |
clear | Clear the screen. |
cp | Copy files and directories. |
date | Display the current date and time. |
diff | Show differences between two text files. |
echo | Print arguments. |
env | Show environment variables. |
head | Display the first few lines of a file. |
ls | List files and directories. |
mkdir | Make directories. |
more | Page through a text file. |
mv | Move (rename) files and directories. |
od | Display the bytes in a file. |
passwd | Change your password. |
pwd | Print current working directory. |
rm | Remove files. |
rmdir | Remove directories. |
sort | Sort lines. |
tail | Display the last few lines of a file. |
uniq | Remove duplicate lines. |
wc | Count lines, words, and characters in a file. |
which | locate a command |
groups command will show you which ones you are inls -l will show all of this informationrwx triples"-"rw-rw-r-- means “user and group can read and write; everyone else can read; no one can execute”chmod
chmod u+x something.exe gives the user execute permission to something.exechmod o-r notes.txt takes away the world's read permission for notes.txttools has permission rwx--x--x (i.e., owner can do anything, but everyone else only has execute permission), then:
ls tools, permission is deniedtools/findanswers, other users can still run itdu | Print the disk space used by files and directories. |
find | Find files that match a pattern. |
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. |
Exercise 4.1:
Suppose you are in your home directory, and ls shows
you this:
Makefile biography.txt data enrolment.txt programs thesis
What argument(s) do you have to give to ls to get it
to put a trailing slash after the names of subdirectories, like
this:
Makefile biography.txt data/ enrolment.txt programs/ thesis/
If you run ls data, it shows:
earth.txt jupiter.txt mars.txt mercury.txt saturn.txt venus.txt
What command should you run to get the following output:
data/earth.txt data/jupiter.txt data/mars.txt data/mercury.txt data/saturn.txt data/venus.txt
What if you want this (note that an extra entry is being displayed):
total 7 drwxr-xr-x 7 someone 0 May 6 08:27 .svn -rw-r--r-- 1 someone 2396 May 6 08:38 earth.txt -rw-r--r-- 1 someone 1263 May 6 08:38 jupiter.txt -rw-r--r-- 1 someone 1015 May 6 08:43 mars.txt -rw-r--r-- 1 someone 946 May 6 08:41 mercury.txt -rw-r--r-- 1 someone 1714 May 6 08:40 saturn.txt -rw-r--r-- 1 someone 881 May 6 08:40 venus.txt
Note: the command will display your user ID, rather than
someone. On some machines, the command will also display a
group ID. Ignore these differences for the purpose of this
question.
Exercise 4.2:
According to the listing of the data directory above, who
can read the file mercury.txt? Who can write it (i.e., change
its contents or delete it)? When was mercury.txt last
changed? What command would you run to allow everyone to edit or
delete the file?
Exercise 4.3:
Suppose you want to remove all files whose names (not including
their extensions) are of length 3, start with the letter 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.4:
What does the command cd ~ do? What about cd
~gvwilson?
Exercise 4.5:
What's the difference between the commands cd HOME
and cd $HOME?
Exercise 4.6:
Suppose you want to list the names of all the text files in the
data directory that contain the word "carpentry". What
command or commands could you use?
Exercise 4.7:
Suppose you have written a program called 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.8:
The command 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.9:
What command(s) would you use to find out how many
subdirectories there are in the lectures directory?
Exercise 4.10:
What does rm *.ch? What about rm
*.[ch]?
Exercise 4.11:
What command(s) could you use to find out how many instances of
a program are running on your computer at once? For example, if you
are on Windows, what would you do to find out how many instances of
svchost.exe are running? On Unix, what would you do to
find out how many instances of bash are running?
Exercise 4.12:
What do the commands pushd, popd,
and dirs do? Where do their names come from?
Exercise 4.13:
How would you send the file earth.txt to the
default printer? How would you check it made it (other than
wandering over to the printer and standing there)?
Exercise 4.14:
A colleague asks for your data files. How would you archive them to send as one file? How could you compress them?
Exercise 4.15:
The instructor wants you to use a hitherto unknown command for manipulating files. How would you get help on this command?
Exercise 4.16:
You're worried your data files can be read by your nemesis, Dr. Evil. How would you check whether or not he can, and if necessary change permissions so only you can read or write the files?
Exercise 4.17:
You have changed a text file on your home PC, and mailed it to the university terminal. What steps can you take to see what changes you may have made, compared with a master copy in your home directory?
Exercise 4.18:
How would you change your password?
Exercise 4.19:
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 I have files
earth.txt and venus.txt containing lines like
this:
Name: Earth Period: 365.26 days Inclination: 0.00 Eccentricity: 0.02
If I type grep Period *.txt in that
directory, I get:
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?
Exercise 4.20:
diff finds and displays the differences between
two files. It works best if both files are plain text (i.e.,
not images or Excel spreadsheets). By default, it shows the
differences in groups, like this:
3c3,4 < Inclination: 0.00 --- > Inclination: 0.00 degrees > Satellites: 1
(The rather cryptic header "3c3,4" means that line 3
of the first file must be changed to get lines 3-4 of the
second.)
What flag(s) should you give 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)?
Exercise 4.21:
Suppose you wanted 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?
| prev | Copyright © 2005, Python Software Foundation. See License for details. | next |