Working with text files

Printing a textfile into the terminal window is done by using the command cat. E.g. there is a special file in the directory tree of the linux operating system that tells you about the specifications of the processor(s) in your workstation. This file is located under /proc/ and named cpuinfo. So, in order to find out about the CPUs in your computer, you can type
$ cat /proc/cpuinfo

Sometimes it is more convenient to be able to go through a text file page by page or line by line instead of printing the entire file into the terminal. Therefore the little program less exists.

$ less /proc/cpuinfo
This will display all lines of text in the file /proc/cpuinfo that fits into your current terminal window. Using the arrow keys UpArrow and DownArrow, you can go through your text line by line. The keys PageUp and PageDown will show the previous or next page of text. Pressing q will quit the less program. You can also search inside a text document by pressing \ followed by your query. This will jump to the nearest occurance of the query in your textfile. By pressing n or Shift+n you will be directed to the next/previous occurance. As you will deal with a lot of textfiles with data in the next lectures, there are many helper commands/programs that assist you to obtain certain informations. The program wc (word count) counts the number of words, lines and characters in the specified text file. The program grep will find out if a certain specified string is in your textfile. E.g. if you want to know if you have a pentium cpu in your workstation you might call
$ grep Pentium /proc/cpuinfo
If so, the programm will print all lines of the textfile where your query was found. It will print nothing, if the query is not found at all. Again, check
$ grep --help
or
$ man grep
to find out more about the possibilities you have using the grep command.

Ronny Lorenz 2010-04-06