Quick reference for
Unix command and system calls
FILE *fp; char ch;
if ((ch = fgetc (fp)) != EOF) ...
FILE *fp; char ch;
fputc (ch, fp);
fgets (line, n, stream)
char *line;
int n; /* max size of line */
FILE *stream; /* opened file */
This returns NULL when EOF is reached.
if (fgets (line, 100, fp) != NULL) ... /* not EOF yet */
cal 1992
cal 1 1700
cal anyfile
cal abc*.c > bigfile.c
cal > newfile
cc hello.c
chmod 0755 mycpgms
chmod 0744 hello.c
chmod 0700 hello.c
chmod 0400 hello.c
cmp file1 file2
cmp -s file1 file2 # only gives exit code
if ( { cmp -s file1 fil2 } ) then # {} switches meaning of 0, 1
echo Files are identical
else
echo Files are different
endif
cp thisfile.c thatfile.c
cp -r somedir newdir # copies a directory and all its contents
diff file1.c file2.c
echo "Hello there" # displays 'Hello there' on the screen
echo -n 'no newline'
finger # displays all users on the same system as you
finger @hostname # display all users on system hostname
finger meyer@hostname # display information about the last time
# user meyer was on system hostname
grep "Mark" MyFile.c YourFile.c # the double quotes are not needed
grep printf * # try to find 'printf' in any file in this dir.
grep -n printf * # print the line number in file where found
grep -v 'no current' somefile
ls -l | grep ^d # print only the directories
ls -l | grep -v ^d # print only the ordinary files
grep 'chapter[0-9]*' file1 # match any of chapter0, chapter1, etc.
grep '\-s' file1 # match the string -s
grep '\
egrep '(ab)+' file1
egrep 'ab+|cd*' file1
look sys # displays all words in the system
# dictionary which begin with 'sys'
look dis | grep 'ion$' # prints out words beginning with dis, ending with ion
ls
ls *.c
ls -a
ls -l
ls -RC
ls -ld
ls -F
ls -i
ls -r
ls -s
ls -d .*
ls -RC ~ # Recursive and Columned
mail # read your own mail
mail meyer # compose and send a message to meyer
mail meyer < somefile # send the contents of "somefile" to meyer
mail -s "come to my office at once" joe # sends quick message, subject only
man ls # display the manual page for 'ls'
man -k fprintf # find out any info about fprintf
mv MyFile.c YourFile.c # renames MyFile.c as YourFile.c
mv MyDir MyOtherDir # renames MyDir as MyOtherDir
mv *.c CPGMS # move all C programs in the current
# directory into the directory CPGMS
ps -aug
ps -vax # on niktow
ps -vx # shows % CPU and MEM of all process
ps -lA # on SGI
rm MyFile.c
rm *
rm * .?*
rm -rf *
script
script MySession # start up a script file called MySession
sort UnSorted1 UnSorted2 > newsorted
sort -m sorted1 sorted2 > newsorted # merge already sorted files
sort UnSorted1 -o newsorted # output file
sort +0f +0 somefile # +0 means entire line, f means treat uppercase
# as lowercase, +0 then sortes consecutive lines
# whosefirst sort keys are equal
sort +0 -1 somefile # sort based on 1st field
sort +1 -2 somefile # sort based on 2nd field
sort -r +4 somefile # sort in reverse order from 5th field to end of
# line
sort -n +3 somefile # sort based on numeric value of field 4
sort -t: +2 -5 somefile # fields are separated by colons
sort -c somefile # just check to see if already sorted
sort -u somefile # squish all duplicate lines down to 1
time cat MyFile.c
tr \$ \* < MyCode1 > MyCode2 # This translates all $ to *
tr a-z A-Z < file1 > file2 # This converts all lowercase letters
# in file1 to uppercase
which ls
ls -l `which rm`
vi MyNewFile.c
vi *.c
vi +57 prog.c # start at line 57
vi +/sub17 prog.c # start at line where "sub17" is found
vi -R prog.c # edit in read-only mode, cannot make changes
as -o ObjectFile.o AssmeblyFile.a
Also see ~meyer/NOTES/awk for longer examples...
awk 'NR==14, NR==30' file select lines 14 through 30 of file
awk '/fleece/' file like "grep", find string "fleece"
awk '{print $2 ":" $1}' file rearrange fields 1 and 2 and put colon in
awk '/BEGIN/,/END/' file all lines between BEGIN and END lines
awk 'END{print NR}' file print number of lines in file
awk -F: '{print $3}' file print 3rd field from each line, field
separator is the colon
awk '{printf "%3d. %s\n", NR, $0}' file This prints out leading line #
awk '/#include/{printf "/* %s */\n", $0; next} \
{print}' file.c comment out only #include statements
basename /mnt1/dept/meyer
basename mypgms.tar.Z .Z
csh
csh -f # do not load .cshrc
ln MyFile.c LinkFile.c # ordinary link
ln -s file1 file2 # symbolic link
sed 's/$/ /' file1 # append blanks to each line
sed 's/^/ /' file1 # prepend blanks to each line
sed 's/can/should/g' file1 # replace all strings "can" by "should"
sed 's/\/should/g' file1 # replace all words "can" by "should"
sed 's/Unix/UNIX/g ; s/ucb/UCB/g' file1
sed '/^$/d' file1 # remove all blank lines
sed '/^[ ]*$/d' file1 # remove lines having only blanks and tabs
# (there's a blank and a tab inside the
# brackets)
sed -n '12,20p' file1 # save only lines 12 through 20 inclusive
sed 'y/abc/ABC/' file1 # translates a->A, b->B, c->C in all lines
sed '/^\.TS/,/^\.TE/!d' file1 # extract (keep) all tables sections
# ! reverse the sense of the address expression
sed -f directives file1 # sed directives stored in file "directives"
sed '/^.code/d' file1 # deletes all lines that start with .code
sed 's/>/\>/g' file.html # replace > with > in an html file
sh
sh -v scriptfile # verbose mode, i.e. echo commands before doing
(sleep 720; ls) # do nothing for 12 minutes, then list
# the current directory
tar cvf mydir.tar mydir # make a new tar file (mydir.tar) from the
# contents of directory "mydir"
tar tf mydir.tar # list contents of file mydir.tar
tar xvf mydir.tar # extract everything from mydir.tar
tar xf mydir.tar Petroleum # extract file Petroleum from mydir.tar
tar cvf mydir.tar mydir;compress mydir.tar # you often compress a tar file!
zcat huge.tar.Z | tar tf - | less # easy way to get a directory without
# uncompressing a HUGE tar file
zcat huge.tar.Z | tar xf - precious.c # pulls the file precious.c out of a
# HUGE tar file
grep -v xyz somefile | tee save.no.xyz | lpr -Pqms
Returns the truth value of the supplied expression: zero for true,
non-zero for false. In many cases, the expression must be preceded by an
option flag. (see man page)
EXAMPLE: test -f MyFile.c # test to see if MyFile.c is a
# regular file
find / -name csh -print # find file named "csh" anywhere
find . -name '*.c' -print # find all files ending in ".c"
find ~ -name a.out -print # find all a.out files
find ~ -name core -exec /bin/rm \{\} \; # find all core files and remove
find ~ -type f -exec chmod 0444 \{\} \; # make all plain files public
find ~ -type d -exec chmod 0755 \{\} \; # make all directories public
find ~ \( -name a.out -o -name '*.o' \) -atime +14 -exec /bin/rm \{\} \;
# get rid of all files a.out or
# ending in .o which haven't been
# accessed in 14 days
find ~ -name a.out -atime +7 -print # print all a.out's unused in last week
find ~ -size 500000c -print # print names of files that are bigger than 500,000 characters
expand filewithtabs > newfile
unexpand newfile > filewithtabs
(Put these into your .cshrc file)
alias ls "'ls' -F"
alias rm '/bin/mv \!* $HOME/TRASH'
alias unrm '/bin/mv $HOME/TRASH/\!* .'
alias RM '/bin/rm \!* '
alias Rm '/bin/rm \!* '
(This works for the Bourne Shell only)
for i in *
do /bin/mv $i $i.Z
done
See "foreach" if you want to know about for loops in Csh.
foreach i (*)
wc $i
end
foreach word (`cat wordfile`)
look $word
end
foreach word (*.GIF)
mv $word `basename $word .GIF`.gif
end
read(fd, buf, n) ==> returns 0 on EOF
otherwise returns number of bytes read in
write(fd, buf, n)
int fd = open("name", O_RDONLY);
int fd = open("name", O_WRONLY | O_CREAT);
On error, returns -1.
Cshell: Bourne shell:
if ($#argv > 0) then if test $# -lt 2
... then echo "hi there"
endif else echo "you're ok"
fi
zcat huge.tar.Z | tar tf - | less # easy way to get a directory without
# uncompressing a HUGE tar file
zcat huge.tar.Z | tar xf - precious.c # pulls the file precious.c out of a
# HUGE tar file
qms filename -- lots of options, do qms -help to see them.
sp filename
t xxx.l -- print a troff document, default is Sparcprinter
t -qms xxx.l -- prints on QMS
t xxx.l 8-10 -- prints only pages 8 through 10
printman rm -- print a standard man page on the Sparcprinter
printman2 xxx.l -- print your own man pages on the Sparcprinter
#include
#include
main()
{
char bigstring[100];
char *p;
int n;
strcpy (bigstring, "Mark:Meyer is here!");
p = strchr (bigstring, ':');
n = p-bigstring;
printf ("n=%d\n", n);
printf ("%s", p);
}
To do something repeatedly from a shell, for example to do ps -v over and
over, you can enter a while loop interactively. Just type "while 1" and
the csh gives you a ? prompt.
% while 1
? clear
? ps -v
? sleep 5
? end
(Now the Cshell will do your tiny script.)
Here's an example of arithmetic in a csh script:
set n1=1
set n2=5
set n=$n1
while ($n < $n2)
echo $n
@ n++
end
The space MUST come between @ and n++ or else!
main()
{
printf ("hi there\n");
usleep ((int)(.2 * 1000000));
printf ("hi again\n");
}