Heya guys, I have this small script and i need to grep all the files and leave only the ones that contain a keyword and I’m stuck on this, any help in pointing out my dumb errors is appreciated 🙂
#!/bin/bash
server=(server1...server24)
.
.
.
for ((n=0; n <= 24 ; n++))
do
if grep -q "KEYWORD" directory/${server[$n]}.html ; then
echo Empty
else
rm -f directory/${server[$n]}.html
fi
done
.
.
.
You don’t have to make sure your count matches if you let your code do it for you in one of the following ways:
or
Use
!to invert the condition so that it’s “if the file does NOT contain the keyword, then delete it”.Use the
grepoption-qto quiet the output and-sto suppress error messages.