Pages

Saturday, July 14, 2012

Shell script to check if tar file have .txt files or not

This is a small script to check if the given tar file is having a .txt files or not.

#!/bin/bash
if tar tvf abc.tar.gz | grep '.txt'
then
echo "Text file present"
else
echo "No text file"
fi

Let me explain what this script will do. This if loop checks if grep command executed successfully or not. If its executed successfully(Found the file) then it will print out "Text file present"

How about check for multiple file extensions such as txt, jpg, html?


#!/bin/bash
if tar tvf abc.tar.gz | grep -E '(txt|html|jpg)'
then
echo "Text file present"
else
echo "No text file"
fi

Here we used -E option because our grep command is using extend regular expression. To know more about RegExp please check it here or here.

0 comments:

Post a Comment