RMTF (ReMove Temp Files)

Heyas

As some applications (sed,grep,vi[m], etc) create some tempfiles, i'd changed a script to this: (sadly i cant find the original post (code) anymore (which just removed 2 'diffrent kinds'), just similar ones - forgot that as i was new to all this)

:) ~ $ cat $(which rmtf)

#!/bin/sh
#
#	sea's Script Tools: The 3rd Generation
#	File Description:	ReMove Temp Files
	script_version=0.9
#	Author:             Simon A. Erat (sea)
#	Created (y.m.d):    2011.07.24
#	Changed:            2013.08.25
#	License:            GPL v3
#
#	Help
#
	[[ "-h" = "$1" ]] && \
		echo -e "$(basename $0) ($script_version)
			\rReMoves Temp Tiles - Recursivly
			\rUsage: $(basename $0) [PATH1] [PATH2] [...]
			\r" && exit 99
#
#	Variables
#
	oldpath=$(pwd)
#
#	Display
#
	for a in "$@"
	do 
		[[ -d "$a" ]] && cd "$a"
		echo -e "Removing temp-files from: \t$(pwd)"
		find ./ -name '*~' -exec rm '{}' \; -print -or -name ".*~" -exec rm '{}' \; -print -or -name ".*.swp" -exec rm '{}' \; -print -or -name ".?outputstream*" -exec rm '{}' \; -print
	done
	cd "$oldpath"

However, Useful one should be aware of made me care/think about it, as i'd like to share it.
Do you see any dangerous stuff in there, or options to improve?

Thank you in advance

The cd is unsave - its exit must be checked! Imagine it suddenly fails with 'permission denied'...
Fix

if [[ -d "$a" ]] && cd "$a"
then
...
fi

The rm in find -exec are safe. I recommend rm -f , can avoid check-backs.
Last but not least, echo -e is not portable. Use printf instead.

1 Like