Working With Text Files

There are, obviously, a lot of things you can do with files. And there are many types of files, but in their essence, they really boil down to two kinds: text and binary. When it comes to text files, you'll often want to see the contents of those files, and there are a number of options.

The following sections deal with commands you can execute on text files.

TODO — not done

Editing Files

If you're using Ubuntu, you're probably doing most of your work using the Graphical User Interface, so if you want to edit a file, the most common option will be to open the file in your favourite text editor. I use gedit, so I would simply use the gedit command to open a file. For example, many pages on this site discuss editing the /etc/X11/xorg.conf file, which I do by entering the following command:

$ gedit /etc/X11/xorg.conf

This would launch the gedit text editor, which would open up the specified text file. In this case, because I don't have permissions to edit that file gedit would open it read-only; I'd be able to see its contents, but not make any changes to the file. (See the security page for information on the sudo command, which I would use to get permission to change the file.)

If you don't have access to — or don't want to use — a graphical text editor, you can always use vi to edit a text file.

$ vi /etc/X11/xorg.conf

Some people love vi, and some people hate it. However, it's been around for decades, so many, many people know how to use it. For more information, see the vi page.

Viewing Files

However, if I only wanted to see the contents of the file — not edit it — I could also use some command-line tools, and look at the file right from the command line. Probably the most common is the more command; which will display the file's contents, one screen at a time. Pressing the spacebar will allow you to see the next screen of contents.

The more command is very common, because it's been around since the early Unix days. However, there is a more advanced command, called less. This command gives you more control over how you view the file; for example, you can scroll back and forth, one line at a time, by using the up and down arrow keys, or one screen at a time, by using the PageUp and PageDown keys.

Other commands

TODO

  • diff
  • grep — probably should go in its own page
  • cat
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.