Appendix A. Unix Reference

In this appendix, you’ll find a fairly thorough reference on Unix commands and flags as well as examples and descriptions of each. We organized this appendix to generally parallel the book, so that you can easily reference key commands and related flags without being overwhelmed with long lists of commands.

Table A.a summarizes what you’ll find in this appendix.

Table A.a. Summary of Appendix Tables
TABLE NUMBERDESCRIPTION
Table A.1Getting Started with Unix
Table A.2Using Directories and Files
Table A.3Working with Your Shell
Table A.4Creating and Editing Files
Table A.5Controlling File Ownership and Permissions
Table A.6Manipulating Files
Table A.7Getting Information About Your System
Table A.8Configuring Your Unix Environment
Table A.9Running Scripts and Programs
Table A.10Writing Basic Scripts
Table A.11Sending and Reading Email
Table A.12Accessing the Internet
Table A.13Working with Encoded and Compressed Files
Table A.14Installing Software
Table A.15Using Handy Utilities

Tables A.1A.15 contain commands and flags that relate to the topics covered by the similarly numbered chapter (Chapters 16 and 17 do not introduce many new commands, so the commands from those chapters are included with similar commands in the other appendixes). In addition to the commands and flags discussed in the chapters, you’ll also find related commands and options that you might find useful in your Unix adventures, reference information that will jog your memory, and ideas to help you get off and running on additional projects. If you’re looking for a thorough command flag reference, check out Appendix C.

Table A.1. Getting Started with Unix: Survival Skills
COMMANDDESCRIPTION
apropos keywordFind appropriate man pages for keyword.
cat fileDisplay file contents onscreen or provide file contents to standard output.
cat file1 file2Display file1 and file2.
cdReturn to your home directory from anywhere in the Unix system.
cd ..Move up one level in the directory tree.
cd /etcChange to the /etc directory relative to the system root.
cd ~/subdirUse a tilde (~) as a handy shortcut for your home directory.
cd ProjectsMove to the Projects directory relative to the current directory.
col -bFilter backspaces and reverse line feeds out of input. Use to make man pages editable without odd formatting.
Close your current process (often a shell) and your Unix session if you close the login shell.
exitClose your current shell and your Unix session if you’re in the login shell.
less fileUse to view file screen by screen.
logoutClose your Unix session.
lsList files and directories.
ls /List the files and directories in the root directory.
ls directoryList the files and directories in directory.
ls -aList all files and directories, including hidden ones.
ls -cList files and directories by modification date.
ls -lList files and directories in long format, with extra information.
ls -lhList files and directories in long format, with extra information and human readable sizes.
man 5 commandView the specified section (5) of the man pages for command. Sometimes used as man -s 5 command.
man commandView the manual (help) pages for command.
man -k keywordFind appropriate man pages for keyword.
more filetoviewView filetoview screen by screen.
passwdChange your password.
pwdDisplay the path and name of the directory you are currently in.
resetReset the shell to fix display problems.
stty saneTry to fix unexpected, sudden, and strange display problems.
su - youridRelog in without having to log out.
suBecome the root user.
sudo commandRun command with the authority of the root user.

