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 it’s 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.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.