The Sun Observer, Volume 8 No.6
Peter V. Radatti
radatti@cyber.com
CyberSoft
June 10 1995
Notice: Copyright March 31, 1995 by Peter V. Radatti, All rights reserved.
Welcome back to a second edition of my column. Last month we reviewed that data is contained in files, file names are how you locate files, directories contain files, file systems contain directories, and how to express each of these in ways that is meaningful to the Unix operating system. We also reviewed a special type of directory called a home directory, some of the files in the home directory that control how you as a user work with the system, (dot files), and the "ls" command which allow you to view your files and directories. Finally, I told you that the all knowing and powerful Editor of The Sun Observer promised me fame, wealth, and a date with Madonna. The fame and wealth parts are under discussion but they kept their promise about Madonna. She is really a very nice women. A real home town type who's secret ambition is to be a librarian in a public library. Madonna has a Ph.D. in Philosophy so naturally we had a great time discussing Descartes, Nietzsche and Camoo while sipping expresso. Next month I meet President Clinton and Socks the Cat.
rinc% file *
report: ascii text
LICENSE: ascii text
VFind Sparc demand paged dynamically linked executable
vfind.sh commands
Figure 1
Now that you know everything about files it's time to learn how to distinguish the contents of the files and to learn ways to manipulate their contents. The command "file filename" will tell you what the "file" command thinks is the contents of the file "filename". It's correct about 70% of the time which is very high considering that there are many types of files. An example of using the "file" command is shown in figure 1.
In figure 1, we used the wild card operator, (asterix), with the "file" command to allow the command to operate on all the files in the current working directory. Four files were found and reported upon. The top two, "report" and "LICENSE", are text files. The last two, "vfind" and "vfind.sh", are executables. The key words on the file "vfind" are "sparc" and "executable". This indicates that the contents of the file "vfind" is a binary executable program. In fact, it is. The contents of the file "vfind.sh" is a shell script program and is reported as "commands text".
If I wanted to see all of the Sparc executables in my directory then there is a slight problem. The "file" command does not have an option to only report one type of file. The solution is to use a feature of the system called piping and a second command called "grep". Piping is a feature that allows the output from one command to be used as the input to a second command. It is written as a vertical bar, "|". The "grep" command reports every line it sees that contains a search string. Therefor if I pipe the output from the "file" command to the input of the "grep" command then I can search for all of the Sparc executables by asking "grep" to report every instance of the word "sparc". The command to do this is written "file * | grep sparc". Try it on your system. If you don't have any executables in your directory try changing your current home directory to "/usr/bin". Remember from our last lesson the command to change the working directory to "/usr/bin" is "cd /usr/bin". When complete just execute the "cd" command without options to return to your home directory.
I now want to know the names of all files in the "/usr/bin" directory that have copyright marks. Again the "grep" command comes into play. I can type "grep opyright /usr/bin/*" and the grep command will report every file with the string "copyright" in it. I did not search for "Copyright" because I didn't know if the first character would be capitalized. If you executed the example command then you will have noticed that the report went off the screen and was no longer readable. There is a command that was created to solve this problem. It's called "more". The more command displays data to the screen until the screen is full then asks you if you want "more". We can feed the more command with the output of the grep report example producing the command, "grep copyright /usr/bin/* |more". If you want to see one more line of data press the Return key. To display the next page of data press the space bar.
So far all of our examples have shown only two command piped together. It is possible to pipe many command together and it is also possible to attach commands to each other for execution without piping. To attach command together use the semi-colon. An good example of how to use this feature is to execute the following command from your home directory, "cd /usr/bin ;ls". The result is a display of the contents of the "/usr/bin" directory and a changing of your current working directory to "/usr/bin". If I wanted to return to my home directory when complete I would have used the example, "cd /usr/bin ;ls ;cd". After the "ls" command executes the "cd" command without options will return me to my home directory.
At this point in the article you may have realized that you can using piping and attachments together to form complex commands. If I wanted a report of all the copyrighted material in the "/usr/bin" directory, displayed to my screen one page at a time and then be returned to my home directory upon completion with verification I can use the command, "cd /usr/bin; grep opyright * |more ;cd ;pwd" or I can use, "grep opyright /usr/bin/* |more ;pwd" and never leave my home directory.
We are getting near the end of the article for this month so lets close with the "strings" command. Lets say you wanted to see all the messages that the system can display to you. If you are using Solaris change the filename in the next example from "/vmunix" to, "/kernel/unix". To display all of the messages use the command, "strings /vmunix |more". To learn if any messages mention Madonna use the command, "strings /vmunix |grep adonna |more". Again, you can string commands together in novel ways to solve different problems. For example to search for Madonna in the "/usr/bin" directory you can use the command "cd /usr/bin ;strings |grep adonna |more ;cd" or just the command "grep adonna /usr/bin/* |more ;cd". They both work but the first example lets me get paid more for this article since it uses more words.
Until next month.
Pete Radatti is the founder and CEO of CyberSoft, Inc. CyberSoft manufactures, VFind the an antivirus software product that executes under Unix and simultaneously scans for Unix, MS-DOS, Macintosh and Amiga destructive software. You can reach Pete at radatti@cyber.com.






