Delete All Backup Files Recursively In Bash

Published:
Last modified:
Tags Emacs , Vim , Bash , Backups

Overview

The most popular *nix editors, [Emacs] and Vim automatically generate backup files of each file you edit, this is because they both has the auto-save feature so you don’t lose your work if anything happens.

Vim backup files has ~ at the end of the file as it was before you edited it: foo.bar~.

Emacs backup files are surrounded by # in their filenames: #foo.bar#.

We will show the commands to remove the files of the form *~ so they should be adjusted to work with #*# files easily.

Visualize the files to remove

Removing files is always a sensitive task, you should always check what files the command you execute will remove.

To see what files the command affects we use find -name "*~" -print.

-print print the full file name on the standard output, followed by a newline.


$ find -name "*~" -print
./_includes/lang_nav.html~
./_includes/head.html~
./_includes/share_buttons.html~
./_includes/adsense_content_1.html~
./_includes/adsense_content_2.html~
./_includes/adsense_side.html~
./_sass/cached/_main.scss~
./_sass/cached/_share_buttons.scss~
./_sass/cached.scss~
./_layouts/default.html~
./index.md~
./trans/hola.md~
./trans/es.md~
./trans/index.md~
./README.md~
./assets/main.scss~
./Gemfile~
./_config.yml~

If you need further information about each file you can also specify set a custom command, like ls -l <file> with: find -name "*~" -exec ls -l {} \;


$ find -name "*~" -exec ls -l {} \;
-rw-rw-r-- 1 marcanuy marcanuy 272 nov 22 23:21 ./_includes/lang_nav.html~
-rw-rw-r-- 1 marcanuy marcanuy 1861 nov 22 23:21 ./_includes/head.html~
-rw-rw-r-- 1 marcanuy marcanuy 2063 nov 22 15:07 ./_includes/share_buttons.html~
-rw-rw-r-- 1 marcanuy marcanuy 543 nov 27 11:02 ./_includes/adsense_content_1.html~
-rw-rw-r-- 1 marcanuy marcanuy 543 nov 27 11:07 ./_includes/adsense_content_2.html~
-rw-rw-r-- 1 marcanuy marcanuy 538 nov 27 10:55 ./_includes/adsense_side.html~
-rw-rw-r-- 1 marcanuy marcanuy 1143 nov 22 14:08 ./_sass/cached/_main.scss~
-rw-rw-r-- 1 marcanuy marcanuy 331 nov 22 14:08 ./_sass/cached/_share_buttons.scss~
-rw-rw-r-- 1 marcanuy marcanuy 46 nov 22 07:44 ./_sass/cached.scss~
-rw-rw-r-- 1 marcanuy marcanuy 3324 nov 26 18:06 ./_layouts/default.html~
-rw-r--r-- 1 marcanuy marcanuy 2569 nov 27 01:51 ./index.md~
-rw-rw-r-- 1 marcanuy marcanuy 34 nov 26 16:20 ./trans/hola.md~
-rw-rw-r-- 1 marcanuy marcanuy 2689 nov 27 01:45 ./trans/es.md~
-rw-rw-r-- 1 marcanuy marcanuy 2256 nov 22 18:09 ./trans/index.md~
-rw-rw-r-- 1 marcanuy marcanuy 835 nov 22 18:25 ./README.md~
-rw-r--r-- 1 marcanuy marcanuy 947 nov 21 19:34 ./assets/main.scss~
-rw-rw-r-- 1 marcanuy marcanuy 811 nov 21 19:53 ./Gemfile~
-rw-r--r-- 1 marcanuy marcanuy 290 nov 22 13:36 ./_config.yml~

Remove files

After making sure the above files we want to delete, we add the -delete flag: find -name "*~" -print -delete.


$ find -name "*~" -print -delete
./_includes/lang_nav.html~
./_includes/head.html~
./_includes/share_buttons.html~
./_includes/adsense_content_1.html~
./_includes/adsense_content_2.html~
./_includes/adsense_side.html~
./_sass/cached/_main.scss~
./_sass/cached/_share_buttons.scss~
./_sass/cached.scss~
./_layouts/default.html~
./index.md~
./trans/hola.md~
./trans/es.md~
./trans/index.md~
./README.md~
./assets/main.scss~
./Gemfile~
./_config.yml~
$ find -name "*~" -print
$ 

Important notes

Find parameters order matter

Warnings: Don't forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified. When testing a find command line that you later intend to use with -delete, you should explicitly specify -depth in order to avoid later surprises. Because -delete implies -depth, you cannot usefully use -prune and -delete together.

This is: using the -delete switch before -name deletes all the file tree recursively.

Safer find command

If your version of find doesn’t have the -delete switch, then you can execute rm on each matched file with -exec rm {} \;.

$ find -name "*~" -exec rm {} \;

Alternative using git

If you are working in a git repo, then there is a high chance you want to delete all the untracked local files from your current branch, that would delete all the automatically created backup files and possibly some other files you don’t want to be there.

If this is the case then you can use git clean.

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.


$ git clean -n
Would remove _data/people.yml~
Would remove _includes/author.html~
$ git clean

Parameters used:

-n --dry-run Don’t actually remove anything, just show what would be done.
-i --interactive Show what would be done and clean files interactively. See “Interactive mode” for details.

Summary

This is a simple way for removing backup files from Emacs and Vim. Special care should be taken when using the delete action of the find command, using it incorrectly could lead to deleting all the files recursively.

References

[Emacs]: Emacs

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Remove all backup files generated by Vim or Emacs from Linux console using the Linux find command correctly.

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·