How do I open, create, edit, and view a file in Linux?

LinuxOne thing GNU/Linux does as well as any other operating system is give you the tools you need to create and edit text files. There are many, many text editors available. Ask ten Linux users what their favorite text editor is, and you will probably get ten different answers. On this page, we cover a few of these, but there are many more available.

GUI Text Editors
Terminal-based text editors
Redirecting command output into text files

GUI Text Editors

In this section discusses text editing applications for the Linux windowing system, X Windows, more commonly known as X11 or X.

If you are coming from Microsoft Windows, you are no doubt familiar with the classic Windows text editor, Notepad. Linux offers many similar programs, including NEdit, gedit, and geany. Each of these programs are free software, and they each provide roughly the same functionality. It's up to you to decide which one feels best and has the best interface for you. All three of these programs support syntax highlighting, which comes in handy if you are using them to edit source code or documents written in a markup language such as HTML or CSS.

NEdit

The nedit text editor for Linux

NEdit, which is short for the Nirvana Editor, is a straightforward text editor that is very similar to Notepad. It uses a Motif-style interface.

The NEdit home page is located at http://sourceforge.net/projects/nedit/. If you are on a Debian or Ubuntu system, you can install NEdit with the following command:

sudo apt-get install nedit

For more information, see our NEdit information page.

Geany

The Geany text editor for Linux

Geany is a text editor that is a lot like Notepad++ for Windows. It provides a tabbed interface for working with multiple open files at once and has nifty features like displaying line numbers in the margin. It uses the GTK+ interface toolkit.

The Geany home page is located at http://www.geany.org/. On Debian and Ubuntu systems, you can install Geany by running the command:

sudo apt-get install geany

Gedit

The Gedit text editor for Linux

Gedit is the default text editor of the GNOME desktop environment. It's a great, text editor that can be used on just about any Linux system.

The Gedit home page is located at https://wiki.gnome.org/Apps/Gedit. On Debian and Ubuntu systems, Gedit can be installed by running the following command:

sudo apt-get install gedit

Terminal-based text editors

If you are working from the Linux command line interface and you need a text editor, you have many options. Here are some of the most popular:

pico

The pico text editor, running on Linux

pico started out as the editor built into the text-based email program pine, and it was eventually packaged as a stand-alone program for editing text files. ("pico" is a scientific prefix for very small things.)

The modern version of pine is called alpine, but pico is still called pico. You can find more information about how to use it on our pico command information page.

On Debian and Ubuntu Linux systems, you can install pico using the command:

sudo apt-get install alpine-pico

nano

The nano text editor, running on Linux

nano is the GNU version of pico and is essentially the same program under a different name.

On Debian and Ubuntu Linux systems, nano can be installed with the command:

sudo apt-get install nano

vim

The vim text editor, running on Linux

vim, which stands for "vi improved", is a text editor used by millions of computing professionals all over the world. Its controls are a little confusing at first, but once you get the hang of them, vim makes executing complex editing tasks fast and easy. For more information, see our in-depth vim guide.

On Debian and Ubuntu Linux systems, vim can be installed using the command:

sudo apt-get install vim

emacs

The emacs editor, running on Linux

emacs is a complex, highly customizable text editor with a built-in interpreter for the Lisp programming language. It is used religiously by some computer programmers, especially those who write computer programs in Lisp dialects such as Scheme. For more information, see our emacs information page.

On Debian and Ubuntu Linux systems, emacs can be installed using the command:

sudo apt-get install emacs

Redirecting command output into a text file

When at the Linux command line, you sometimes want to create or make changes to a text file without actually running a text editor. Here are some commands that you might find useful.

Creating an empty file with the touch command

To create an empty file, it's common to use the command touch. touch updates the atime and mtime attributes of a file as if the contents of the file had been changed — without actually changing anything. If you touch a file that doesn't exist, the system will create the file without putting any data inside.

For instance, the command:

touch myfile.txt

...will create a new, empty file called myfile.txt if that file does not already exist.

Redirecting text into a file

Sometimes you need to stick the output of a command into a file. To accomplish this quickly and easily, you can use the > symbol to redirect the output to a file.

For instance, the echo command is used to "echo" text as output. By default, this goes to the standard output — the screen. So the command:

echo "This is my text."

...will print that text on your screen and return you to the command prompt. However, you can use > to redirect this output to a file. For instance:

echo "This is my text." > myfile.txt

...will put the text "This is my text." into the file myfile.txt. If myfile.txt does not exist, it will be created. If it already exists, its contents will be overwritten, destroying the previous contents and replacing them.

WARNING: Be careful when redirecting output to a file using >. It will overwrite the previous contents of the file if it already exists! There is no undo for this operation, so make sure you want to completely replace the file's contents before you run the command.

Here's an example using another command:

ls -l > directory.txt

The above command executes the ls command with the -l option, creating a detailed listing of files in the current directory, and output the listing to the file directory.txt instead of printing it to the screen. If directory.txt does not exist, it will be created first; if it already exists, its contents will be replaced.

Redirecting to the end of a file

The redirect operator >> is similar to >, but instead of overwriting the contents of the file, it will append the new data to the end of the file. For instance, the command:

ls -l >> directory.txt

...takes the output of ls -l and add it to directory.txt. If directory.txt does not exist, it will be created first; if it already exists, the output of ls -l will be added to the end of the file, one line after whatever was already in the file.

Additional information

  • See the create, edit, and view definitions for other documents related links and information.