Re: shell script removing log files.
On Mon, Dec 22, 2014 at 10:51:26AM -0800, firstname.lastname@example.org wrote:
> This command in a shell script removes unwanted log files.
> for i in $( echo *.Log ); do
> /bin/rm $i;
> echo "Removed $i."
> In the edge case of no matching files, rm complains.
> /bin/rm: cannot remove `*.Log': No such file or directory
> If echo is replaced with ls, it complains when there
> is no match.
> Does anyone have a tidy solution for this task?
First of all, you have an unnecessary use of "echo". Although
seemingly innocuous, if you have file names with spaces, the spaces
will mess things up as things will be going through the shell
twice. Which is once too many.
Second, remember that when the shell performs pathname expansion, it
will leave the original pattern in there if nothing matches. Hence
the error message you see.
This should do the trick:
for i in *.Log ; do
if test -f "$i"; then
echo Removed $i.
or if you want to make things *really* simple:
rm --force --verbose *.Log
Karl E. Jorgensen