Below are a few useful commands for dealing with text files in Linux from the command line.
Displaying Text Files
more – More
Displays text files one page at a time in the shell screen. This program is particularly useful for viewing small README files and other small text files which you do not need to use for editing. A somewhat more powerful program “less” is also available and has similar syntax. To advance a page, press the space bar. To exit either program, type ‘q’.
more [filename] less [filename]
Ex: To view the contents of README.TXT
$ more README.TXT $ less README.TXT
tail – Tail
Displays the last 10 lines of the file that is passed to it. Most commonly used to view the most recent entries to log files or other files when the user is most concerned about the end of a text document.
Ex: To view the last 10 lines of the httpd error log
cat – Concatenate
Concatenates a file or group of files and prints them on the standard output. Most commonly, cat can be used for printing a single file to the screen for reading. This program is best used with very short text files which can be viewed in one screen or less.
Ex: To view the contents of README.TXT
$ cat README.TXT
grep – Grep
Locates lines and files which contain a text string, or any regular expression for that matter. It can be passed either a file, directory, or output from another program. Consider similar programs like egrep and fgrep (just use the man command)
Ex: To find instances of “basedir” in httpd.conf
$ grep basedir httpd.conf
Ex: To find all files that contain “basedir” in the httpd directory (including the line that has them)
$ grep basdir httpd
Ex: To find all instances of httpd running on the system and its Process ID (more information on ps to come later)
$ ps -A | grep httpd
diff – Difference
File comparison utility that will output the difference between the two files supplied by it. Useful when comparing configuration files or debugging seemingly identical text files which appear to be behaving different.
diff [file1] [file2]
Ex: To compare httpd.conf.old to httpd.conf
$ diff httpd.conf.old httpd.conf
Editing Text Files
nano – Editor
Allows users to edit text files from the shell. Nano is a particular useful, small, and robust text editor which is simple to use from the shell. It provides basic functionality for editing scripts, configuration, and other text files directly from the shell line without the need for a gui. It is very similar to pico which used to ship with the pine mail program. Functions are basically performed through Ctrl key sequences which are listed in a rudimentary menu at the bottom of the screen.
Ex: To edit httpd.conf with nano
$ nano httpd.conf
emacs – Editor
Emacs is a more powerful text editor which, depending on the version and the monitor being used has some basic text highlighting and formatting control. Emacs can be very useful when you want to or must edit text files from the command line. Emacs has additional advanced programming feature which includ highlighting code blocks and keywords in addition to the ability to comment out large blocks of code. There are GUI versions of emacs available, typically run from xemacs, although the command line version can be very useful when running through ssh session or without a GUI launched, such as on a server.
Ex: To edit httpd.conf with emacs
$ emacs httpd.conf
vi – Editor
The tried and true editor available on most, if not all, Unix and Unix type systems. Although many people claim that it is the most efficient text editor and most powerful, it is also one of the most difficult to learn That being said, it can come in quite handy when working on a foreign machine which does not have any of the more user friendly text editors. “vi” more easily displays all characters in the text files, including codes for some non-printing characters. This can be especially useful when trying to debug formatting issues in a text file or when you have moved files across platforms, say from dos to Unix.
Some things to keep in mind. “vi” has multiple “modes” and you have to be aware of which one you are in at any given time. For instance, you are not initially dropped into a mode where you can edit the text on the screen. To begin editing the text, you must press ‘a’ which will allow you to append text to the document as well as make other changes. When not in editing mode (either initially or after hitting “escape,” you can access various functions like save, open, and quit by first typing ‘:’ followed by the usually one letter shortcut for the function.