Table A.2. Using Directories and Files
COMMANDDESCRIPTION
cp existingfile newfileCopy existingfile to a file named newfile.
cp -i existingfile oldfileCopy existingfile to a file named newfile, prompting you before overwriting existing files.
cp -r /Projects /shared/ProjectsCopy the directory /Projects to the new name /shared/Projects, specifying recursive copy.
find . -name lostfile -printFind a file or directory in the current directory or subdirectories named lostfile.
find /home -name "pending*" -printFind all files or directories with names starting with “pending” in the home directory or subdirectories.
find /home/shared -mtime -3 -printFind all files or directories in the shared directory that were modified within the past three days.
find ~ -name '*.backup' -exec compress {} ;Compress all files in the home directory with names containing “.backup,” without confirmation.
find ~ -name '*.backup' -ok rm {} ;Find and remove, with confirmation, all files in the home directory whose names end with “.backup”.
ln /home/a /home/bHard link all of the files in the a directory to the files in the b directory.
ln afile alinkLink afile and alink, making the same file essentially exist in two different directories.
ln -s /home/deb/Projects /home/helper/ProjectCreate a soft link from /home/deb/Projects to /home/helper/Project.
locate stringLocate files with string in their names.
mkdir NewdirectoryMake a new directory named Newdirectory.
mv existingfile newfileRename existingfile to newfile.
mv -i oldfile newfileRename oldfile to newfile, requiring the system to prompt you before overwriting (destroying) existing files.
rm badfileRemove badfile.
rm -i *Delete interactively, with prompting before deletion. Good for files with problematic names that Unix thinks are command flags.
rm -i badfileRemove badfile interactively.
rm -ir dan*Interactively remove all the directories or files that start with “dan” in the current directory and all of the files and subdirectories in the subdirectories starting with “dan”.
rmdir YourdirectoryRemove the empty directory Yourdirectory.
touch newfileCreate a file named newfile with no content.
touch -t 200112312359 oldfileUpdate file date for oldfile to December 31, 23 hours, and 59 minutes in 2001.
which commandFind out the full path to command. This is valuable for seeing which of multiple commands with the same name would be executed.
whereis fileFind out the full path to file and related files.

Table A.3. Working with Your Shell
COMMANDDESCRIPTION
!10Rerun command 10 from the history list in csh or zsh.
bashStart a bash subshell or run a bash script.
chshChange your shell.
cshStart a csh (C) subshell or run a csh shell script.
echo $SHELLDisplay the value of the $SHELL environment variable.
exitLeave the current shell and return to the previous one, or log out of the login shell.
historyView a numbered list of previous commands.
kshStart a ksh (Korn) subshell or run a ksh shell script.
r 2Repeat a specific command in ksh from the history output (in this case command 2).
set -o emacsEnable command completion with emacs commands in the ksh shell.
set -o viEnable command completion with vi commands in the ksh shell.
shStart a sh (Bourne) subshell or run a sh shell script.
stty erase '^?'Make erase characters to the left of the cursor.
stty erase '^H'Make erase characters to the left of the cursor. Type stty erase then press .
su - youridStart a new login shell as yourid.
su userSwitch user to user.
tcshStart a tcsh subshell or run a tcsh shell script.
zshStart a zsh subshell or run a zsh shell script.

Table A.4. Creating and Editing Files
COMMANDDESCRIPTION
edChoose a line-oriented text editor.
emacsChoose a tremendously powerful, somewhat easy to use text editor.
emacs -nOpen emacs and force a terminal-window- (not graphical window-) oriented session.
emacs filenameOpen emacs and edit filename.
joeChoose a fairly friendly editor.
picoChoose for menu-oriented, user-friendly text editing.
pico filenameOpen and edit filename in pico.
pico -w filenameDisable word wrapping for filename in pico. This is particularly useful for configuration files.
viChoose a powerful editor with lots of power but little ease of use.
vi filenameOpen and edit filename in vi.

Table A.5. Controlling File Ownership and Permissions
COMMANDDESCRIPTION
chgrpChange the group association of files or directories.
chgrp groupname filenameChange the group association of filename to groupname.
chgrp -R group directoryRecursively change the group association of directory and all subdirectories and files within it to group.
chmodChange the permissions for a file or directory.
chmod a-w fileRemove write permission for file for all (everyone).
chmod g+w fileAdd write permission for file for the owning group.
chmod -R go-rwx *Revoke all permissions from everyone except the user for all files in the current directory and all subdirectories and their contents.
chmod u=rwx, g=rx, o=r fileSet the permissions on file to user read, write, and execute, group read and write, and others read.
chmod ugo= *Revoke all permissions for everything in the current directory from everyone.
chownChange the ownership of files or directories.
chown -R user DirectoryRecursively change the ownership of Directory and all contents to user.
chown user fileChange the ownership of file to user.
umask 022Specify the default permissions for all created files.

