Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

1 Answer 1

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!"
    fi
done
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
2  
Yeah, change the -z to -j (and the for loop, obviously) –  Oli Nov 21 '13 at 2:05

Your Answer

 
discard

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.