Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

After a restoration of my HDD with PhotoRec I have a folder with 36,500 GZ archives. Of those, around 200 are very important for me.

Each of these archives contains exactly one TAR archive. These TAR archives contain folders and among others files of the following file types:

  • doc
  • docx
  • xls
  • rtf
  • pdf
  • ppt
  • ods
  • odt
  • jpg

Is it possible to create a shell script that

  1. opens each of these GZ archives
  2. checks if there is a TAR archive in it and - if yes
  3. opens the TAR archive
  4. checks if the TAR archive contains one of the given filetypes and - if yes
  5. moves the GZ archive containing the TAR to a given directory

As I am a complete beginner when it comes to shell scripts, I would be very grateful for any hint or pointer in the right direction on how such a script could look like.

share|improve this question
up vote 2 down vote accepted

This is untested. I've put a safety comment in there (so it just echos the gz file rather than moving it) but when you're sure it works, crack on.

for f in *.gz; do
    win=$(tar -ztvf "$f" | grep -iE 'doc|docx|xls|rtf|pdf|ppt|ods|odt|jpg$' | wc -l)
    if [[ $win -gt "0" ]]; then
        # mv "$f" /other/directory
        echo "$f contains one of my files!"
share|improve this answer
This works perfectly. Could I use a similar script to look inside .tar.bz2 files? – Stefan Nov 20 '13 at 19:53
Yeah, change the -z to -j (and the for loop, obviously) – Oli Nov 21 '13 at 2:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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