Table A.6. Manipulating Files
COMMANDDESCRIPTION
awkManipulate a file as a database.
awk /CA/'{ print $2 $1 $7 }' fileSelect (and display) three fields in each record in file on lines that contain “CA”.
awk '{ print $1 }' fileSelect (and display) the first field in each record in file.
awk -f script.awk fileRun an awk command from a script called script.awk on file.
awk -F, '{ print $1 }' file > newfileSelect the first field in each record in file, specifying that a “,” separates fields, and redirect the output to newfile.
awk -F: '{ print $2 "" $1 "in" $7 }' fileSelect (and display) several fields and some text for each record in file, using a colon (:) as a field delimiter.
basenameRemove the path from a filename, leaving only the name proper. Good to use in scripts to display just a filename.
cmp newfile oldfileCompare newfile to oldfile.
cryptEncrypt or decrypt a password-protected file.
csplitDivide files based on line number or other characteristics.
diff -b newfile oldfileFind differences (ignoring white space) between newfile and oldfile.
diff Directory NewdirectoryFind differences between Directory and Newdirectory.
diff -i newfile oldfileFind differences (except in case) between newfile and oldfile.
diff -iBw file1 file2Find all differences between file1 and file2 except those involving blank lines, spaces, tabs, or lowercase/uppercase letters.
diff newfile oldfileFind the differences between newfile and oldfile.
diff -w newfile oldfileFind differences (ignoring spaces) between newfile and oldfile.
fmt fileReformat file so it has even lines and a nicer appearance.
fold -w 60 fileReformat file so no lines exceed a specified length (60 characters here).
grep expression fileFind expression in file and view the lines containing expression.
grep -c expression fileCount how many times expression appears in file.
grep -i expression fileFind all lines containing expression in file, using any capitalization (case-insensitive).
grep -n expression fileDisplay each found line and a line number.
grep 'Nantucket$' limerick*Find the lines in the limerick files that end with “Nantucket”.
grep -v expression fileFind all lines in file that do not contain expression.
grep '^[A-Z, a-z]' limerickFind all the lines in limerick that start with any letter, but not with a number or symbol.
grep '^[A-Z]' limerickFind all the lines in limerick that start with a capital letter.
grep '^There' limerick*Find all the lines in the limerick files that start with “There”.
grep -5 'word[1234]' fileFind word1, word2, word3, or word4 in file and view the surrounding five lines as well as the lines containing the words.
head -20 fileView the first 20 lines of file.
head fileView the first 10 lines of file.
pr fileReformat file for printing, complete with headers and footers.
pr –-columns=2 fileReformat file for printing, complete with headers and footers and two columns.
sdiff newfile oldfileView the differences between newfile and oldfile.
sdiff -s newfile oldfileView the differences between newfile and oldfile, without showing identical lines.
sedMake changes throughout a file according to command-line input or a sed script.
sed '/old/new/g' file.htm > file.htmSearch through file.htm and replace every occurrence of “old” with “new”.
sed -f script.sed file > file.new; → mv file.new fileRun the commands in script.sed, apply them to file, and replace file with the manipulated content.
split –b 500k fileSplit file into 500 KB chunks.
sort file | uniqSort file and send it to uniq to eliminate duplicates.
sort file > sortedfileSort the lines in file alphabetically and present the sorted results in sortedfile.
sort file1 | tee sorted | mail [email protected]Sort file1 and, with tee, send it both to the file sorted and to standard output, where it gets mailed to the boss.
sort file1 file2 | uniq -dSort file1 and file2 together and find all the lines that are duplicated.
sort file1 file2 file3 > bigfileSort and combine the contents of file1, file2, and file3 and put the sorted output in bigfile.
sort -n fileSort file numerically.
sort -t, +2 fileSort on the third (really) field in the comma-delimited file.
sort -t, fileSort fields in the comma-delimited file; the character following -t (, ) indicates the delimiter.
spell fileCheck the spelling of all words in file. Returns a list of possibly misspelled words.
tail -15 fileView the last 15 lines of file.
tail fileView the last 10 lines of file.
tidy file.htmlClean file.html to make it “good” HTML, and optionally also easier to read and maintain.
tr A-Za-z a-zA-Z < fileChange uppercase to lowercase and lowercase to uppercase.
uniqUse with sorted files to eliminate duplicate lines.
wc -b fileCount the bytes in file.
wc fileCount the lines, words, and bytes in file.
wc -l fileCount the lines in file.
wc -w fileCount the words in file.

