49

Whenever I edit files on emacs, it seems a temporary file is created with the same name with ~ appended to it. Does anyone know an quick/easy way to delete all of these files in the working directory?

1
  • 1
    Why don't you add them to your .gitignore file if you are using git? I think you can do it similarly in other VCS too. Commented Aug 12, 2017 at 10:45

8 Answers 8

118

While all the others answers here correctly explain how to remove the files, you ought to understand what's going on. Those files ending in ~ are backup files, automatically created by Emacs. They can be useful sometimes. If you're annoyed by the files and want to delete them every time, then you either

(1). prevent the creation of backup files:

(setq make-backup-files nil)

or

(2). Have it save the backup files in some other directory, where they won't bother you unless you go looking for them. I have the following in my .emacs:

(setq backup-directory-alist '(("." . "~/.emacs.d/backup"))
  backup-by-copying t    ; Don't delink hardlinks
  version-control t      ; Use version numbers on backups
  delete-old-versions t  ; Automatically delete excess backups
  kept-new-versions 20   ; how many of the newest versions to keep
  kept-old-versions 5    ; and how many of the old
  )

(Only the first line is crucial.) To see documentation about backup-directory-alist, type C-h v backup-directory-alist.

7
  • How is the above command supposed to be entered? neither the shell nor Meta-x seem to work for me... Commented Aug 20, 2012 at 21:36
  • 1
    @OcastaEshu: This is supposed to be put in your .emacs config file. (On Unix, it's in your home directory; I'm not sure where the file is on Windows.) Alternatively (and non-permanently), you can put it in any empty buffer and do M-x eval-buffer, or else type M-x eval-expression and type/paste the above. Commented Aug 21, 2012 at 2:58
  • 2
    You can also made all this changes through M-x -> customize-group -> backup Commented Oct 30, 2013 at 23:44
  • Is there a way to expand the ~/.emacs/backup to something like ~/.emacs/cwd/backup, that is, separate the backup files by including the directory they came from?
    – Don
    Commented Dec 11, 2014 at 15:39
  • @Don: I'm sure there's a way, but why? You'll almost never have to go look in the directory, so why bother? The names already have the full path in them, so files from the same directory stay together. Commented Dec 13, 2014 at 16:50
32
find . -name '*~' -exec rm {} \;

EDIT: Huh ... while this works, I posted it thinking rm *~ would cause the shell to interpolate ~ into the user's home dir. It doesn't, at least with the version of bash on this machine - YMMV, of course.

Some versions of find have a -delete option:

find . -name '*~' -delete
3
  • The second one worked fine on my mac on Mountain Lion. thanks.
    – pjammer
    Commented Feb 27, 2013 at 16:26
  • how do i do it recursively in sub-directories
    – vijayst
    Commented Jun 30, 2017 at 15:53
  • 1
    @vijayst that's what this command actually does... it traverses subdirectories starting with the current working directory, . Commented Jan 21, 2021 at 21:21
9

You can just

rm *\~

More usefully, you can change the emacs backup directory so all those files are stored in a common location, by adding this to your .emacs:

'(backup-directory-alist (quote (("." . "/common/backup/path"))))

There are other options you can fiddle with

0
7

From the working directory:

$ rm *~

From everywhere:

$ cd; find . -name '*~' | xargs rm -f

From within emacs, using dired.

C-x C-f . RET ~ x y e s RET

You can suppress backup file creation permanently by adding the following line to your ~/.emacs

(setq make-backup-files nil)

I don't recommend this last one, as emacs's backup files have saved me many times over the years.

0
5
rm -rf *~ 
2
  • this does not handle subdirectories
    – user221209
    Commented Apr 21, 2010 at 6:22
  • 1
    You can invoke the above command from within Emacs by typing M-! first.
    – offby1
    Commented Apr 21, 2010 at 15:18
5

You can open the directory in emacs, flag all backup file with ~ then delete them with x

4

In eamcs dired mode:

d Flag this file for deletion.

u Remove deletion flag on this line.

DEL Move point to previous line and remove the deletion flag on that line.

x Delete the files that are flagged for deletion.

# : Flag all auto-save files (files whose names start and end with `#') for deletion

~ : Flag all backup files (files whose names end with `~') for deletion

& : Flag for deletion all files with certain kinds of names, names that suggest you could easily create the files again

. : Flag excess numeric backup files for deletion. The oldest and newest few backup files of any one file are exempt; the middle ones are flagged.

% d regexp RET : Flag for deletion all files whose names match the regular expression regexp.

1
  • It makes sense to remove these files using the software that creates it. I don't like the rm approach, that is error prone.
    – smonff
    Commented Sep 18, 2015 at 10:33
0

I prefer to use the command utility fd (https://github.com/sharkdp/fd) to do this.

This command will delete all files ending with ~ in the current directory, excluding files in .emacs.d/backups/. Run the command without -X rm first to see which files would be deleted.

fd --glob "*~" -tf -u -E .emacs.d/backups/ -X rm
  • --glob "*~" find everything ending with ~
  • -tf everything that is of type file
  • -u search hidden folders
  • -E .emacs.d/backups/ don't search inside of .emacs.d/backups/
  • -X rm with each search result execute rm, and thus delete it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.