GNU Emacs
Emacs
Dashboard

18.11 Copying, Naming and Renaming Files

Emacs has several commands for copying, naming, and renaming files. All of them read two file names, old (or target) and new, using the minibuffer, and then copy or adjust a file’s name accordingly; they do not accept wildcard file names.

In all these commands, if the argument new is just a directory name (see Directory Names in the Emacs Lisp Reference Manual), the real new name is in that directory, with the same non-directory component as old. For example, the command M-x rename-file RET ~/foo RET /tmp/ RET renames ~/foo to /tmp/foo. On GNU and other POSIX-like systems, directory names end in ‘ /’.

All these commands ask for confirmation when the new file name already exists.

M-x copy-file copies the contents of the file old to the file new.

M-x copy-directory copies directories, similar to the cp -r shell command. If new is a directory name, it creates a copy of the old directory and puts it in new. Otherwise it copies all the contents of old into a new directory named new. If copy-directory-create-symlink is non- nil and old is a symbolic link, this command will copy the symbolic link. If nil, this command will follow the link and copy the contents instead. (This is the default.)

M-x rename-file renames file old as new. If the file name new already exists, you must confirm with yes or renaming is not done; this is because renaming causes the old meaning of the name new to be lost. If old and new are on different file systems, the file old is copied and deleted.

If a file is under version control (see Version Control), you should rename it using M-x vc-rename-file instead of M-x rename-file. See Deleting and Renaming Version-Controlled Files.

M-x add-name-to-file adds an additional name to an existing file without removing the old name. The new name is created as a hard link to the existing file. The new name must belong on the same file system that the file is on. On MS-Windows, this command works only if the file resides in an NTFS file system. On MS-DOS, and some remote system types, it works by copying the file.

M-x make-symbolic-link creates a symbolic link named new, which points at target. The effect is that future attempts to open file new will refer to whatever file is named target at the time the opening is done, or will get an error if the name target is nonexistent at that time. This command does not expand the argument target, so that it allows you to specify a relative name as the target of the link. However, this command does expand leading ‘ ~’ in target so that you can easily specify home directories, and strips leading ‘ /:’ so that you can specify relative names beginning with literal ‘ ~’ or ‘ /:’. See Quoted File Names. On MS-Windows, this command works only on MS Windows Vista and later. When new is remote, it works depending on the system type.