#!/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.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018