In a slightly more complex example, you might have a command with more than one file as an argument. For example, you might want to make a new file out of the sorted phone list.
Move the cursor to the phone file, then type: !
Emacs asks what command you want to run (Mac OS X).
Now tell Emacs you want to sort your phone file and put the output in a new file called phonesorted. The cursor is on the phone file, so you don't need to type its name in the command. Substitute an asterisk (*) for the name of the file:
Type: sort * > phonesorted
The operating system sorts the phone file and puts the output into the new file phonesorted (Mac OS X).
We created the file, but it doesn't appear on the display, which is not automatically updated in this case. To see the phonesorted file, type g.
Type: g
Emacs updates the Dired display, showing the file phonesorted (Mac OS X).
Dired is frankly inconsistent about whether you type g before the display is updated. Some commands, as we'll see shortly, update the display immediately. Others, such as running shell commands on files, do not (Emacs really doesn't know what shell commands it's running or their effect on the display). A good rule of thumb is to type g if you don't see what you expect to see.
5.2.6 Working with Groups of Files
So far we've talked about working with one file at a time; any commands you give apply to the file the cursor is on. Working with multiple files is a better illustration of the real power of Dired. You can organize your directories in a flash once you learn a few shortcuts. First let's talk about some ways to select files, and then we'll talk about what we can do with the selected files.
5.2.6.1 Selecting files
So far we've primarily talked about flagging files for deletion. When you want to do something else with a group of files, you first mark them with an asterisk. Pressing m marks the file the cursor is on; an asterisk appears where you normally see a D. Typing 3m marks this file and the next two files. Once you mark files with an asterisk, Emacs assumes that any command you issue is meant for these files. So if you have three files marked with an asterisk and press Z to compress, Emacs assumes you want to compress those three files. After the compression, the files remain marked with asterisks. So how do you get rid of the asterisks when you're done with these files?
To remove the asterisks, you press M-Del (for dired-unmark-all-files). Emacs asks which marks to remove. Press Enter, and Emacs removes all the marks.
Sometimes it's easier to mark the files you don't want to work with than those you do. Pressing t toggles the marks, marking all unmarked files and removing marks from those previously marked.
5.2.6.2 Selecting likely candidates for deletion
Marking files sequentially is simple but, in all honesty, it's not very powerful. Emacs provides commands for selecting types of files that you often want to get rid of when you're cleaning up a directory: backup files, auto-save files, and so-called garbage files.
Auto-save files are created when a session terminates abnormally; they have the format #filename#. Backup files which Emacs creates periodically, have the format filename~. To mark these files in Dired, type # or ~ respectively.
Emacs also has an option that automatically selects "garbage" files. By default, this includes files with the following extensions: .log, .toc, .dvi, .bak, .orig, and .rej. Garbage files are defined by a regular expression, which is contained in the variable dired-garbage-files-regexp; you can change the value of this variable to define garbage files as you see fit (after all, one man's junk is another man's treasure).
5.2.6.3 Selecting files by type
Dired provides commands for selecting executable files, directories, and symbolic links. To select executable files, type * *. To select directories, type * /. Typing * @ marks symbolic links.
5.2.6.4 Using regular expressions to choose files
Often you want to select related files and either archive them, move them, compress them, or just delete them. Typically, you use wildcards to select multiple files. In Dired, you use regular expressions. To mark a group of files whose filenames match a regular expression, press % followed by m to mark them with an asterisk.
For example, let's mark all the files that start with ch. Remembering the quick lesson on regular expressions from Chapter 3, ^ finds the beginning of a word, so the regular expression ^ch would mark all the files that start with ch.
Type: %m
Emacs asks for a regular expression so that it can mark the files (Windows).
Type: ^ch Enter
Emacs marks all the files starting with ch and tells you how many it marked.
Sometimes it's more useful to mark files whose contents match a given regular expression. To mark files that contain a certain regular expression, type % g, followed by the regular expression to match (think g for grep if you're familiar with grep).
Now that we've got the files marked, let's talk about what to do with them.
5.2.6.5 Operating on groups of files
In the course of daily work, a directory can get cluttered with many different kinds of files. Eventually, you need to make subdirectories to organize the files by project, then move the files to those subdirectories. You can do both these things from within Dired.
Let's say that the ch files are chapters from a novel you work on in your spare time. We need a subdirectory called novel to store the files in. You can create a directory by typing + (for dired-create-directory).
Type: +
Emacs asks for a directory name (Windows).