Table A.7. Getting Information About the System
COMMANDDESCRIPTION
dfSee what file systems are mounted where, and how much space is used and available.
df /usr/local/srcFind out where /usr/local/src is mounted and how much space is available on it.
df -k /homeView the file system for /home with the usage reported in 1 KB, not 512-byte, blocks.
df -h /homeView the file system for /home with the usage reported in human-readable terms.
duGet information about disk usage in the current directory as well as in all subdirectories.
du /homeGet information about disk usage in the /home directory.
du -kGet information about disk usage, measured in 1 KB blocks.
du -hGet information about disk usage, displayed in human-readable terms.
file /usr/bin/picoFind out the file type of /usr/bin/pico.
fingerSee who else is logged into the system and get a little information about them.
finger @stc.orgFind out who is logged into the stc.org system.
finger ejrGet information about user ejr on your system.
finger [email protected]Get information about user [email protected].
idFind out the numeric value of your userid and what groups (by name and numeric userid value) you belong to.
id otheruserCheck someone else’s status to find out what groups they’re in.
quotaFind out if you’re over quota.
quota -vView your current quota settings and space usage.
unameUse to find out what kind of Unix system
uname -aPrint all system information, including the Unix system type, host name, version, and hardware.
uname -srFind both the system type and release level.
watchMonitor a file or other data for changes.
wGet information about other users on the system and what
whoGet information about the other users on the system.
whoamiFind out what userid you’re currently logged in as.

Table A.8. Configuring Your Unix Environment
COMMANDDESCRIPTION
alias ourterm="longhonking
 → command -w -many –flags
 → arguments"

Create the alias ourterm to substitute for the command longhonking command -w-many -flags arguments.
setFind out what environment variables are set and their current values in zsh, bash, and ksh.
set VARIABLE="long value"Use in csh to set the value of VARIABLE with spaces or special characters in it.
set VARIABLE=valueUse in csh to set VARIABLE to value.
setenvUse in csh to find out what environment variables are set and their current values.
setenv VARIABLE valueUse in csh to make the VARIABLE available to other scripts in the current shell.
VARIABLE="long value"Use in zsh, bash, and ksh to set the value of VARIABLE with spaces or special characters in it.
VARIABLE=valueUse in zsh, bash, and ksh to set the VARIABLE to value.
export VARIABLEUse in zsh, bash, and ksh to make the value of VARIABLE available to other scripts.

Table A.9. Running Scripts and Programs
COMMANDDESCRIPTION
at 01:01 1 Jan 2004Schedule a job or jobs to run at 01:01 on January 1, 2004.
at 01/01/04Schedule a job to run on 1/1/04.
at 3:42amSchedule a job to run at 3:42 a.m.
at noon tomorrowSchedule a job to run at noon tomorrow.
at now + 3 weeksSchedule a job to run in three weeks.
at teatimeSchedule a job to run at 4 p.m.
atqReview jobs in the at queue.
atrm 3Remove the specified queued job (3, in this case).
batchSchedule jobs to run when system load permits.
bgRun the most recently suspended or controlled job in the background.
bg %2Run job 2 in the background.
crontab -eEdit your crontab in the default editor to schedule regular processes or jobs.
Suspend a running job, program, or process.
fgRun the most recently suspended or controlled job in the foreground.
fg 1Run job 1 in the foreground.
jobsSee a list of the currently controlled jobs.
kill %ftpKill a job by name or job number.
kill 16217Kill process number 16217.
kill -9 16217Kill process 16217; the -9 flag lets you kill processes that a regular kill won’t affect.
niceRun a job “nicely”—slower and with less of an impact on the system and other users.
 Bigger numbers are nicer, up to 19. 10 is the default.
nice -n 19 slowscriptRun slowscript nicely with a priority of 19.
pkill badjobKill the process called badjob.
psView the list of current processes that you’re running
ps -eView all processes, including those from other users.
ps -fView processes and their interrelationships (the forest view).
ps -xView the processes that the system itself is running (also called daemons).
renice 19 processid-of-slowscript
									

