prev | Version 1130 (Mon Nov 27 20:45:56 2006) | next |
stdin
and stdout
are$PATH
is-rwxr-xr-x
means*
matches zero or more charactersls bio/*.txt
lists all the text files in the bio
directory$ ls bio/*.txt
bio/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.txt
ta*
does not find the tabulate
commandcommand < 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 bothFigure 4.1: Redirecting Standard Input and Output
words.len
:$ cd bio
$ wc *.txt > words.len
words.len
cat
$ 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
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…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.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
Figure 4.2: Pipes
$ grep 'Title' spells.txt | sort | uniq -c | sort -n -r | head -10 > popular_spells.txt
set
at 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=i686
LINES=60
NUMBER_OF_PROCESSORS=1
OSTYPE=cygwin
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 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
/cygdrive/c/home/rweasley
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 |
HOMEDRIVE | C: | The current user's home drive (Windows only) |
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 |
$ VILLAIN="Lord Voldemort"
$ VILLAIN="Lord Voldemort"
$ bash
$ echo $VILLAIN
$ exit
Figure 4.3: Setting a Variable Without Export It
$ VILLAIN="Lord Voldemort"
$ export VILLAIN
$ bash
$ echo $VILLAIN
Lord Voldemort
$ exit
Figure 4.4: Exporting a Variable's Value
$ export VILLAIN="Lord Voldemort"
$ bash
$ echo $VILLAIN
Lord 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 ./whatever
Cygwin
does things a little differently/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/rweasley
is a synonym for C:/cygwin/home/rweasley
groups
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/pfold
chmod
chmod u+x broom
allows broom
's owner to run itchmod o-r notes.txt
takes away the world's read permission for notes.txt
nojunk
#!/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
./try
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. |
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
According to the listing of the 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:
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.3:
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.4:
What's the difference between the commands cd HOME
and cd $HOME
?
Exercise 4.5:
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.6:
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.7:
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.8:
What command(s) would you use to find out how many
subdirectories there are in the lectures
directory?
Exercise 4.9:
What does rm *.ch
? What about rm
*.[ch]
?
Exercise 4.10:
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.11:
A colleague asks for your data files. How would you archive them to send as one file? How could you compress them?
Exercise 4.12:
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.13:
How would you change your password?
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 *.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?
Exercise 4.15:
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?
Exercise 4.16:
What does the alias
command do? When would
you use it?
prev | Copyright © 2005-06 Python Software Foundation. | next |