CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" >

5. Working with files

In addition to performing common file manipulation tasks, such as moving, copying, and deleting, you'll also want to download files from the Internet, search for files in your directories, and FTP files. Some of these functions are built into Emacs, while some of these tasks require additional applications that can be downloaded.

5.1. Downloading files

Once you know the URL of the file to download, you can use the wget command from within the Emacs shell to retrieve the file. If you do not know the URL of the file to download, use the W3 application to browse the web and find the URL of the file (refer to Browsing the Internet for information on using the W3 application). Then use the command wget to download it. For more information on the wget command, refer to the wget man page.

To download a file:

  1. From inside Emacs, type M-x shell to start the shell.

  2. Since the wget command places the downloaded file into your current directory, change directories to the desired directory.

  3. When you are in the appropriate directory, type wget [url], then press Enter. Remember that you can copy and paste the URL of the file to download from the W3 web browser application.

As the file is being downloaded, one dot appears on the screen for each kilobyte of data received. The wget command lets you know when the download is complete.

Another option for downloading (or uploading) a file is via FTP. Refer to FTPing a file for more information.

5.2. Finding a file

There are several ways you can find a file. This section discusses two options.

If you know the name of the file, or at least remember part of it, you can use the find-file command:

  1. In Emacs, type M-x find-file.

    Result: You will be prompted for the name of the file in the default working directory.

  2. Type the name of the file (or use wildcards) you wish to find.

Alternatively, you may want to browse your directories to lookfor a file. You can do this using the Emacs directory editor,dired. To start dired:

  1. In Emacs, type C-x d or M-x dired. You will be prompted for the name of the file. You can type the name (or part of the name) of the file to find, using the Tab key to complete the file name. dired will list any files that match the name you entered. Alternatively, you can view a full listing of the directory by providing a directory name (or pressing Return to view the current directory) when prompted.

For more information on using the directory editor, refer to Using the directory editor. For a complete listing of dired's features and capabilities, refer to http://www.delorie.com/gnu/docs/emacs/emacs_360.html.

5.3. FTPing a file

Emacs contains a built-in file transfer protocol, or FTP,application. The FTP application can be used to upload and download files to and from other machines. In order to use FTP, you will need to know the hostname of the machine to contact.

To invoke the FTP application:

  1. Within Emacs, type M-x ftp.

    Result: You will be prompted for an ftp hostname.

Alternatively, you can start an Emacs shell using M-x shell and use FTP from the command line. There is a short tutorial on using the command line FTP at Focus on Unix.

5.4. Manipulating files

This section contains information on moving, copying, and deleting files from within Emacs. In general, there are two ways you can go about these tasks. One option is to use the command line from an Emacs shell, and the other is to use dired. Both methods are discussed in this section.

5.4.2. Using the directory editor

Although many Linux users use the command line to manipulate files, Emacs provides a powerful alternative, called dired. Dired is the Emacs directory editor, and is great for manipulating files if you'd rather not use the command line or aren't familiar with it. To start dired:

  1. In Emacs, type M-x dired. You will be prompted for the directory to open.

  2. Type in the directory to display, or press Return to open the default directory.

Once you've started dired, you can move around in the Emacs buffer to have the file names, permissions, owners, sizes, and dates created/edited read aloud.

In dired, some commands mark files for manipulation (for example, you can mark several files, then delete them all), and some commands (such as the copy command) are executed immediately. Note that, in dired, case does make a difference for many commands. Some of the more common dired commands and what they do are listed below:

Because dired is so extensive, including the full documentation for it here is not feasible. However, complete instructions can be found in the GNU Emacs Manual at http://www.delorie.com/gnu/docs/emacs/emacs_360.html.