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 Section 6.1 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 Section 5.3 on 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 look for 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 Section 5.4.2. 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 http://unix.about.com/library/weekly/aa121800a.htm?once=true&.

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.1. Using the command line

If you are familiar with Unix command-line arguments, you may prefer to use the command line to manipulate files. To use the command line, open an Emacs command shell using M-x shell. The following list shows some basic file manipulation commands and what they do:

cp file1 file2

Copies file1 to a new file named file2.

mv source target

Moves a file from the source directory to the target directory.

rm file

Deletes a file.

mkdir directoryname

Creates a directory with the name directoryname.

rmdir directoryname

Deletes the directory directoryname if it is empty.

ls

Lists all the files in the current directory.

You can find more information on any of these commands (most can be run with various options that allow for more user control) by looking them up in the online man pages. Since the list presented here covers only the most basic commands, you may want to consult the Linux user's guide, available at http://www.ibiblio.org/pub/Linux/docs/linux-doc-project/users-guide/, for more information.

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:

press the Returnkey

Pressing Return when the focus is on a particular file opens that file in the appropriate major mode.

d

Marks a single file for deletion, but doesn't actually delete the file. You can mark multiple files for deletion, then delete them all simultaneously.

u

If a file is marked for deletion, this option unmarks it.

x

Deletes all files marked for deletion. You will be prompted to make sure you want to delete the files in question; type yes or no as appropriate.

C

Copies a file. You will be asked for the name of the new file to create when you copy the old file.

R

Renames a file. You will be prompted for the name of the new file to create when you rename the file.

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.