I have a tar file. That's what its structure looks like:







  -folder_x ...

How do I extract all the files from the root directory that have the .jpg extension?

(I'd like to extract these files: folder_0_image_1.jpg, folder_0_image_2.jpg ...)


You need to exclude files in subfolders like this:

tar --wildcards --exclude='*/*' -xvzf images.tar.gz '*.jpg'



means we specify files to extract by a wildcard, i.e. *.jpg - specified later


an option to exclude (from being selected for extraction) all entries with a / in them - i.e. all files in subfolders


eXtract, Verbose output, gunZip decompress first, archive from a File


the archive name, of course


filename pattern - we promised tar one, here it is - everything that ends in .jpg.

  • Thank you for your detailed answer.
    – fff
    Aug 14 '19 at 20:27
  • @fff You're welcome :-)
    – Ned64
    Aug 14 '19 at 22:23

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.