Skip to Content, PHP, MySQL, Javascript, Ajax, Htacces

Useful UNIX Files and Commands

Be First!
by October 26, 2012 LINUX, UNIX

Helpful Files

The following files may be useful when trying to write your PVM programs in this class.

  • The online UNIX Dictionary of over 40,000 words:
    /usr/dict/words   The UNIX dictionary
  • Some useful system files in the directory /etc or /usr/games/lib/fortune:
    /etc/passwd    The password file
    /etc/HOSTNAME  The name of the computer
    /etc/issue     The logon banner
    /usr/games/lib/fortunes/fortune   The source for UNIX fortunes

Useful Commands

Word Count, or “wc”
A helpful UNIX command is the “word count” program that can count how many words are in the file.

wc -w <filename> counts the number of words in a file
wc -l <filename>counts lines in a file.Grab Regular Expression, or “grep”
Another helpful command is “grep” for grabbing lines from a file that contain a specific string pattern, or regular expression. The command grep <string> <files> looks through a list of files and finds lines that contain the specific word in the string argument.
grep pvm_pack *.cpp   will look for occurrences of the string “pvm_pack” in all files ending in “.cpp”.
grep “My name is”   *   will look in all files in a directory trying to find the string “My name is”.

Input / Output Redirection

The UNIX operating system has a number of useful tools for allowing other programs to work with one another. One of the ways to handle screen input and output with I/O Redirection, and ways to link several programs together with “pipes”.With the use of the > for sending output to a file, a user can easily covert from screen display programs to ones that save the output without major changes in rewriting code. It is also very convenien for grabbing the output from various UNIX commands, too.

myprogram   >   myoutfile
This takes the output of “myprogram” and sends it a file called “myoutfile”. ls -alF   >   filelist
This runs the command “ls”, but saves the directory listing to a file rather than displaying it on the screen.

In order to convert a program that originally required lots of user input into one that runs on its own, the input redirection symbol < can be used to say where to get the values.

program2   <   myinput
This runs “program2” but takes any keyboard input from the file “myinput”. It is important the input values are in the proper sequence in the file “myinput” since there will not be ways to reply to prompts at the console.


The vertical bar “|” is called the pipe symbol, and it is designed for linking commands together to make them more powerful. The way it works is that the output from one command is sent as input to the next, thus creating a new command.

ls -alF   |   grep “.cpp”
This will list all files in a directory, and will then grab the names of only the ones that contain the string “.cpp” in the name, or the C++ source files.

The   system()   command in C

The system()command is actually a C function that is very valuable for accessing UNIX commands from within a C program. It can also be used to run other programs you have already written. Be careful with extensive use of this command because according to the online manual pages (man), there are a few bugs such as not being able to break out of infinite loops because interrupts are not processed, and some other security issues. For many of the things we will be doing in this class, though, this command will be quite useful.

system( “ls – alF”);
This will run the “ls” command from within a C program and display the results to the screen.

system ( “ps -aux | grep dhyatt > outfile”);
This will run the “ps” command, the will send that output to “grep” which will look for occurrences of “dhyatt”, and finally will print the results to a file called “outfile” rather than displaying anything on the screen.



Leave a Reply