Shell is a command interpretr.
2. What is kernal?
Kernal is medium between shell and hardware which interprets shell result to kernal or vice versa.
3. What are the different types of shells?
1. Bourne shell
2. C shell
3. Korn Shell
Almost all the commands work for each shell but very few commands doens't work.
4. How to create multiple files?
Using touch command.
touch file1 file2 file3 ... filen
touch command is also used to change the time stamp of an existing files to current system time.
5. how to find unix version?
uname -a
6. How to find user id and group id?.
id
7. Hot to check which shell you are using?.
echo $SHELL
SHELL is a system variable. So, we can find using above command.
8. How to change unix shell?
chsh
9. How to set environment variables for different shells?
BASH shell:
Variable_Name = Value
export variable_name
C Shell:
setenv variable_name value
Korn shell:
export variable_name = value
10. What is .profile?.
.profile is a file which is executed during startup of the session.
All the variables set in .profile are set when the session is started.
11. What is first line in the shell script?
Shell executable location.
Ex:
#! /bin/csh
12. How to execute the shell script?.
./shell_script_name
Ex:
./file.sh
. - current directory
13. How to display all enviroment variables?.
env
14. How to change file permissions?.
chmod 777 filename
weightage:
r - 4
w - 2
x - 1
15. what is umask?
umask is used to change default file permissions.
ex:
umask 422
After executing above command when ever file is created file contains 244 permissions.
Directory contains 355 permissions.
For file - 666 - 422
For directory - 777 - 422
16. How to find disk usage?
df -lPk
17. How to search for a pattern?.
Using grep command we can search for patterns. grep stands for 'globally search for regular expression and print it.
i - ignores case
w - exact pattern
c - count of matching pattern
v - returns pattern that do not match
n - precedes line number with pattern
l - lists file names that matches pattern
^pattern - beginning of pattern
pattern$ - ending of pattern
18. How to search for multiple patterns?.
grep -e "pattern1" "pattern2" filename
or
egrep "pattern1" "pattern2" filename
19. what is soft link?.
ln -s source target
soft link or symbolinc link is link that points to another file. If original file is deleted the target file becomes unusable.
If content is changed in one file the other file content also will be changed.
20. what is hard link?.
ln source target
hard link is a link that have physically located file. If one file is deleted or changed the other file will not be affected.
21. How to find users current logged in?.
finger -i
or
who -T
22. How to find memory and CPU utilization?.
top sar Those two commands are used for the CPU Utilization
23. How to find currently running processes?.
ps -ef
24. How to find processes running of user currently logged in?
ps -euf
or
ps -ef | grep "username"
25. How to display all user processes running?.
ps -eaf
26. What is daemon process?.
daemon process is a process that starts during start up of the server. User will not have control on these processes and will be terminated
when the system is shutdown.
Ex:
init process
27. How to terminate a process?.
kill -9 process_id
kill -15 process_id
9 and 15 are called unix signals.
signal 15 calls SIGTERM signal. It does graceful kill of a process. Before terminating process this signal clean up the processes and closing open files
related to the process id.
signal 9 call SIGKILL signal. It does forcible kill of a process. It doesn't clean up processes and closes opened files as signal 15 does.
28. What does signal 0 does?.
kill -0 process_id
signal 0 retruns success if process_id exists or 1 if process_id doesn't exist
Ex:
kill -0 process_id
echo $?
29. How to read file line by line?.
while read line
do
echo $line
done < filename 30. How to compress the files?. tar -cvf
c - create a new archive
v - verbose, provide information on what you are doing
f - the next argument is the name of the archive
gzip
31. How to uncompress the files?.
tar -xvf
x - extract
gunzip
32. How to view files with out extracting the tar file?.
tar -tvf
t - list the files
v - displays owner date and time like ls -l command
f - the next argument is the name of the archive
33. How to view help in unix?.
man command
34. How to find number of lines,words and characters in a file?.
wc -lwc filename
l - lines
w - words
c - characters
35. How to find string length?.
echo "string name" | wc -c
or
echo $string | wc -c
36. How to send output to null device which doesn't store output on disk?.
Redirecting output to /dev/null will not save output on disk.
ex:
cat filename > /dev/null
37. How to find difference between two files?.
diff file1 file2
38. What is sticky bit?
Sticky bit gives protection to the files in a directory. Other users cannot delete the files under the directory for which sticky bit is set.
But, other users can modify the data in the files which are in the directory for which sticky bit is set.
Syntax:
chmod u+t
Example:
drwxrwxrwt 2 build build 1024 Mar 5 04:48 ajay
In permissions for which t is seen at the end that means sticky bit is set for that directory.
39. How to list only files in the current directory?.
ls -l | grep '^-'
40. How to list only sub directories in the current directory?.
ls -l | grep '^d'
41. How to run the process in the back ground?.
By appending '&' symbol at the end of the command to execute we can run process in the back ground.
example:
cat file.txt &
Note: If we close the sessions processes running in the back ground using & will be terminated.
42. How to list jobs running in the back ground?.
jobs
43. what is nohup command?.
nohup command runs in the back ground even though we close the session.
By default output is stored in nohup.out
syntax:
nohup cat file.txt
44. what is batch command?.
Batch command is used to run the command whenever system is free.
syntax:
batch command
45. what is at command?.
at command is used to execute the job only ones at a specific time.
syntax:
at
46. what is crontab command?.
crontab commans is used to schedule jobs recurrinly.
syntax:
crontab -e
minute hour dayofmonth monthoftheweek dayoftheweek command
47. How to list the jobs scheduled?.
crontab -l
48. How to remove all the jobs in crontab?.
crontab -r
49. How to delete a specific job in crontab?.
crontab -e
Delete the line related to job scheduled
save and quit
50. How to schedule job to run for every 5 mins using crontab?.
crontab -e
0-59/5 * * * * command
51. How to remove files along with directory?.
rm -rf directory_name
r - recursively
f - forcibily
52. How to pass command line arguments?.
./file.sh one two three four ....
one two three four are called command line arguments
53. Important commands
$# - Total number of positional parameters or command line arguments
$* - display all command line arguments
$$ - PID of the current shell
$- - current shell settings
$? - Exit status of last executed command
$! - PID of the last back ground process
54. How to rename file?.
mv source target
55. How to copy file with out changin time stamp?.
cp -p
56. How many command line arguments can be passed to script?.
We can pass 'n' number of command line arguments
57. How to get command line arguments if more than 9?.
using 'SHIFT' command we read command line
ex:
shift 9
58. What is export command?.
Export command is used to export the variables to sub shells. Reverse is not possible.
Ex:
a = 10;
export a
sh
exho $a
59. how to redirect error output?.
cat file > file1.txt 2> error.txt
60. How to go to last line in vi editor?.
G
61. How to replace string in vi editor?
:s/source/target/g
62. How to change the owner in unix?.
chown
63. How to locate or find a file recursively?.
find . -name "filename"
64. Find files modified less than 7 days before?.
find . -type f -mtime -7
65. Find directories that are modified less than 7 days before?.
find . -type d -mtime -7
66. Find all that are modified less than 7 days before?.
find . -mtime -7
67. Find files that are modified exactly 7 days back?.
find . -mtime 7
68. Find files that are modifed over 7 days before?.
find . -mtime +7
69. Find files that are modfied 7 days before and delete them?.
find . -mtime +7 -exec rm -rf {}/;
70. Find files that are modified 12 hours before?.
find . -mmin +12
71. Find files greater than 1 MB?.
find . -size +1M
72. find files greater than 1 kb?.
find . -size +1k
73. How to view last portion of the file dynamically?.
tail -f filename
74. How to replace all occureces of pattern?.
sed 's/source/target/g' filename
75. Delete lines from m to n in a file?.
sed '1,10 d' filename
76. Print lines from m to n in a file?.
sed '20,30 p' filename
77. Delete empty line in unix?.
sed '/^$/d' filename
78. Change extension of the file?.
sed 's/\(.*\.\)c/\1cpp/' file name
79. print the second field using awk?.
awk '{print $2}' filename
yesterdaysdate=`head -1 $datefile
(sysdate -1) ?
=============================================================================
=============================================================================
=============================================================================
=============================================================================
UNIX Commands
Unix Command Summary
See the Unix tutorial for a leisurely, self-paced introduction on how to use the commands listed below. For more documentation on a command, consult a good book, or use the man pages. For example, for more information on grep, use the command man grep.
Contents
• cat --- for creating and displaying short files
• chmod --- change permissions
• cd --- change directory
• cp --- for copying files
• date --- display date
• echo --- echo argument
• ftp --- connect to a remote machine to download or upload files
• grep --- search file
• head --- display first part of file
• ls --- see what files you have
• lpr --- standard print command (see also print )
• more --- use to read files
• mkdir --- create directory
• mv --- for moving and renaming files
• ncftp --- especially good for downloading files via anonymous ftp.
• print --- custom print command (see also lpr )
• pwd --- find out what directory you are in
• rm --- remove a file
• rmdir --- remove directory
• rsh --- remote shell
• setenv --- set an environment variable
• sort --- sort file
• tail --- display last part of file
• tar --- create an archive, add or extract files
• telnet --- log in to another machine
• wc --- count characters, words, lines
________________________________________
cat
This is one of the most flexible Unix commands. We can use to create, view and concatenate files. For our first example we create a three-item English-Spanish dictionary in a file called "dict."
% cat >dict
red rojo
green verde
blue azul
%
% cat dict
red rojo
green verde
blue azul
%
If we wish to add text to an existing file we do this:
% cat >>dict
white blanco
black negro
%
Now suppose that we have another file tmp that looks like this:
% cat tmp
cat gato
dog perro
%
Then we can join dict and tmp like this:
% cat dict tmp >dict2
We could check the number of lines in the new file like this:
% wc -l dict2
8
The command wc counts things --- the number of characters, words, and line in a file.
________________________________________
chmod
This command is used to change the permissions of a file or directory. For example to make a file essay.001 readable by everyone, we do this:
% chmod a+r essay.001
To make a file, e.g., a shell script mycommand executable, we do this
% chmod +x mycommand
Now we can run mycommand as a command.
To check the permissions of a file, use ls -l . For more information on chmod, use man chmod.
________________________________________
cd
Use cd to change directory. Use pwd to see what directory you are in.
% cd english
% pwd
% /u/ma/jeremy/english
% ls
novel poems
% cd novel
% pwd
% /u/ma/jeremy/english/novel
% ls
ch1 ch2 ch3 journal scrapbook
% cd ..
% pwd
% /u/ma/jeremy/english
% cd poems
% cd
% /u/ma/jeremy
Jeremy began in his home directory, then went to his english subdirectory. He listed this directory using ls , found that it contained two entries, both of which happen to be diretories. He cd'd to the diretory novel, and found that he had gotten only as far as chapter 3 in his writing. Then he used cd .. to jump back one level. If had wanted to jump back one level, then go to poems he could have said cd ../poems. Finally he used cd with no argument to jump back to his home directory.
________________________________________
cp
Use cp to copy files or directories.
% cp foo foo.2
This makes a copy of the file foo.
% cp ~/poems/jabber .
This copies the file jabber in the directory poems to the current directory. The symbol "." stands for the current directory. The symbol "~" stands for the home directory.
________________________________________
date
Use this command to check the date and time.
% date
Fri Jan 6 08:52:42 MST 1995
________________________________________
echo
The echo command echoes its arguments. Here are some examples:
% echo this
this
% echo $EDITOR
/usr/local/bin/emacs
% echo $PRINTER
b129lab1
Things like PRINTER are so-called environment variables. This one stores the name of the default printer --- the one that print jobs will go to unless you take some action to change things. The dollar sign before an environment variable is needed to get the value in the variable. Try the following to verify this:
% echo PRINTER
PRINTER
________________________________________
ftp
Use ftp to connect to a remote machine, then upload or download files. See also: ncftp
Example 1: We'll connect to the machine fubar.net, then change director to mystuff, then download the file homework11:
% ftp solitude
Connected to fubar.net.
220 fubar.net FTP server (Version wu-2.4(11) Mon Apr 18 17:26:33 MDT 1994) ready.
Name (solitude:carlson): jeremy
331 Password required for jeremy.
Password:
230 User jeremy logged in.
ftp> cd mystuff
250 CWD command successful.
ftp> get homework11
ftp> quit
Example 2: We'll connect to the machine fubar.net, then change director to mystuff, then upload the file collected-letters:
% ftp solitude
Connected to fubar.net.
220 fubar.net FTP server (Version wu-2.4(11) Mon Apr 18 17:26:33 MDT 1994) ready.
Name (solitude:carlson): jeremy
331 Password required for jeremy.
Password:
230 User jeremy logged in.
ftp> cd mystuff
250 CWD command successful.
ftp> put collected-letters
ftp> quit
The ftp program sends files in ascii (text) format unless you specify binary mode:
ftp> binary
ftp> put foo
ftp> ascii
ftp> get bar
The file foo was transferred in binary mode, the file bar was transferred in ascii mode.
________________________________________
grep
Use this command to search for information in a file or files. For example, suppose that we have a file dict whose contents are
red rojo
green verde
blue azul
white blanco
black negro
Then we can look up items in our file like this;
% grep red dict
red rojo
% grep blanco dict
white blanco
% grep brown dict
%
Notice that no output was returned by grep brown. This is because "brown" is not in our dictionary file.
Grep can also be combined with other commands. For example, if one had a file of phone numbers named "ph", one entry per line, then the following command would give an alphabetical list of all persons whose name contains the string "Fred".
% grep Fred ph | sort
Alpha, Fred: 333-6565
Beta, Freddie: 656-0099
Frederickson, Molly: 444-0981
Gamma, Fred-George: 111-7676
Zeta, Frederick: 431-0987
The symbol "|" is called "pipe." It pipes the output of the grep command into the input of the sort command.
For more information on grep, consult
% man grep
________________________________________
head
Use this command to look at the head of a file. For example,
% head essay.001
displays the first 10 lines of the file essay.001 To see a specific number of lines, do this:
% head -20 essay.001
This displays the first 20 lines of the file.
________________________________________
ls
Use ls to see what files you have. Your files are kept in something called a directory.
% ls
foo letter2
foobar letter3
letter1 maple-assignment1
%
Note that you have six files. There are some useful variants of the ls command:
% ls l*
letter1 letter2 letter3
%
Note what happened: all the files whose name begins with "l" are listed. The asterisk (*) is the " wildcard" character. It matches any string.
________________________________________
lpr
This is the standard Unix command for printing a file. It stands for the ancient "line printer." See
% man lpr
for information on how it works. See print for information on our local intelligent print command.
________________________________________
mkdir
Use this command to create a directory.
% mkdir essays
To get "into" this directory, do
% cd essays
To see what files are in essays, do this:
% ls
There shouldn't be any files there yet, since you just made it. To create files, see cat or emacs.
________________________________________
more
More is a command used to read text files. For example, we could do this:
% more poems
The effect of this to let you read the file "poems ". It probably will not fit in one screen, so you need to know how to "turn pages". Here are the basic commands:
• q --- quit more
• spacebar --- read next page
• return key --- read next line
• b --- go back one page
For still more information, use the command man more.
________________________________________
mv
Use this command to change the name of file and directories.
% mv foo foobar
The file that was named foo is now named foobar
________________________________________
ncftp
Use ncftp for anonymous ftp --- that means you don't have to have a password.
% ncftp ftp.fubar.net
Connected to ftp.fubar.net
> get jokes.txt
The file jokes.txt is downloaded from the machine ftp.fubar.net.
________________________________________
This is a moderately intelligent print command.
% print foo
% print notes.ps
% print manuscript.dvi
In each case print does the right thing, regardless of whether the file is a text file (like foo ), a postcript file (like notes.ps, or a dvi file (like manuscript.dvi. In these examples the file is printed on the default printer. To see what this is, do
and read the message displayed. To print on a specific printer, do this:
% print foo jwb321
% print notes.ps jwb321
% print manuscript.dvi jwb321
To change the default printer, do this:
% setenv PRINTER jwb321
________________________________________
pwd
Use this command to find out what directory you are working in.
% pwd
/u/ma/jeremy
% cd homework
% pwd
/u/ma/jeremy/homework
% ls
assign-1 assign-2 assign-3
% cd
% pwd
/u/ma/jeremy
%
Jeremy began by working in his "home" directory. Then he cd 'd into his homework subdirectory. Cd means " change directory". He used pwd to check to make sure he was in the right place, then used ls to see if all his homework files were there. (They were). Then he cd'd back to his home directory.
________________________________________
rm
Use rm to remove files from your directory.
% rm foo
remove foo? y
% rm letter*
remove letter1? y
remove letter2? y
remove letter3? n
%
The first command removed a single file. The second command was intended to remove all files beginning with the string "letter." However, our user (Jeremy?) decided not to remove letter3.
________________________________________
rmdir
Use this command to remove a directory. For example, to remove a directory called "essays", do this:
% rmdir essays
A directory must be empty before it can be removed. To empty a directory, use rm.
________________________________________
rsh
Use this command if you want to work on a computer different from the one you are currently working on. One reason to do this is that the remote machine might be faster. For example, the command
% rsh solitude
connects you to the machine solitude. This is one of our public workstations and is fairly fast.
See also: telnet
________________________________________
setenv
% echo $PRINTER
labprinter
% setenv PRINTER myprinter
% echo $PRINTER
myprinter
________________________________________
sort
Use this commmand to sort a file. For example, suppose we have a file dict with contents
red rojo
green verde
blue azul
white blanco
black negro
Then we can do this:
% sort dict
black negro
blue azul
green verde
red rojo
white blanco
Here the output of sort went to the screen. To store the output in file we do this:
% sort dict >dict.sorted
You can check the contents of the file dict.sorted using cat , more , or emacs .
________________________________________
tail
Use this command to look at the tail of a file. For example,
% head essay.001
displays the last 10 lines of the file essay.001 To see a specific number of lines, do this:
% head -20 essay.001
This displays the last 20 lines of the file.
________________________________________
tar
Use create compressed archives of directories and files, and also to extract directories and files from an archive. Example:
% tar -tvzf foo.tar.gz
displays the file names in the compressed archive foo.tar.gz while
% tar -xvzf foo.tar.gz
extracts the files.
________________________________________
telnet
Use this command to log in to another machine from the machine you are currently working on. For example, to log in to the machine "solitude", do this:
% telnet solitude
See also: rsh.
________________________________________
wc
Use this command to count the number of characters, words, and lines in a file. Suppose, for example, that we have a file dict with contents
red rojo
green verde
blue azul
white blanco
black negro
Then we can do this
% wc dict
5 10 56 tmp
This shows that dict has 5 lines, 10 words, and 56 characters.
The word count command has several options, as illustrated below:
% wc -l dict
5 tmp
% wc -w dict
10 tmp
% wc -c dict
56 tmp
Unix commands reference card
________________________________________
Environment Control
Command Description
cd d Change to directory d
mkdir d Create new directory d
rmdir d Remove directory d
mv f1 [f2...] d Move file f to directory d
mv d1 d2 Rename directory d1 as d2
passwd Change password
alias name1 name2 Create command alias (csh/tcsh)
alias name1="name2" Create command alias (ksh/bash)
unalias name1[na2...] Remove command alias na
ssh nd Login securely to remote node
exit End terminal session
setenv name v Set env var to value v (csh/tcsh)
export name="v" set environment variable to value v (ksh/bash)
________________________________________
Output, Communication, & Help
Command Description
lpr -P printer f
or
lp -d printer f Output file f to line printer
script [f] Save terminal session to f
exit Stop saving terminal session
mailx username Send mail to user
man name Unix manual entry for name
________________________________________
Process Control
Command Description
CTRL/c * Interrupt processes
CTRL/s * Stop screen scrolling
CTRL/q * Resume screen output
sleep n Sleep for n seconds
jobs Print list of jobs
kill % Kill job n
ps Print process status stats
kill -9 n Remove process n
CTRL/z * Suspend current process
stop %n Suspend background job n
cmmd& Run cmmd in background
bg [%n] Resume background job n
fg [%n] Resume foreground job n
exit Exit from shell
________________________________________
Environment Status
Command Description
ls [d] [f...] List files in directory
ls -1 [f...] List files in detail
alias [name] Display command aliases
printenv [name] Print environment values
quota Display disk quota
date Print date & time
who List logged in users
whoami Display current user
finger [username] Output user information
chfn Change finger information
pwd Print working directory
history Display recent commands
! n Submit recent command n
________________________________________
File Manipulation
Command Description
vi [f] Vi fullscreen editor
emacs [f] Emacs fullscreen editor
ed [f] Text editor
wc f Line, word, & char count
cat f List contents of file
more f List file contents by screen
cat f1 f2 >f3 Concatenates f1 & f2 into f3
chmod mode f Change protection mode of f
cmp f1 f2 Compare two files
cp f1 f2 Copy file f1 into f2
sort f Alphabetically sort f
split [-n] f Split f into n-line pieces
mv f1 f2 Rename file f1 as f2
rm f Delete (remove) file f
grep 'ptn' f Outputs lines that match ptn
diff f1 f2 Lists file differences
head f Output beginning of f
tail f Output end of f
________________________________________
Compiler
Command Description
cc [-o f1] f2 C compiler
lint f Check C code for errors
f77 [-o f1] f2 Fortran77 compiler
pc [-o f1] f2 Pascal compiler
________________________________________
Working with NFS files
Files saved on the UITS central Unix computers Steel, the Parallel PC cluster, Solar/Lunar, and the Research SP are stored on the Network File Server (NFS). That means that your files are really on one disk, in directories named for the central Unix hosts on which you have accounts.
No matter which of these computers you are logged into, you can get to your files on any of the others. Here are the commands to use to get to any system directory from any other system:
cd /N/u/username/PPPC/
cd /N/u/username/Cobalt/
cd /N/u/username/Solar/
cd /N/u/username/Steel/
cd /n/u/username/SP/
Be sure you use the capitalization just as you see above, and substitute your own username for "username".
For example, if Jessica Rabbit is logged into her account on Steel, and wants to get a file on her SP account, she would enter:
cd /N/u/jrabbit/SP/
Now when she lists her files, she'll see her SP files, even though she's actually logged into Steel.
You can use the ordinary Unix commands to move files, copy files, or make symbolic links between files. For example, if Jessica Rabbit wanted to move "file1" from her Steel directory to her SP directory, she would enter:
mv -i /N/u/jrabbit/Steel/file1 /N/u/jrabbit/SP/
This shared file system means that you can access, for example, your SP files even when you are logged into Steel, and vice versa. However, if you are logged into the SP, you can only use the software installed on SP -- only users' directories are linked together, not system directories.
________________________________________
Abbreviations used in this document
CTRL/x hold down control key and press x
d directory
env environment
f filename
n number
nd computer node
prtr printer
ptn pattern
var variable
[y/n] yes or no
[] optional arg
... list
Environment Control
cd d Change to directory d
mkdir d Create new directory d
rmdir d Remove directory d
mv f1 [f2...] d Move file f to directory d
mv d1 d2 Rename directory d1 as d2
passwd Change password
alias name1 name2 Create command alias
unalias name1 Remove command alias name1
rlogin nd Login to remote node
logout End terminal session
setenv name v Set env var to value v
unsetenv name1 name2...] remove environment variable
Output, Communication, & Help
lpr -P printer f Output file f to line printer
script [f] Save terminal session to f
exit Stop saving terminal
session
mail username Send mail to user
biff [y/n] Instant notification of mail
man name UNIX manual entry for
name
learn Online tutorial
Process Control
Ctrl/c * Interrupt processes
Ctrl/s * Stop screen scrolling
Ctrl/q * Resume screen output
sleep n Sleep for n seconds
jobs Print list of jobs
kill [%n] Kill job n
ps Print process status stats
kill -9 n Remove process n
Ctrl/z * Suspend current process
stop %n Suspend background job n
command& Run command in background
bg [%n] Resume background job n
fg [%n] Resume foreground job n
exit Exit from shell
Environment Status
ls [d] [f...] List files in directory
ls -1 [f...] List files in detail
alias [name] Display command aliases
printenv [name] Print environment values
quota Display disk quota
date Print date & time
who List logged in users
whoami Display current user
finger [username] Output user information
chfn Change finger information
pwd Print working directory
history Display recent commands
! n Submit recent command n
File Manipulation
vi [f] Vi fullscreen editor
emacs [f] Emacs fullscreen editor
ed [f] Text editor
wc f Line, word, & char count
cat f List contents of file
more f List file contents by screen
cat f1 f2 > f3 Concatenates f1 & f2 into f3
chmod mode f Change protection mode of f
cmp f1 f2 Compare two files
cp f1 f2 Copy file f1 into f2
sort f Alphabetically sort f
split [-n] f Split f into n-line pieces
mv f1 f2 Rename file f1 as f2
rm f Delete (remove) file f
grep ‘ptn’ f Outputs lines that match ptn
diff f1 f2 Lists file differences
head f Output beginning of f
tail f Output end of f
Compiler
cc [-o f1] f2 C compiler
lint f Check C code for errors
f77 [-o f1] f2 Fortran77 compiler
pc [-o f1] f2 Pascal compiler
Unix Command Dictionary (Hanson)
________________________________________
Table of Contents
• UNIX Log In and Out Commands.
• UNIX Information Commands.
• UNIX C Language Commands.
• UNIX makefile Commands.
• UNIX Directory Commands.
• UNIX File Commands.
• UNIX Pipe and Redirection Commands.
• UNIX Mail Commands.
• UNIX Control-Key Commands.
• UNIX Terminal Environment Commands.
• UNIX Process Commands.
• UNIX Editor Commands.
• The ex Editor.
• The vi Editor.
________________________________________
UNIX Command Dictionaries
The UNIX manual is mostly on line and the UNIX `man' command is used to display parts of the manual. Typing
man [command] (CR)
will yield information in an almost readable format during a IBM Telnet session. The problem is that you have both UNIX and CMS paging the output. You respond to the UNIX paging prompt `:' with a `(CR)' return for a new page, `d (CR)' for a short new page, u (CR)' for a short page up (back), or `q (CR)' to quit. For the CMS paging prompt `holding', respond with the designated `Clear-key'. If you are using IBM Telnet, then `man [command]' usually produces poor output for the head of the display. The version `man -blou [command] (CR)' should remove underscoring and other backspacing for printing at UIC, but does not work completely. For a quick overview of a command try the `-q' quick option:
man -q command] (CR)
Alternatively,
man [command] > [file] (CR)
is useful for redirecting the output to a file that can later be transfer back to CMS for printing (e.g. by `printdoc'). The UNIX no paging `-r' option does not work in a CMS session, so the CMS user has to press both the `Return-key' for a new UNIX `man' page or the `Clear-key' for a new CMS page depending on the odd UNIX prompt or the CMS ``HOLDING'' prompt, respectively.
This abridged UNIX dictionary is only intended to be a short enough list to get you started without being bewildered by the enormous UNIX manuals, but with enough commands to be able to do something useful. For more information use the `man' command or refer to some of the UNIX texts. UNIX is a trademark of Bell Laboratories.
The format is
[command] [generic operand] : [Definition.]
along with a carriage return `(CR)' for each command. DO NOT FORGET that almost all UNIX commands must be in lower case. Do not attempt to learn all of this at once, but read some of it and try it out at an actual computer session.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
UNIX Log In and Out Commands:
login (CR) : Logon command.
logout (CR) : Logoff command.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
UNIX Information Commands
man [-option] [command] (CR) : Manual or UNIX help command. The usual quit sequence `q (CR)' can be used to quit long UNIX `man' listings, `(CR)' is used for new `man' pages. During a IBM Telnet session the `Clear-key' is needed for new CMS pages that are not the same as the `man' pages. Otherwise `d', `q' or `Ctrl-c' should work for UNIX like access.
finger [user] (CR) : Displays system biography on user `[user]'.
whereis [name] (CR) : Locates source for program or command; e.g. `whereis kermit'.
which [name] (CR) : Tell which version of a program or command will be used in your session in case of multiple copies; e.g. `which cc'.
whatis [command] (CR) : Describes the command [command].
who am i (CR) : Displays current user id and access.
who (CR) : Displays currently logged in users.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
UNIX C Language Commands
cc -o run [file].c (CR) : Compiles source [file].c, using the standard C compiler `scc2.0' and producing an executable named run. In place of `cc', use `scc3.0' or `scc' for the latest version of standard C or `pcc' for portable C.
cc -c [file].c (CR) : Compiles source [file].c, using the standard C compiler `scc2.0' and producing an object file named [file].o.
cc -hnoopt -o run [file].c (CR) : Compiles source [file].c, using the standard C compiler `scc3.0' and producing an executable file named run without scalar optimization or vector optimization while `hopt' enables scalar and vector optimization, Some other optimization related options are `-hinline' for inlining while `-hnone' is the default no inlining, `-hnovector' for no vector (vector is the default), and `-h listing' for a pseudo-assembler (CAL) listing. Some standard C options are `-htask3' for automatic parallelization (autotasking in crayese) and `-hvector3' for more powerful vector restructuring. Other `-h' suboptions are `ivdep' for ignore vector dependence, `-hreport=isvf' generates messages about inlining (i), scalar optimization (s) and vector optimization (v), and `-hreport=isvf' writes same messages to `[file].v'. A commonly used form will be
cc -o run -h report=isvf [file].c (CR)
See `man cc' or `docview' for more information.
#define fortran : Form of C header statement to permit the call to a fortran subroutine from a C program. For example:
#include
#include
#define fortran
main()
{
fortran void SUB();
float x = 3.14, y;
SUB(&x, &y);
printf("SUB answer: y = %f for x = %f\n", x, y);
}
#pragma _CRI [directive] : Form of C compiler directive placed within the C code, where some example directives are `ivdep' for ignoring vector dependence, `novector' for turning off the default vectorization, `vector' for turning it back on, `inline' for procedure inline optimization, `shortloop', `noreduction', `getcpus [p]', `relcpus', `parallel ........', and `end parallel'. See `vector directives' for instance in `docview' for more information and examples.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
UNIX makefile Commands
make [-options] [step-name] (CR) : Makes the files [files] according to the template in the `makefile'. See the examples `makefile *' on the `getdisk hanson' disk in CMS, e.g., the file `makefile.unicos_2':
# Use ``make -f make.unicos_2 mrun>& pgm.l &;
runout''.
SOURCES = pgm.f
OBJECTS = pgm.o
FLAGS = -em
mrun : $(OBJECTS)
segldr -o run $(OBJECTS)
.f.o : cft77 $(FLAGS) $*.f
{CAUTION: The commands, like `segldr' or `cft77', must be preceded by a `Tab-key' tab as a delimiter, but the tab will not be visible in the UNIX listing.}
fmgen -m [make-name] -c cft77 -f [-flag] -o [executable] [source].f (CR) : Automatically generates a makefile for compiling under the `cft77' compiler and loading up the executable file named `[executable]'. Invoke with `make -f [make-name] [executable](CR)' and the execute `[executable]'. Also produces steps for profiling, flow-traces, performance traces, and clean-up, in the heavily documented makefile. For example, `make -c cft77 -f -em -o run pgm.f (CR)' produces a makefile named `makefile', executable named `run', an information listing named `[name in program statement].l' with loops marked by optimization type, etc.; the making is done with `make run (CR)'. Caution: the makefile only uses the source name only when that coincides with the name used in the Fortran `program' statement and only one type of `cft77' flag can be used. These flaws can be corrected by editing the resulting makefile `[make-name]'.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
UNIX Directory Commands
mkdir [name] (CR) : Makes a directory or file group name [name]; e.g. `mkdir dirpgm (CR)' make the directory called `dirpgm'.
pushd [name] (CR) : Pushes from the working directory to the directory [name] keeping the sequence of directories in a buffer for `popd'.
popd (CR) Pops back up to the prior directory, if `pushd' was used before. For this reason, `pushd' and `popd' are more useful than the regular change directory command `cd'.
cd [directory] (CR) : Changes the working directory to the directory [directory]; you can change it back with `cd(CR)' using your own login id; `cd $HOME (CR)' returns the shell back to your home directory. `.' denotes the current directory and `..' denotes the root or parent directory.
cd ~[user] (CR) : Changes working directory to that of login id `[user]'.
cd $TMP (CR) : changes to your temporary directory; same as `cd $TMP (CR)'.
pwd (CR) : Displays working directory; `echo $HOME (CR)' displays the home directory.
ls [directory] (CR) : displays the contents of the directory `[directory]'.
mv [file1] ... [fileN] [directory] (CR) : moves `[file1]', ..., `[fileN]' to directory `[directory]'; e.g. `mv addtwo.* diradd' moves all files with prefix `addtwo.' to the directory `diradd' which must already exist from a prior 'mkdir diradd' command. This format works for `cp' also.
cp [file1] [directory]/[file2] (CR) : copies [file1] into [file2] in directory [directory]. `cp [file] . (CR)' copies a file to the current directory using the original name. This format works for `mv' also.
rmdir (CR) : Removes or erases empty directory. You must first use `rm *' to empty the file.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
UNIX File Commands
ls (CR) : Lists sets or files of current user id or current directory.
ls ~[user] (CR) : Lists files or directories under user/account id `[user]'. Also `ls ~/[directory] (CR)' will list the contents of the directory `[directory]' on the same account.
ls [string].* (CR) : Lists all current files with prefix [name]. Examples of other forms are `ls *[string] (CR)' or `ls *[string]* (CR)' or `ls *[string1]*[string2]*'.
cat [file1] ... [fileN] (CR) : Lists content of N (N .le. 1) argument files catenated. Use `cat [file1] ... [fileN] > [fileM] (CR),' to catenate and store the N files in `[fileM]'.
more [file] (CR) : Displays file in half pages of 11 lines; use `q (CR)' for quitting; use `d' for 11 more lines or `u' to go back up 11 more lines; similarly, `f' and `b' produce full pages forward and backwards, respectively; while `/[string]?[string]
Caution: works poorly with TELNET from CMS. Use `cat [file] (CR)' with the CMS Clear-key instead.
cp [file1] [file2] (CR) : Copies file `[file1]' into file `[file2]'.
rm [file1] (CR) : Erases file `[file1]'; can take several file arguments, with the system asking if you really want to do it, `y' for yes and `n' for no target file `[file2]' already exists to avoid unintentional. {The query can be removed in any session by the command `unalias rm (CR)' or permanently by editing the C-shell resource configuration file `.cshrc'.}
mv [file1] [file2] (CR) : Renames file `[file1'] as file `[file2]', i.e., moves one file to another.
grep `[str]' [file1] (CR) : Searches for string [str] in file [file1]. ``cat [file1] [file2] | grep `[string]' (CR)'' searches for the pattern `[string]' in the catenated files. Note the different string pattern, with the standard single quote used within the command to enclose the target string when it is more than one word.
diff [file1] [file2] (CR) : Displays the difference between files `[file1]' and `[file2]'.
chmod [mode] [file] (CR) : Changes the read, write and execute permissions for the file (or files) `[file]' according to the `[mode]' which has form `[[who] [operator] [permission]]'; `[who]' is `u' for the user, `g' for the defined group', `o' for others and `a' = `ugo' for all; `[operator]' is `+' to add and `-' for remove; `[permission]' is `r' for read, `w' for write and `x' for execute; current permissions are displayed with the default long list command `ls [file] (CR)' in the first field or the sample forms `drwxrwxr-x' or `-rwxr--r--' with the first character denoting a directory if `d' is present, a `--' denotes no permission, with each remaining subfield of three denoting the user, group and others, respectively; for example `chmod go-wx *' removes write and execute permissions in the current directory for all but the user, `chmod u+w [file]' adds write permission to only the user; the user may be queried about removing protection for his own files.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
UNIX Pipe and Redirection Commands
The commands in this subsection embody some of the powerful advantages of UNIX.
alias [command nickname] `[command definition]' (CR) : Makes alias for commands to save typing. The quotes around the definition are not required for single words, but only when the definition contains delimiters like blanks. If used a lot, put the `alias' in the group account `.cshrc' file and execute by `source .cshrc}. {`csh' is the UNIX C-shell, one of the UNIX operating sub-systems, and `rc' stands for resource configuration.}
[command] > [file] (CR) : Redirects standard output of command [command] to file [file]. E.g., `cat [fn1] [fn2] > [fn3] (CR)', catenating two files into a third file.
[command] > & [file] (CR) : Redirects standard and diagnostic or error output of [command] to file [file]. E.g., `run > & [output] (CR)', puts compiled listing and errors into the file pgm.l when pgm.f is compiled.
[command] >> [file] (CR) : Redirects standard output of `[command]' and appends it to `[file]'. E.g., `run < [data] >> [output] (CR)', catenates the new output with the current file `[output]'.
[cmd1] | [cmd2] (CR) : Pipes output of command `[cmd1]' to input of command `[cmd2]'. E.g., `ls -l | grep `Jan 31' (CR)' lists only those files last changed on January 31. Caution: the string `Jan 31' must be enclosed in single quotes, but the quotes are optional for single words without delimiters.
[command] & (CR) : Executes `[command]' in the background, so you can work at something else in your session. E.g., `run < [data] > [output] & (CR)', execute `run' and stores results into the file `[output]'.
history (CR) : Lists the history of the most recent commands entered.
![number] (CR) : Repeats execution of the command numbered `[number]' in the `history' listing.
![string] (CR) : Repeats execution of the last command beginning with the pattern [string] in the `history' listing.
![string]:p (CR) : Repeats listing of the last command beginning with the pattern `[string]' in the `history' listing, but does not execute it. You can return (CR) to execute or you can modify it by the command that follows immediately below.
^[str1]^[str2] (CR) : Replaces string `[str1]' with `[str2]' in the previous command and executes the modified command.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
UNIX Mail Commands
mail (CR) : Shows user`s mail; use the subcommand `t [N](CR)' to list message number `[N]' , `s [N] mbox (CR)' to append message `[N]' to your mailbox `mbox' file or `s [N] [file](CR)' to append `[N]' to another file; `e [N] (CR)' to edit number [N] or look at a long file with `ex' {see Section on `EX' below}; `v [N] (CR)' to edit number [N] or look at a long file with `vi'; `d [N] (CR)' deletes {your own mail!} `[N]'; `m [user] (CR)' permits you to send mail to another account `[user]'; a `~m [N] (CR)' inside the message after entering a subject, permits you to forward message `[N]' to `[user]', `\d (CR)' to end the new message {see the send form below;`x' quits `mail' without deleting {use this when you run into problems}; and `q (CR)' to quit.
mail [user] (CR) : Sends mail to user `[user]'; the text is entered immediately in the current blank space; carriage return to enter each line; enter a file with a `~r[filename] (CR)'; route a copy to user `[userid]' by `~c[userid] (CR)'; enter the `ex' line editor with `~e (CR)' or `vi' visual editor with `~v (CR)' (see Sections on EX and on VI) to make changes on entered lines, exiting `ex' with a `wq (CR)' or `vi' with a `:wq' (CR)'; exit `mail' by entering `\d (CR)'. {A bug in the current version of Telnet does not allow you to send a copy using the `cc:' entry. However, ending with the ``hack'' `\d [user_cc] (CR)' should get a copy to user `[user_cc]'.} UNIX users should not encounter IBM Telnet problems.
mail [userid]@uicvm.cc.uic.edu < [filename] (CR) : Sends the UNIX file `[filename]' to user `[userid]' at UICVM, i.e., to `[userid]'`s CMS, as with CMS SENDFILE. mail [name]@[machine].[dept].uic.edu < [filename] (CR) : Sends the UNIX file `[filename]' to user `[name]' on some UNIX or other machine. from (CR) : Tells who the mail is from. ________________________________________ Return to TABLE OF CONTENTS? ________________________________________ UNIX Control-Key Commands Ctrl-h : Erase or backspace over character; note the CTRL-key and h-key must be simultaneously pressed. Ctrl-c : Interrupt or break character; stops printing and returns to UNIX. {Caution: for a IBM TELNET session, should use \c (CR), but this masked interrupt will not work during long listings due to interference of the CMS `Clear-key' in IBM Telnet sessions.} Ctrl-s : Stop character {else or IBM Telnet use ` \s (CR)'}. Ctrl-q : Quiet character {else for IBM Telnet use `\q (CR)'}. Ctrl-u : Kill character {else for IBM Telnet use `\u (CR)'}. Ctrl-w : Word erase character {else for IBM Telnet use `\w (CR)'}. ________________________________________ Return to TABLE OF CONTENTS? ________________________________________ UNIX Terminal Environment Commands printenv (CR) : Print out environment meta parameters such as defaults, terminal types and key assignments, eg., SHELL, PATH, TERM, NCPUS, HOME, TMP, AFS, and AFSTMP. setenv TERM vt100 (CR) : Sets `TERM' variable to type `vt100', which should be the default and can be checked by using `printenv', else use `h19b' or your correct terminal emulation if recognizable by the remote host. The recognizable terminal type are in the alphabetized subdirectories of `/usr/lib/terminfo', e.g., `v' directory contains vt100 listings. Caution: `YTERM' is ideal for PC to CMS communication, but does not have a terminal type that is recognizable by UNIX systems ('vt100' may sometimes work as a substitute, but `unknown' type means a poor line edit session). setenv TERMCAP vt100 (CR) : Sets `TERMCAP' variable to type `vt100', else use `h19b' etc. You can put customized terminal configuration in the file `.termcap' and enable it with the command `setenv TERMCAP $HOME.termcap' either in your shell or in your '.login' file. tset -m :h19b (CR) : Sets terminal type to Heathkit or Zenith type `h19b'. WARNING: Several terminal commands are given here, because you might have to try several before you find one that works. Note that one of the biggest problems in working with advanced, remote computers is COMMUNICATION and that much of this local guide is intended to solve communication problems. stty erase \[key](CR) : Set or reset the terminal (`tty') erase key to `[key]'. stty all (CR) : Display usual Control characters; with arguments can be use to set terminal communication variables; also try `stty everything'. ________________________________________ Return to TABLE OF CONTENTS? ________________________________________ UNIX Process Commands jobs - l (CR) : Display a simple single line with currently active job status. ps (CR) : Display current process ids (``pid'') needed for killing. ps t[N] (CR) : Displays ``pid'' for terminal or tty [N]. kill -9 [pid] (CR) : Means a ``sure kill'' of ``pid'' [pid]; this becomes necessary when you lose control of a process or have a session aborted. CAUTION: Aborted sessions are not uncommon so it is helpful to develop skills of a super process (program) killer. ________________________________________ Return to TABLE OF CONTENTS? ________________________________________ UNIX Editor Commands ex [file] (CR) : `EX' line editor. This is the preferred editor for LINE EDIT MODE with TELNET. `:' is the `ex' prompt. `ex [file1] ... [fileN] (CR)' is the form used for multiple files with the `n' command used to go to the next file, rather than `q' to quit. Ex can also be used in vi with ':' as a prefix, when vi works. `ed' is another line editor with less availability. More details on `ex' are given in the next section. vi [file] (CR) : Invokes the UNIX full screen editor `vi' to edit file [file]; this visual editor has a large set of subcommands. Warning: the `vi' command will NOT work properly with the LINE MODE of CMS TELNET and YOU WILL LIKELY GET IN A STUCK SESSION IF YOU TRY IT. (Try to get access to a UNIX system or PC Telnet systems, such as those in the 2249f SEL PC Lab.) vi -r [file] (CR) : Form of `vi' used to recover `[file]' after aborted session. Similarly, `ex -r [file] (CR)' is for an aborted `ex' session. view [file] (CR) : This is the read only form of `vi'. ________________________________________ Return to TABLE OF CONTENTS? ________________________________________ ex Editor `Ex' is the UNIX line editor (`ed' is another UNIX line editor) and `vi' is the full screen editor that is disabled by IBM TELNET. The prompt is `:', but the user enters input at the bottom of the screen with IBM TELNET line mode. In `ex' `.' means the current line, `$' means the last line, and `%' means the whole range of lines `1,$'. `[L1],[L2]' denotes the range from line `[L1]' to line `[L2]'. The user may want to do major editing on the CMS multi-line editor XEDIT and send the file using the FTP file transfer protocol. Some students may have had experience with this editor (or the similar `ed' editor) from EECS courses. These `ex' commands can be used within the `vi' editor by typing a colon `:' in front of the `ex' command, which is another reason for learning `ex' with `vi' when you have an account where `vi' can be used. 0a (CR) : This form of the append subcommand puts you in input mode starting with line 1, new lines are entered following a `(CR)', and input mode is ended with a solitary or sole `.' on a line with an immediate `(CR)', i.e., `.(CR)'. This is not the usual method for opening a new file, but the usual way does not work correctly with the IBM Telnet and CMS pass through. q! (CR) : Quit or abort `ex' without saving. Use, especially, in an emergency when your edit session seems hopeless, and you want to start over at the beginning. w [file] (CR) : Save (write) or resave into the new file [file], but do not end. If no [file] is specified, then the current file is resaved. w! [file] (CR) : Resave (rewrite) into an old file [file], but do not end. `Ex' will not write over an existing non-current file with the `w' command without the `!'. wq (CR) : Save and quit ex. w|n (CR) : When `ex' is used on more than one file, writes the current file and makes `ex' go to the next file. `|' puts two `ex' commands together. nu (CR) : Number current line. set number (CR) : Number all lines; line numbers are needed for effective use or `ex'; by putting this and other commands in your `.exrc' Ex Resource Configuration file, the command will be operative until it is changed. set list (CR) : Show carriage control characters, such as End-Of-Line ($ = EOL). /[string] (CR) : Search forward for pattern [string]. ?[string] (CR) : Search backward for pattern [string]. [L1],[L2] p (CR) or [L1],[L2] l (CR) : Prints or lists (listing control characters) lines [L1] to [L2]. `% (CR)' lists the whole range of lines, and `.,$ (CR)' lists the current line to the last line. $ (CR) : Prints last line. \d (CR) : Scrolls lines {In UNIX, use `Ctrl-d'}. [L1],[L1]+[N] p (CR) : Prints lines [L1] to [L1]+[N]. [L1],[L2] d (CR) : Deletes lines [L1] to [L2]. [L1] i (CR) : Insert at line [L1]. End with a lone `.(CR)' after the last input line. Does not work on an empty file. [L1] a (CR) : Append after line [L1]. End with a lone `.(CR)' after the last input line. Does not work on an empty file. [L1] o (CR) : The UNIX open command does not work correctly with IBM TELNET because the usual end commands do not work properly. End with a line `.(CR)'. [L1] c (CR) : Change line [L1]; end with `.(CR) alone. [L1],[L2] co [L3] (CR) : Copy lines [L1] to [L2] to after line [L3]. [L1],[L2] m [L3] (CR) : Move lines [L1] to [L2] to after line [L3]. [L1],[L2] t [L3] (CR) : Take {copy} lines [L1] to [L2] to [L3]; destination [L3] can not be in ([L1] to [L2]-1). [L1],[L2] g/[string]/[commands] (CR) : Globally search for all occurrences of pattern [string] in lines [L1] to [L2] (or current line only if no lines are given), and execute commands [commands] on matching lines. [L1],[L2] s/[string1]/[string2]/g (CR) : Substitute [string2] for all [string1] in lines [L1] to [L2] (or current line only if no lines are given). If `gp' is used in place of `g' then print change(s). [L1],[L2] & (CR) : Repeat prior substitution command. g/[string1]/s/{/}[string2]/gp (CR) : Globally substitute [string2] for each [string1] in all lines and print changes; works globally; use `?' in place of `/' if [string*] contains a `/'. [L] r [file] (CR) : Read in or append file [file] at line [L]. u (CR) : Undo most recent substitution. [L1],[L2] ya [buffer] (CR) : Yank lines [L1] to [L2] to named buffer [buffer]. See `pu'. [L1],[L2] d [buffer] (CR) : Delete and yank lines [L1] to [L2] to named buffer [buffer]. See `pu'. [L3] pu [buffer] (CR) : Put lines from named buffer [buffer] after line [L3]. See `ya'. g/^$/d (CR) : Delete all null lines. s/A\/B/A\/C/g (CR) : Illustrates the use of `\' to change a string containing the `/' delimiter to change `A/B' to `A/C' globally. ________________________________________ Return to TABLE OF CONTENTS? ________________________________________ vi Editor The UNIX full screen editor `vi' is a tightly designed editing system in which almost every letter has a function and the function is stronger for upper than lower case. However, a letter and its actual function are usually closely related. It is important to remember that the `(Esc)' escape key ends most functions and a `(Esc), (Esc)' double application certainly ends the function with the ring of a bell. The subcommand `u' undoes the last function (presumably an error). Use `:q! (CR)' to end with out saving, especially in hopeless situations. Use `:wq (CR)' to resave and end {`ZZ' also resaves and ends, but will not resave if the file has been saved in another file and no further changes have been made}, or `:w (CR)' to only resave. The character `:' prompts the UNIX line editor `ex' which you can think of as being embedded in `vi'. Some of the above critical `vi' subcommands are repeated below with others. Most `vi' subcommands are not displayed when used and do not take a carriage return `(CR)'. The fact that most keys have a meaning both as single characters and as concatenations of several characters has many benefits, but has disadvantages in that mistakes can turn out to be catastrophic. {Remember that `(Esc), (Esc), u' key sequence!} {WARNING: `VI' is disabled during an IBM Telnet session.} (Esc) : End a command; especially used with insert `i', append `a' or replace 'R'. (Esc), (Esc) : Ensured end of a command with bell; press the Escape-key twice; use it. u : Undoes last command; usually used after `(Esc)' or `(Esc), (Esc)'; if undoing is worse then repeat `u' again to undo the undoing. :set all (CR) : Display all vi options. Use this ex command when your initial vi session is poor. Customized options are placed in the `.exrc' ex resource configuration profile. :w (CR) : Save or resave the default file being edited, but do not end. :w [file] (CR) : Save into a new file [file], but do not end. :w! [file] (CR) : Save or resave into an existing file [file], but do not end. :q (CR) : Quit vi without saving, provided no changes have been made since the last save. :q! (CR) : Quit vi without saving, living the file as it was in the last save. :wq (CR) : Save the default file being edited, and quit. ZZ : Save the edited file, provided not changes have been made since the last save of the edited file to any file, and quit `vi'. {Warning: if you just saved the edited file into any other file, the file will NOT be resaved. `:wq (CR) is much safer to use.} h or j or k or l : The arrow keys, such that k = up ^ | h = left <-- --> right = l
|
v
j = down
each take a number prefix that moves the cursor that many times.
(CR) : moves cursor a line forward; `+' also does.
-- : Moves cursor a line backward.
[N] (CR) : Moves cursor [N] lines forwards.
[N]-- : Moves cursor [N] lines backwards.
Ctrl-f : Moves cursor a page forward.
Ctrl-b : Moves cursor a page backward.
Ctrl-d : Moves cursor a half page down.
Ctrl-u : Moves cursor a half page up.
[L]G : Go to line [L]. `1G' moves the cursor to the beginning of the file (BOF).
G : Go to the last line just before the end of file (EOF) mark. `$G' does the same thing.
0 : Go to beginning of the line (BOL).
^ : Go to beginning of the nonblank part of the line (BOL).
~ : Got to first nonblank character on a line.
$ : Go to end of the line (EOL).
[N]| : Go to column [N] of the current line.
% : Find the matching parenthesis.
/[string] (CR) : Find the next occurrence of `[string]' forwards. Use `n' to repeat, or `N' to search backwards.
?[string] (CR) : Find the next occurrence of` [string]' backwards.
n : Repeat last `/[string] (CR)' or `?[string] (CR)'; think of the file as being wrapped around from end to beginning, so that when you return to the start you know that you have found all occurrences.
N : Repeat last `/[string] (CR)' or `?[string] (CR)', but in reverse.
. : Repeat last change. This is best used along with the repeat search `n' or `N'.
i[string](Esc) : Insert a string `[string]' before current character at the cursor; the subcommand `i' itself and other subcommands are not displayed; a `(CR)' in the string during the insert is used to continue input on additional lines; end with the escape key `(Esc)' or `(Esc), (Esc)'.
o[string](Esc) : Opens a new line below the current line for insertion of string `[string]'; end with `(Esc)' or `(Esc), (Esc)'; use for POWER TYPING input for an old or new file; `O[string](Esc)' opens a new line above the current line for insertion.
I[string](Esc) : Insert a string at the beginning of the current line (BOL), else is like insert `i';a `(CR)' in the string during the insert is used to continue input on additional lines; end with `(Esc)' or `(Esc), (Esc)'.
J : Joins next line to current line.
a[string](Esc) : Appends a string `[string]' following the current character at the cursor, else it works like insert `i'; use `(CR)' in the string to continue input onto new lines; end with `(Esc)'; also use for POWER TYPING.
A[string](Esc) : Appends a string `[string]' at the end of a line (EOL), works like `i' or `a'; use `(CR)' in the string to continue input onto new lines; end with `(Esc)'; also use for POWER TYPING.
r[C](SPACE) : Replace a single character over the cursor by the single character [C]; finalize with the Space-bar.
R[string](Esc) : Replace a string of characters by `[string]' in until `(Esc)' is typed to end.
s[string](Esc) : Substitutes the string `[string]' for the single character at the cursor. The multiple form `[N]s[string](Esc)' substitutes `[string]' for the `[N]' characters starting at the cursor.
x : Delete the current character at the cursor.
d(SPACE) : Deletes a single character. `[N]d(SPACE)' deletes `[N]' characters.
dd : Deletes the current line. `[N]dd' deletes `[N]' lines.
D : Deletes from the cursor to the end of line (EOL).
dw : Deletes the current word; `[N]dw' deletes `[N]' words.
w : Move cursor to the beginning of the next word. `[N]w' moves the cursor `[N]' words forward. `[N]b' moves it `[N]' words backward. `[N]e' moves it to the end of the word.
[N]y(SPACE) : Yanks `[N]' characters starting at the cursor and puts them into the default buffer. `[N]yy' yanks `[N]' lines.
p : Puts the current contents of the default buffer after the cursor if characters or after the current line if lines. Helpful to use right after a character yank `y' or a character delete `d' or a line yank `yy' or a line delete `dd', along with a search `/[string](CR)' or repeat search `n'. and a repeat change `.'. `P' puts the contents of the default buffer before the current line.
"b[N]Y : Yank [N] lines starting with the current line to the buffer labeled b; the double quote {"} is used to avoid an id conflict with subcommand names; any letter other than `x' can be used to name the buffer; safer than the line yank `yy' because it is very easy to accidentally change the default buffer.
"b[N]dd : Deletes [N] lines starting with the current line to the buffer labeled `b'.
"bp : Put back lines from the buffer labeled `b' after or below the cursor; use after a yank or delete to a labeled buffer to move groups of lines from one location to another.
"bP : Put back lines from the buffer labeled `b' before or above the cursor; use after a yank or delete to a labeled buffer to move groups of lines from one location to another.
Some `ex' editor commands that are useful in `vi' follow the `:' prompt. See the previous section on `ex' for more commands.
:nu (CR) : Number current line.
:[L1],[L2] d (CR) : Deletes lines `[L1]' to `[L2]'.
:[L1],[L2] m [L3] (CR) : Move lines `[L1]' to `[L2]' to after line `[L3]'.
:[L1],[L2] t [L3] (CR) : Take [copy] lines `[L1]' to `[L2]` to `[L3]'; destination `[L3]' can not be in `[L1]' to `[L2]-1'.
:[L1],[L2]s/[string1]/[string2]/g (CR) : Substitute `[string2]' for all `[string1]' in lines `[L1]' to `[L2]' only.
:s/[string1]/[string2]/gp (CR) : Substitute `[string2]' for all `[string1]' in current line only and print change(s).
:g/[string1]/s/{/}[string2]/gp (CR) : Globally substitute `[string2]' for each `[string1]' in all lines and print changes; works globally; use `?' in place of `/' if `[string*]' contains a `/'.
:[L]r [file] (CR) : Append file [file] at line `[L]'.
________________________________________
Return to TABLE OF CONTENTS?
________________________________________
Web Source: http://www.math.uic.edu/~hanson/UNIX/UnixDictionary.html
Email Comments or Questions to hanson@uic.edu
Commands
ls ................. show directory, in alphabetical order
logout ............. logs off system
mkdir .............. make a directory
rmdir .............. remove directory (rm -r to delete folders with files)
rm ................. remove files
cd ................. change current directory
man (command) ...... shows help on a specific command
talk (user) ........ pages user for chat - (user) is a email address
write (user) ....... write a user on the local system (control-c to end)
pico (filename) .... easy to use text editor to edit files
pine ............... easy to use mailer
more (file) ........ views a file, pausing every screenful
sz ................. send a file (to you) using zmodem
rz ................. recieve a file (to the unix system) using zmodem
telnet (host) ...... connect to another Internet site
ftp (host) ......... connects to a FTP site
archie (filename) .. search the Archie database for a file on a FTP site
irc ................ connect to Internet Relay Chat
lynx ............... a textual World Wide Web browser
gopher ............. a Gopher database browser
tin, trn ........... read Usenet newsgroups
passwd ............. change your password
chfn ............... change your "Real Name" as seen on finger
chsh ............... change the shell you log into
grep ............... search for a string in a file
tail ............... show the last few lines of a file
who ................ shows who is logged into the local system
w .................. shows who is logged on and what they're doing
finger (emailaddr).. shows more information about a user
df ................. shows disk space available on the system
du ................. shows how much disk space is being used up by folders
chmod .............. changes permissions on a file
bc ................. a simple calculator
make ............... compiles source code
gcc (file.c) ....... compiles C source into a file named 'a.out'
gzip ............... best compression for UNIX files
zip ................ zip for IBM files
tar ................ combines multiple files into one or vice-versa
lharc, lzh, lha .... un-arc'ers, may not be on your system
dos2unix (file) (new) - strips CR's out of dos text files
unix2dos (file) (new) - adds CR's to unix text files
General
apropos command
Locate commands by keyword lookup.
exit
Terminate your current session, or shell.
man command
Display the Unix manual page describing a given Unix command.
File System Navigation
cd
Return to your home directory.
cd directory
Change directory to make directory your current directory.
file files
Determine file type.
ls
List the contents of the current directory.
ls names
List the contents of the directories; names can name files and/or directories:
ls -l
. . . in a long format, showing permissions, owner, size, and other file info.
ls -a
. . . all files, including "hidden" files (file names that begin with a dot ".").
ls -R
. . . Recursively, for all subdirectories.
ls -t
. . . in time order (when modified, newest to oldest) rather than in name order.
pwd
Display the name of the current directory, or "print working directory."
File/Directory Manipulation
compress files
Reduces the size of a file.
uncompress files
Restores compressed files to their original form.
cp file1 file2
Copy file(s).
cp files directory
Copy file(s) into a directory.
cp -r dir1 dir2
Copy a directory and, recursively, its subdirectories.
mkdir directory
Create, or "make" a directory.
mv file1 file2
Move a file or, if file1 and file2 are in the same directory, rename a file.
mv files directory
Move files into a directory.
mv dir1 dir2
If directory dir2 exists, move dir1 into dir2; otherwise, rename dir1 as dir2.
rm files
Remove (erase) file(s).
rm -r names
Remove files, directories, and recursively, any subdirectories.
rmdir directory
Remove directory (directory must be empty).
Data Manipulation
cat files
Concatenate file(s); you can use cat to display the contents of a file (this is not advisable if the file is a binary file).
grep "pattern" files
Display all lines in the files that match a pattern.
more files
Display contents of files one screen at a time.
sort files
Order the lines in a file or files alphabetically (this command does not alter the file or files -- it merely displays the sorted output to the screen):
sort -r files
. . . in reverse order.
sort -n files
. . . numerically (puts 2 before 10 instead of after).
Networking/Communications
finger user@umich.edu
Displays information about a U-M user from the U-M Online Directory.
ssh hostname
Connect to a remote host using Secure Shell.
telnet hostname
Connect to a remote host using the telnet protocol.
talk user
Initiate a conversation with another user (end conversation with Control-C); talk works only between machines of the same architecture.
Miscellaneous
!!
Repeat last shell command.
!string
Repeat last shell command that began with string (for example, type "!m" to repeat the last command that began with "m").
cal
Display a calendar of the current month.
cal month year
Display a calendar of the given month and year. Note that the year must be fully qualified, for example, "2003" and not "03."
clear
Clears terminal screen.
date
Display the current local date and time.
who
Display a list of users currently logged in.
Files
• ls --- lists your files
ls -l --- lists your files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified.
ls -a --- lists all files, including the ones whose filenames begin in a dot, which you do not always want to see.
There are many more options, for example to list files by size, by date, recursively etc.
• more filename --- shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern.
• emacs filename --- is an editor that lets you create and edit a file. See the emacs page.
• mv filename1 filename2 --- moves a file (i.e. gives it a different name, or moves it into a different directory (see below)
• cp filename1 filename2 --- copies a file
• rm filename --- removes a file. It is wise to use the option rm -i, which will ask you for confirmation before actually deleting anything. You can make this your default by making an alias in your .cshrc file.
• diff filename1 filename2 --- compares files, and shows where they differ
• wc filename --- tells you how many lines, words, and characters there are in a file
• chmod options filename --- lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them, but you may sometimes want to change these permissions. For example, chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others again. Note that for someone to be able to actually look at the file the directories it is in need to be at least executable. See help protection for more details.
• File Compression
o gzip filename --- compresses files, so that they take up much less space. Usually text files compress to about half their original size, but it depends very much on the size of the file and the nature of the contents. There are other tools for this purpose, too (e.g. compress), but gzip usually gives the highest compression rate. Gzip produces files with the ending '.gz' appended to the original filename.
o gunzip filename --- uncompresses files compressed by gzip.
o gzcat filename --- lets you look at a gzipped file without actually having to gunzip it (same as gunzip -c). You can even print it directly, using gzcat filename | lpr
• printing
o lpr filename --- print. Use the -P option to specify the printer name if you want to use a printer other than your default printer. For example, if you want to print double-sided, use 'lpr -Pvalkyr-d', or if you're at CSLI, you may want to use 'lpr -Pcord115-d'. See 'help printers' for more information about printers and their locations.
o lpq --- check out the printer queue, e.g. to get the number needed for removal, or to see how many other files will be printed before yours will come out
o lprm jobnumber --- remove something from the printer queue. You can find the job number by using lpq. Theoretically you also have to specify a printer name, but this isn't necessary as long as you use your default printer in the department.
o genscript --- converts plain text files into postscript for printing, and gives you some options for formatting. Consider making an alias like alias ecop 'genscript -2 -r \!* | lpr -h -Pvalkyr' to print two pages on one piece of paper.
o dvips filename --- print .dvi files (i.e. files produced by LaTeX). You can use dviselect to print only selected pages. See the LaTeX page for more information about how to save paper when printing drafts.
Directories
Directories, like folders on a Macintosh, are used to group files together in a hierarchical structure.
• mkdir dirname --- make a new directory
• cd dirname --- change directory. You basically 'go' to another directory, and you will see the files in that directory when you do 'ls'. You always start out in your 'home directory', and you can get back there by typing 'cd' without arguments. 'cd ..' will get you one level up from your current position. You don't have to walk along step by step - you can make big leaps or avoid walking around by specifying pathnames.
• pwd --- tells you where you currently are.
Finding things
• ff --- find files anywhere on the system. This can be extremely useful if you've forgotten in which directory you put a file, but do remember the name. In fact, if you use ff -p you don't even need the full name, just the beginning. This can also be useful for finding other things on the system, e.g. documentation.
• grep string filename(s) --- looks for the string in the files. This can be useful a lot of purposes, e.g. finding the right file among many, figuring out which is the right version of something, and even doing serious corpus work. grep comes in several varieties (grep, egrep, and fgrep) and has a lot of very flexible options. Check out the man pages if this sounds good to you.
About other people
• w --- tells you who's logged in, and what they're doing. Especially useful: the 'idle' part. This allows you to see whether they're actually sitting there typing away at their keyboards right at the moment.
• who --- tells you who's logged on, and where they're coming from. Useful if you're looking for someone who's actually physically in the same building as you, or in some other particular location.
• finger username --- gives you lots of information about that user, e.g. when they last read their mail and whether they're logged in. Often people put other practical information, such as phone numbers and addresses, in a file called .plan. This information is also displayed by 'finger'.
• last -1 username --- tells you when the user last logged on and off and from where. Without any options, last will give you a list of everyone's logins.
• talk username --- lets you have a (typed) conversation with another user
• write username --- lets you exchange one-line messages with another user
• elm --- lets you send e-mail messages to people around the world (and, of course, read them). It's not the only mailer you can use, but the one we recommend. See the elm page, and find out about the departmental mailing lists (which you can also find in /user/linguistics/helpfile).
About your (electronic) self
• whoami --- returns your username. Sounds useless, but isn't. You may need to find out who it is who forgot to log out somewhere, and make sure *you* have logged out.
• finger & .plan files
of course you can finger yourself, too. That can be useful e.g. as a quick check whether you got new mail. Try to create a useful .plan file soon. Look at other people's .plan files for ideas. The file needs to be readable for everyone in order to be visible through 'finger'. Do 'chmod a+r .plan' if necessary. You should realize that this information is accessible from anywhere in the world, not just to other people on turing.
• passwd --- lets you change your password, which you should do regularly (at least once a year). See the LRB guide and/or look at help password.
• ps -u yourusername --- lists your processes. Contains lots of information about them, including the process ID, which you need if you have to kill a process. Normally, when you have been kicked out of a dialin session or have otherwise managed to get yourself disconnected abruptly, this list will contain the processes you need to kill. Those may include the shell (tcsh or whatever you're using), and anything you were running, for example emacs or elm. Be careful not to kill your current shell - the one with the number closer to the one of the ps command you're currently running. But if it happens, don't panic. Just try again :) If you're using an X-display you may have to kill some X processes before you can start them again. These will show only when you use ps -efl, because they're root processes.
• kill PID --- kills (ends) the processes with the ID you gave. This works only for your own processes, of course. Get the ID by using ps. If the process doesn't 'die' properly, use the option -9. But attempt without that option first, because it doesn't give the process a chance to finish possibly important business before dying. You may need to kill processes for example if your modem connection was interrupted and you didn't get logged out properly, which sometimes happens.
• quota -v --- show what your disk quota is (i.e. how much space you have to store files), how much you're actually using, and in case you've exceeded your quota (which you'll be given an automatic warning about by the system) how much time you have left to sort them out (by deleting or gzipping some, or moving them to your own computer).
• du filename --- shows the disk usage of the files and directories in filename (without argument the current directory is used). du -s gives only a total.
• last yourusername --- lists your last logins. Can be a useful memory aid for when you were where, how long you've been working for, and keeping track of your phonebill if you're making a non-local phonecall for dialling in.
Connecting to the outside world
• nn --- allows you to read news. It will first let you read the news local to turing, and then the remote news. If you want to read only the local or remote news, you can use nnl or nnr, respectively. To learn more about nn type nn, then \tty{:man}, then \tty{=.*}, then \tty{Z}, then hit the space bar to step through the manual. Or look at the man page. Or check out the hypertext nn FAQ - probably the easiest and most fun way to go.
• rlogin hostname --- lets you connect to a remote host
• telnet hostname --- also lets you connect to a remote host. Use rlogin whenever possible.
• ftp hostname --- lets you download files from a remote host which is set up as an ftp-server. This is a common method for exchanging academic papers and drafts. If you need to make a paper of yours available in this way, you can (temporarily) put a copy in /user/ftp/pub/TMP. For more permanent solutions, ask Emma. The most important commands within ftp are get for getting files from the remote machine, and put for putting them there (mget and mput let you specify more than one file at once). Sounds straightforward, but be sure not to confuse the two, especially when your physical location doesn't correspond to the direction of the ftp connection you're making. ftp just overwrites files with the same filename. If you're transferring anything other than ASCII text, use binary mode.
• lynx --- lets you browse the web from an ordinary terminal. Of course you can see only the text, not the pictures. You can type any URL as an argument to the G command. When you're doing this from any Stanford host you can leave out the .stanford.edu part of the URL when connecting to Stanford URLs. Type H at any time to learn more about lynx, and Q to exit.
Miscellaneous tools
• webster word --- looks up the word in an electronic version of Webster's dictionary and returns the definition(s)
• date --- shows the current date and time.
• cal --- shows a calendar of the current month. Use e.g., 'cal 10 1995' to get that for October 95, or 'cal 1995' to get the whole year.
You can find out more about these commands by looking up their manpages:
man commandname --- shows you the manual page for the command
-------------------------------------------------------------------------------------------------------------
awk
UNIX Utilities - awk
• utline
o General structure of awk scripts
o Elementary awk programming
Elementary examples
o Advanced awk programming
Advanced examples
o Important things which will bite you
• General structure of awk (Aho, Weinberg, and Kernighan)
o awk, oawk, nawk, gawk, mawk
The original version, based on the first edition of The awk Programming Language was called awk
2nd edition of book led to nawk
Unices usually ship with three different names for awk: oawk, nawk, and awk; either oawk=awk or nawk=awk.
gawk is the FSF version.
mawk is a speedier rewrite which does a partial compilation
o The awk command line is:
awk [program|-f programfile] [flags/variables] [files]
• Command line flags
o -f file -- Read the awk script from the specified file rather than the command line
o -F re -- Use the given regular expression re as the field separator rather than the default "white space"
o variable=value -- Initialize the awk variable with the specified
• An awk program consists of one or more awk commands separated by either \n or semicolons.
• The structure of awk commands
o Each awk command consists of a selector and/or an action; both may not be omitted in the same command. Braces surround the action.
o selector [only] -- action is print
o {action}[only] -- selector is every line
o selector {action} -- perform action on each line where selector is true
o Each action may have multiple statements separated from each other by semicolons or \n
• Line selection
o A selector is either zero, one, or two selection criteria; in the latter case the criteria are separated by commas
o A selection criterion may be either an RE or a boolean expression (BE) which evaluates to true or false
o Commands which have no selection criteria are applied to each line of the input data set
o Commands which have one selection criterion are applied to every line which matches or makes true the criterion depending upon whether the criterion is an RE or a BE
o Commands which have two selection criteria are applied to the first line which matches the first criterion, the next line which matches the second criterion and all the lines between them.
o Unless a prior applied command has a next in it, every selector is tested against every line of the input data set.
• Processing
o The BEGIN block(s) is(are) run (mawk's -v runs first)
o Command line variables are assigned
o For each line in the input data set
It is read and NR, NF, $I, etc. are set
For each command, its criteria are evaluated
If the criteria is true/matches the command is executed
o After the input data set is exhausted, the END block(s) is(are) run
• Elementary awk programming
o Constants
Strings are enclosed in quotes (")
Numbers are written in the usual decimal way; non-integer values are indicated by including a period (.) in the representation.
REs are delimited by /
• Variables
o Need not be declared
o May contain any type of data, their data type may change over the life of the program
o Are named as any token beginning with a letter and continuing with letters, digits and underscores
o As in C, case matters; since all the built-in variables are all uppercase, avoid this form.
o Some of the commonly used built-in variables are:
NR -- The current line's sequential number
NF -- The number of fields in the current line
FS -- The input field separator; defaults to whitespace and is reset by the -F command line parameter
• Fields
o Each record is separated into fields named $1, $2, etc
o $0 is the entire record
o NF contains the number of fields in the current line
o FS contains the field separator RE; it defaults to the white space RE, /[
o Fields may be accessed either by $n or by $var where var contains a value between 0 and NF
• print/printf
o print prints each of the values of $1 through $NF separated by OFS then prints a \n onto stdout; the default value of OFS is a blank
o print value value ... prints the value(s) in order and then puts out a \n onto stdout;
o printf(format,value,value,...) prints the value(s) using the format supplied onto stdout, just like C. There is no default \n for each printf so multiples can be used to build a line. There must be as many values in the list as there are item descriptors in format.
o Values in print or printf may be constants, variables, or expressions in any order
• Operators - awk has many of the same operators as C, excepting the bit operators. It also adds some text processing operators.
• Built-in functions
o substr(s,p,l) -- The substring of s starting at p and continuing for l characters
o index(s1,s2) -- The first location of s2 within s1; 0 if not found
o length(e) -- The length of e, converted to character string if necessary, in bytes
o sin, cos, tan -- Standard C trig functions
o atan2(x,y) -- Standard quadrant oriented arctangent function
o exp, log -- Standard C exponential functions
o srand(s), rand() -- Random number seed and access functions
• Elementary examples and uses
o length($0)>72 -- print all of the lines whose length exceeds 72 bytes
o {$2="";print} -- remove the second field from each line
o {print $2} -- print only the second field of each line
o /Ucast/{print $1 "=" $NF} -- for each line which contains the string 'Ucast' print the first variable, an equal sign and the last variable (awk code to create awk code; a common trick)
o BEGIN{FS="/"};NF<4 -- using '/' as a field separator, print only those records with less than four fields; when applied to the output of du, gives a two level summary o {n++;t+=$4};END{print n " " t} -- when applied to the output of an ls -l command provides a count and total size of the listed files; I use it as part of an alias for dir. Depending on your flavor of UNIX, the $4 may need to be changed to $5. o $1==prv{ct++;next}{printf("%8d %s",ct,prv);ct=1;pr v=$0} -- prints each unique record with a count of the number of occurrences of it; presumes input is sorted • Advanced awk programming o Program structure (if, for, while, etc.) if(boolean) statement1 else statement2 if the boolean expression evaluates to true execute statement1, otherwise execute statement 2 for(v=init;boolean;v change) statement Standard C for loop, assigns v the value of init then while the boolean expression is true executes the statement then the v change for(v in array) statement Assigns to v each of the values of the subscripts of array, not in any particular order, then executes statement while(boolean) statement While the boolean expression is true, execute the statement do statement while(boolean) execute statement, evaluate the boolean expression and if true, repeat statement in any of the above constructs may be either a simple statement or a series of statements enclosed in {}, again like C; a further requirement is that the opening { must be on the line with the beginning keyword (if, for, while, do) either physically or logically via \ . break -- exit from an enclosing for or while loop continue -- restart the enclosing for or while loop from the top next -- stop processing the current record, read the next record and begin processing with the first command exit -- terminate all input processing and, if present, execute the END command • Arrays o There are two types of arrays in awk - standard and generalized o Standard arrays take the usual integer subscripts, starting at 0 and going up; multidimensional arrays are allowed and behave as expected o Generalized arrays take any type of variable(s) as subscripts, but the subscript(s) are treated as one long string expression. o The use of for(a in x) on a generalized array will return all of the valid subscripts in some order, not necessarily the one you wished. o The subscript separator is called SUBSEP and has a default value of comma (,) o Elements can be deleted from an array via the delete(array[subscript]) statement • Built-in variables o FILENAME -- The name of the file currently being processed o OFS -- Output Field Separator default ' ' o RS -- Input Record Separator default \n o ORS -- Output Record Separator default \n o FNR -- Current line's number with respect to the current file o OFMT -- Output format for printed numbers default %.6g o RSTART -- The location of the data matched using the match built-in function o RLENGTH -- The length of the data matched using the match built-in function • Built-in functions o gsub(re,sub,str) -- replace, in str, each occurrence of the regular expression re with sub; return the number of substitutions performed o int(expr) -- return the value of expr with all fractional parts removed o match(str,re) -- return the location in str where the regular expression re occurs and set RSTART and RLENGTH; if re is not found return 0 o split(str,arrname,sep) -- split str into pieces using sep as the separator and assign the pieces in order to the elements from 1 up of arrname; use FS if sep is not given o sprintf(format,value,value,...) -- write the values, as the format indicates, into a string and return that string o sub(re,sub,str) -- replace, in str, the first occurrence of the regular expression re with sub; return 1 if successful, 0 otherwise o system(command) -- pass command to the local operating system to execute and return the exit status code returned by the operating system o tolower(str) -- return a string similar to str with all capital letters changed to lower case • Other file I/O o print and printf may have > (or >>) filename or | command appended and the output will be sent to the named file or command; once a file is opened, it remains open until explicitly closed
o getline var < filename will read the next line from filename into var. Again, once a file is opened, it remains so until it is explicitly closed o close(filename) explicitly closes the file named by the filename expression • Writing your own functions o A function begins with a function header of the form: function name(argument(s), localvar(s)) { o and ends with the matching } o The value of the function is returned via a statement of the form: return value o Functions do not have to return a value and the value returned by a function (either built-in or written locally) may be ignored by just placing the function with its arguments as a whole, separate statement o The local variables indicated in the localvars of the heading replace the global variables of the same name until the function completes, at which time the globals are restored o Functions may have side effects such as updating global variables, doing I/O or running other functions with side effects; beware the frumious bandersnatch • Advanced examples and uses { split($1,t,":") $1 = (t[1]*60+t[2])*60+t[3] print } ________________________________________ Replaces an HH:MM:SS time stamp in the first field with a seconds since midnight value which can be more easily plotted, computed with, etc. ________________________________________ ________________________________________ { for(i = 1; i<=NF; i++) ct[$i] += 1 } END { for(w in ct) { printf("%6d %s",ct[w],w) } } ________________________________________ This reads a file of text and creates a file containing each unique word along with the number of occurrences of the word in the text. ________________________________________ ________________________________________ NR=1 { t0=$1; tp = $1; for(i=1;i<=nv;i++) dp[i] = $(I+1);next} { dt=$1-tp; tp = $1 printf("%d ",$1-t0) for(i=1;i<=nv;i++) { printf("%d ",($(I+1)-dp[i])/dt) dp[i] = $(i+1) } printf("\n") } ________________________________________ Take a set of time stamped data and convert the data from absolute time and counts to relative time and average counts. The data is presumed to be all amenable to treatment as integers. If not, formats better the %d must be used. ________________________________________ ________________________________________ BEGIN{ printf("set term postscript\n") > "plots"
printf("set output '|lpr -Php'\n") > "plots" }
{ if(system("test -s " $1 ".r") {
print "process1 " $1 ".r " $2
printf("plot '%s.data' using 2:5 title '%s'",\
$1,$3) >> "plots"
}
}
END { print "gnuplot < plots" } ________________________________________ Write a pair of set lines to a file called plots. For each input line, if a file whose name is the first field on the line with a .r appended exists, write a command to the stdout file containing the file name and the second field from the line; also write a plot statement to a file called plots using the third field from the input line. After the file has been processed, add a gnuplot command to the stdout file. If all of the output is passed to sh or csh through a pipe, the commands will be executed. ________________________________________ ________________________________________ BEGIN { l[1]=25; l[2]=20; l[3]=50 } /^[ABC]/ { I = index("ABC", substr($0,1,1)) a=$0 " " print substr(a,1,l[i]) } { print } ________________________________________ Make lines whose first characters are 'A', 'B', or 'C' have lengths of 25, 20, and 50 bytes respectively, changing no other lines. ________________________________________ ________________________________________ /^\+/ { hold = hold "\r" substr($0,2); next} { if( unfirst ) print hold hold ="" } /^1/ { hold = "\f" } /^0/ { hold = "\n" } /^-/ { hold = "\n\n" } { unfirst = 1 hold = hold + substr($0,2) } END { if(unfirst) print hold } ________________________________________ This routine will take FORTRAN-type output with leading ANSI vertical motion indicators and convert it to a stream with ASCII printer control sequences in it. ________________________________________ ________________________________________ BEGIN { b=""; if(ll==0) ll=72 } NF==0 { print b; b=""; print ""; next } { if(substr(b,length(b),1)=="-") { b=substr(b,1,length(b)-1) $0 } else b=b " " $0 while(length(b)>ll) {
i = ll
while(substr(b,i,1)=" ") I--
print substr(b,1,i-1)
b = substr(b,i+1)
}
}
END { print b; print "" }
________________________________________
This will take an arbitrary stream of text (where paragraphs are indicated by consecutive \n) and make all the lines approximately the same length. The default output line length is 72, but it may be set via a parameter on the awk command line. Both long and short lines are taken care of but extra spaces/tabs within the text are not correctly handled.
________________________________________
________________________________________
BEGIN { FS = "\t" # make tab the field separator
printf("%10s %6s %5s %s\n\n",
"COUNTRY", "AREA", "POP", "CONTINENT")
}
{ printf("%10s %6d %5d %s\n", $1, $2, $3, $4)
area = area +$2
pop = pop + $3
}
END { printf("\n%10s %6d %5d\n", "TOTAL", area, pop) }
________________________________________
This will take a variable width table of data with four tab separated fields and print it as a fixed length table with headings and totals.
________________________________________
________________________________________
• Important things which will bite you
o $1 inside the awk script is not $1 of the shell script; use variable assignment on the command line to move data from the shell to the awk script,
o Actions are within {}, not selections
o Every selection is applied to each input line after the previously selected actions have occurred; this means that a previous action can cause unexpected selections or selection misses.
Operators
" " The blank is the concatenation operator
+ - * / % All of the usual C arithmetic
operators, add, subtract, multiply,
divide and mod.
== != < <= > >= All of the usual C relational
operators, equal, not equal, less
than, less than or equal and greater
than, greater than or equal
&& || The C boolean operators and and or
= += -= *= /= %= The C assignment operators
~ !~ Matches and doesn't match
?: C conditional value operator
^ Exponentiation
++ -- Variable increment/decrement
Note the absence of the C bit operators &, |, << and >>
[s]printf format items
Format strings in the printf statement and sprintf function consist of three different type of items: literal characters, escaped literal characters and format items. Literal characters are just that: characters which will print as themselves. Escaped literal characters begin with a backslash (\) and are used to represent control characters; the common ones are: \n for new line, \t for tab and \r for return. Format items are used to describe how program variables are to be printed.
All format items begin with a percent sign (%). The next part is an optional length and precision field. The length is an integer indicating the minimum field width of the item, negative if the data is to be white spacethe left of the field. If the length field begins with a zero (0), then instead of padding the value with leading blanks, the item will be padded with leading 0s. The precision is a decimal followed by the number of decimal digits to be displayed for various floating point representations. Next is an optional source field size modifier, usually 'l' (ell). The last item is the actual source data type, commonly one of the list below:
d Integer
f Floating point in fixed point format
e Floating point invaluel format
g Floating point in "best fit" format; integer, fixed
point, or exponential; depending on exact value
s Character string
c Integer to be interpreted as a character
x Integer to be printed as hexadecimal
Examples:
%-20s Print a string in the left portion of a 20 character
field
%d Print an integer in however many spaces it takes
%6d Print an integer in at least 6 spaces; used to format
pretty output
%9ld Print a long integer in at least 9 spaces
%09ld Print a long integer in at least 9 spaces with leading
0s, not blanks
%.6f Print a float with 6 digits after the decimal and as
many before it as needed
%10.6f Print a float in a 10 space field with 6 digits after
the decimal