Run slowscript more nicely (change the niceness) with a priority of 19.
time scriptTime how long it takes (in real time and system time) to run script.
topMonitor system load and processes in real time.

Table A.10. Writing Basic Scripts
COMMANDDESCRIPTION
breakUse in a shell script to skip the rest of the commands in the loop and restart at the beginning of the loop.
case ... in ... esacUse in a shell script to perform separate actions for a variety of cases.
clearClear the screen.
continue 
echoDisplay a statement or the value of an environment variable onscreen.
echo "Your shell is $SHELL"Display “Your shell is” and the name of your shell onscreen.
echo -e " A Tab Stop"Move one tab stop to the right and print “A Tab Stop” on the screen.
for ... do ... doneUse in a shell script with conditions and commands to specify a loop to occur repeatedly.
getoptsUse in a shell script to read flags from the command line.
if ... then ... else ... fiUse in a script (with conditions and commands) to set a conditional process.
read variableUse in a script to get input (the variable) from the terminal.
sh -x scriptExecute script and require the script to display each command line as it is executed.
sleep 4h5m25sPause for 4 hours, 5 minutes, and 25 seconds here.
sleep 5sPause for 5 seconds.
testUse in a script to check to see if a given statement is true.
test expressionSee if expression is true or false—usually used with conditional statements.
while ... do ... doneUse in a shell script to perform a loop only while the condition is true.

Table A.11. Sending and Reading Email
COMMANDDESCRIPTION
elmStart the elm mail program and read, respond to, or send email.
elm [email protected]Start a new elm mail message to [email protected].
elm [email protected],[email protected]Start a new elm mail message to [email protected] and [email protected].
mailStart the mail program. (Use pine or mutt rather than mail if possible.)
mail [email protected] < fileSend file to [email protected].
mail [email protected] -s "For you!" < fileSend file to [email protected] with the subject "For you!".
mail [email protected]Start a simple mail message to [email protected].
mail [email protected],[email protected]Start a simple mail message to [email protected] and [email protected].
muttStart the mutt mail program and read, respond to, or send email.
mutt [email protected]Start a new mutt mail message to [email protected].
mutt [email protected] -a file.tgzStart a new mutt mail message to [email protected] and attach file.tgz.
mutt [email protected],[email protected]Start a new mutt mail message to [email protected] and [email protected].
pineStart the pine mail program and read, respond to, or send email, or to read Usenet newsgroups.
pine [email protected],[email protected]Start a pine mail message to [email protected] and [email protected].
pine [email protected]Start a pine mail message to [email protected].
procmailFilter and sort mail according to a “recipe.” Run from the .forward file or automatically by the system.
vacationInitialize vacation and edit the message template.
vacation -IStart vacation and tell it to respond to incoming messages.
vacation -jStart vacation and automatically respond to all messages.

Table A.12. Accessing the Internet
COMMANDDESCRIPTION
dig @nameserver.some.net www.raycomm.comLook up the name www.raycomm.com from the name server nameserver.some.net.
dig –x 192.168.12.52Look up the name corresponding to the IP number 192.168.12.52.
ftp ftp.raycomm.comTransfer files to or from ftp.raycomm.com using the FTP protocol.
irc wazoo irc.netcom.comConnect to the irc server at irc.netcom.com and use the nickname wazoo.
linksStart the links Web browser.
links http://www.google.com/Start the links Web browser at http://www.google.com/.
lynx -dump http://url.com > newname.txtGet a spiffy plain text file named newname.txt out of an HTML document from http://url.com.
lynxStart the lynx Web browser.
lynx http://www.yahoo.com/Start the lynx Web browser on http://www.yahoo.com/.
mesg nRefuse talk and write messages.
mesg yAccept talk and write messages.
nnRead Usenet news.
nslookup www.raycomm.comnameserver.some.netLook up the name www.raycomm.com from the name server nameserver.some.net.
nslookup www.raycomm.comLook up the IP number for the host www.raycomm.com.
ping www.raycomm.comTest the connection to the host www.raycomm.com.
rnRead Usenet news.
ssh somewhere.comSecurely connect to and use a computer on the Internet named somewhere.com.
slrnRead Usenet news.
talk debTalk interactively with the owner of the ID deb.
talk [email protected]Talk interactively with a user id the system wherever.com.
telnet somewhere.comConnect to and use a computer on the Internet named somewhere.com.
tinRead Usenet news.
tin comp.unix.userfriendlyRead Usenet news from the comp.unix.userfriendly group.
tn3270 library.wherever.eduConnect to a host computer named library.wherever.edu that uses an IBM-mainframe-type operating system, like many library card catalogs.
traceroute www.yahoo.comIdentify the computers and other devices between you and the host www.yahoo.com.
traceroute -n hostnameCheck the path to hostname without resolving the intervening host names for faster results.
trnRead Usenet news.
trn comp.unix.shellRead Usenet news from the comp.unix.shell group.
wallSend a write-type message to all users on the system.
wget http://www.example.com/Download the file found at http://www.example.com/.
wget –r –l 2 http://www.example.com/Download the files found at http://www.example.com/ for two levels down in the Web structure.
write otheridSend a message to the user otherid on the same system.

