FILE *fp; char ch;
if ((ch = fgetc (fp)) != EOF) ...

getcwd -- get current working directory (like pwd inside a C program) char wd[500]; getcwd (wd, sizeof(wd));
fputc 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
cat anyfile cat abc*.c > bigfile.c cat > newfile cat -n newfile
cd cd /bin cd mycpgms cd .. cd ~meyer/bin
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'
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 '\<ex' file1 # match any word (in any line) which begins with ex grep -w state file1 # find only the word "state", not embedded grep -i '^mcc' file1 # match 3 chars "mcc" at beginning of line, # but ignore case distinctions grep -q xyz file1 # quiet mode, used when grep is in shell script
egrep '(ab)+' file1 egrep 'ab+|cd*' file1
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
man ls # display the manual page for 'ls' man -k fprintf # find out any info about fprintf
ps ps -aug ps -vax # on niktow ps -vx # shows % CPU and MEM of all process
rm MyFile.c rm * rm * .?* rm -rf *
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
long now = time(0); /* if you forget the 0, you will get core dump*/
tr \$ \* < MyCode1 > MyCode2 # This translates all $ to * tr a-z A-Z < file1 > file2 # This converts all lowercase letters # in file1 to uppercase tr '\225' '*' < file1 > file2 # convert octal 95 chars to *
wc MyDocument wc -l mydoc # output only # of lines wc -w mydoc wc -c mydoc
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 %s/.$// gets rid of the final control-m at the ends of lines
as -o ObjectFile.o AssemblyFile.a
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 Also see ~meyer/NOTES/awk for longer examples...
basename /mnt1/dept/meyer basename mypgms.tar.Z .Z
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/\<can\>/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 to A, b to B, c to 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/>/\&gt;/g' file.html # replace > with &gt; in an html file sed 's/99/17/' file.html > z # replace all 99's with 17
sleep (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
tee grep -v xyz somefile | tee save.no.xyz | lpr -Pqms
test 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 . -type f -exec grep what \{\} \; # find what in all files in here rec. 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
alias (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 \!* '
for (This works for the Bourne Shell only) for i in * do /bin/mv $i $i.Z done if [ \*.c = *.c ]; then echo "No files to process" else echo " " > z for k in *.c do \ wc $k cat $k >> z done fi 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 (This only works in CSH)
read(fd, buf, n) ==> returns 0 on EOF otherwise returns number of bytes read in
write(fd, buf, n)
open int fd = open("name", O_RDONLY); int fd = open("name", O_WRONLY | O_CREAT); On error, returns -1.
if 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
strchr #include <stdio.h> #include <string.h> 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); }
while 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!
usleep main() { printf ("hi there\n"); usleep ((int)(.2 * 1000000)); printf ("hi again\n"); }
gdb -nw (gdb) file a.exe You must compile with -g of course.