0

I need to search for a pattern (string) in all .tar archives and then append the names of archives which contains the pattern (string) provided in a text.txt folder. My code is:

for f in *.tar.gz 
do
  if [ -f "$f" ]; then
    found=$(tar -xvzf "$f" -O | grep -sE "$1")
    if [ ! -z "$found" ]; then
      echo "$f" >> text.txt
    fi
  fi

done

The problem is that I keep getting:

gzip:stdin:not in gzip format
tar:Child returned status 1
tar:Error is not recoverable:Exiting now
  • Your output indicates a data problem: not in gzip format. – user535733 May 22 at 21:38
2

Problem is in a file type. A file is not gz archive, but you tried to untar it tar -xvzf "$f". In your case, possible, it is tar, but with tar.gz extension.
Try to check type of files file your_file.tar.gz
Or try it for all tar.gz files

for f in *.tar.gz 
do
  if [ -f "$f" ]; then
    file "$f"
  fi
done

Result for gz archive should be my_file.tar.gz: gzip compressed data,...
If your files are tar archives, change tar -xvzf ... to tar -xvf ... in the script.

| improve this answer | |
  • I have only one tar archive on my current directory.I used your command and i get: pac.tar.gz :POSIX tar archive (GNU) – Nistor Mihnea May 23 at 8:54
  • @NistorMihnea, that means your file is tar, but not gz. In that case, you should change line 4 in your script. Instead "tar -xvzf", there should be "tar -xvf". Or, you can rename it to pac.tar (mv pac.tar.gz pac.tar), and make real gz by "gzip pac.tar". In that case, you do not need to change your script – Goran Paukovic May 23 at 10:23
  • Got it.The way i create tar archive is tar -cvf name.tar f1 f2 (f1 and f2 are folders).After i create this tar i should do gzip name.tar in order to use my script.If not i should use only tar -xvf right? – Nistor Mihnea May 23 at 11:12
  • True. Also, you can create a tar.gz archive in one step "tar -czvf file1 file2 file3 pac.tar.gz" – Goran Paukovic May 23 at 19:59
  • Understood.Thanks a lot, – Nistor Mihnea May 23 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.