Table A.13. Working with Encoded and Compressed Files
COMMANDDESCRIPTION
compress -c file.tar > file.tar.ZCompress file.tar under the same name with a .Z ending while retaining the original file.
compress file.tarCompress file.tar. The named file will be replaced with a file of same name ending with .Z.
gunzip archive.tar.gzUncompress (un-gzip) archive.tar.gz. Including .gz on the end of the filename is optional.
gzip archive.tarGzip (compress) archive.tar. The zipped file will replace the unzipped version and will have a new .gz extension
gzip -c filetogzip > compressed.gzGzip filetogzip and keep a copy of the original, unzipped file.
gzip -dUncompress (un-gzip) a file. Including .gz on the end of the filename is optional.
tar -cf newfile.tar DirectoryCreate a new tar archive containing all of the files and directories in Directory.
tar -czf newfile.tgz DirectoryCreate a new gzipped tar archive containing all of the files and directories in Directory.
tar -vAdd the -v flag to tar for a verbose description of what is happening.
tar -xf archive.tar "*file*"Extract the files with names containing “file” from the tar archive.
tar -xf archive.tarExtract the contents of archive.tar.
tar -xzf archive.tgzUncompress and extract the contents of archive.tgz.
uncompress archive.tar.ZUncompress archive.tar.Z, resulting in a file of the same name but without the .Z ending.
uncompress -c archive.tar.Z > archive.tarUncompress archive.tar.Z and retain the original file.
unzip zippedUnzip zipped without specifying the extension.
uudecode file.uueUudecode file.uue.
uuencode afile.jpg a.jpg > tosend.uueUuencode afile.jpg and a.jpg and save the encoded output as tosent.uue.
uuencode -mUse uuencode with the -m flag to specify base64 encoding, if your version of uuencode supports it.
gzcat archive.gz | moreUncompress (on the fly without deleting the original) archive.gz to read the contents.
zip zipped fileCreate a new zip file named zipped from file.
yencode fileCreate a new yencoded file from file.

Table A.14. Installing Software
COMMANDDESCRIPTION
makeSet up, link, and compile new programs.
make cleanClear out the garbage from a messed-up installation before you try again.
make installComplete installation of new programs.

Table A.15. Using Handy Utilities
COMMANDDESCRIPTION
bcUse a calculator to add, subtract, multiply, divide, and more.
bc bcfileDo the calculations specified in bcfile, then more calculations from the command line.
exprEvaluate mathematical or logical expressions.
calView the current month’s calendar.
cal 12 1941View the calendar for December 1941.
cal 1999View the calendar for 1999.
cal -jView the Julian calendar.
calendarView reminders for the current date, read from the file ~/calendar.
fortuneDisplay a fortune, saying, quotation, or whatever happens to come up.
ispell gudspelerInteractively spell-check the gudspeler file.
lookLook up a word in the system dictionary.
lpPrint a file.
rsync file backupfileRemotely synchronize (copy) file to backupfile.
scriptRecord your actions in a file called typescript in the current directory.
script covermybuttRecord your actions in the file covermybutt.
unitsConvert from one kind of unit to another.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.108.168