While working with Linux, I found myself constantly switching back and forth between my text editor (to create and edit files) and a terminal window (to use the shell commands to manage those files). Then I learned that the Emacs text editor has a built-in directory editor called Dired that lets you manage your files and directories without ever having to leave the Emacs window. Dired allows you to select a file in a directory list and perform an operation on it by entering in a command (e.g., entering c for copy or e to open a file for editing.) Whether you’re looking to learn how to use Linux yourself or needing to train your end users, this short lesson will show you how Dired directory editor works with the Emacs text editor.

Working with Dired
First, create a text file called about_emacs by entering the following at a command prompt:
emacs about_emacs

After pressing [Enter], you’ll see the Emacs text editor screen, as shown in Figure A. Note that the screen is divided into three main areas, the Edit area where text is typed, the Mode line where status information is displayed, and the Echo area, where commands are entered and prompts and messages from Emacs are displayed.

Figure A

Next, type a short sentence in the Edit area. To save the changes to the file, press [Ctrl]X, then [Ctrl]W. Enter the name of the directory you want to save your file to at the prompt in the Echo area like this:
Write file: “/home/Mary/Emacsdemo

Pressing [Enter] saves the file about_emacs to the directory Emacsdemo. Now let’s switch from the text editor to Dired. Press [Ctrl]X, then D. At the prompt in the Echo area, type the name of the directory you want to display as in the following:
Dired (directory): /home/Mary/Emacsdemo

After pressing [Enter], the Edit area will display the directory listing, as shown in Figure B.

Figure B

Returning to Edit mode from Dired
Next, you’ll need to open about_emacs from the directory list. Position the cursor on the last line within about_emacs by either pressing the N (new line) key, press the down arrow to get to the line, or simply click on the line with your mouse. Once the cursor is positioned, press the E key to open the file and return to edit mode. Now add the date to the file, as shown in Figure C.

Figure C

To save the changes, press [Ctrl]X, then [Ctrl]S.

Returning to Dired
To return to Dired, press [Ctrl]X, then press the D key. The directory list is displayed as shown in Figure D.

Figure D

As instructed in the Echo area, typing G will update the listing (Figure E).

Figure E

Note that by default Dired sorts the files in alphabetical order by name. You can also have them listed by date. Pressing the S key will sort the list by date and time, starting with the most recent file. Pressing S again will resort the list by name.

Creating a new directory
To create a new directory with Dired, press the Plus symbol key (+). You will then be prompted for the directory name in the Echo area. For this example, call the new directory backup.

After pressing [Enter] the backup directory will be added to the listing, as shown in Figure F.

Figure F

Copying files
To use Dired to copy the file about_emacs to the backup directory, first position the cursor anywhere within the filename. Then, press [Shift]C. A prompt will appear in the Echo area asking for the name of the directory where you want the file to be copied, which for this example is the backup directory (Figure G).

Figure G

To display the contents of the backup directory, position the cursor over the directory name and press E. Figure H shows the contents of this directory.

Figure H

Renaming a file
To rename a file, first position the cursor over the filename (in this example, about_emacs), and then press [Shift]R. A prompt will appear in the Echo area, asking for the new name; for this example, you would enter about_emacs_v1, as shown below:
Rename about_emacs to :/home/Mary/Emacsdemo/backup/about_emacs_vl

After pressing [Enter], the new name will appear in the listing (Figure I).

Figure I

Changing group permissions
To change a file’s group permissions, move the cursor to the filename, in this case, about_emacs_v1, and press [Shift]G. You’ll receive a prompt in the Echo area asking for the new group. Enter the name of that group at the prompt, as shown in Figure J.

Figure J

After pressing [Enter], the new group will be added to the list as shown in Figure K.

Figure K

Viewing and printing a file
Dired also lets you view a file without going back into Edit mode. To do so, position the cursor over the name of the file you want to view, and press V to display the contents of the file in read-only mode. To return to the directory listing, press Q (quit).

You can also print a file by positioning the cursor over the name of the file you want to print, and pressing [Shift]P.

Deleting a file
You can also use Dired to delete files from a directory list. First, flag each file to be deleted by positioning the cursor over the name of the file you want deleted and pressing D. (If you mark a file for deletion by mistake, pressing U will clear the D.) Three files flagged for deletion are shown in Figure L.

Figure L

Pressing X will delete the files flagged with D. Note that Dired will display a message in the Echo area asking whether you want to delete the flagged files before actually deleting them.

Executing shell commands
Dired lets you execute shell commands on any file or directory within a directory list by positioning the cursor on the file or directory (in this example, backup), and then press [Shift]1.

The Echo area will then prompt for the command you want to use; in this example, ls is used to list the contents of the backup directory to the file backuplist as shown in Figure M.

Figure M

Pressing [Enter] executes the ls command.

Quitting Dired
To get back to Edit mode from Dired, press Q. With a little practice, I’ve found working with Dired and the Emacs text editor a lot quicker and easier than having to work with two or more terminal windows to create, edit, and manage files and directories.