PDA

View Full Version : test for filetype with no error o/p


pickarooney
December 24th, 2006, 12:57 PM
I'm trying to test for the existence of a file type and perform an action on all files of that type, but don't want to get an error output if the filetype doesn't exist.

Both snippets below throw up errors depending on whether or not the filetype exists.


if [ -f *.txt ]
then
rm *.txt
fi

or

for file in *.txt
do
rm $file
done

po0f
December 24th, 2006, 03:26 PM
pickarooney,

`rm -f`.

pickarooney
December 24th, 2006, 03:39 PM
True, that works for the example I gave. However, if I expand the if/for loops to perform any other actions besides rm I still get errors. They're not serious errors, as the script still works, it's just a bit messy.

po0f
December 24th, 2006, 03:40 PM
pickarooney,

if [ -f *.txt ]; then
<some_command> 2>/dev/null
<some_other_command> 2>/dev/null
fi

pickarooney
December 24th, 2006, 03:53 PM
Yeah, that'll work. Thanks :)