Since ~
files will come back the moment you clik the "Save" button in your editor, you might want to consider systematically excluding them and not simply removing the ones that are present now. This way your shell script will be more useful over time. You can do this using the find
command. Here is an example:
find . -type f -and \( -name "*~" -prune -or -print \)
It says that two things must be true: It must be a regular file, and it must either be the case that it ends with a ~
, in which case find will prune the result (i.e. not print it), or it gets printed.
The slightly less readable, POSIX-compliant version of this is:
find . -type f \( -name "*~" -prune -o -print \)
However, different editors have different naming schemes for temporary / backup files. Vim uses .filename.swp
, Emacs uses #filename#
and GEdit uses filename~
to name a few. For a truly versatile script, you may want to consider supporting these as well:
find . -type f \( -name "*~" -prune -o \
-name "*#" -prune -o \
-name "*.swp" -prune -o -print \)