1

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

-images.tar.gz

  -folder_0_image_1.jpg
  -folder_0_image_2.jpg
  -folder_0_image_3.png
  -...

  -folder_1

      -folder_1_image_1.jpg
      -folder_1_image_2.jpg
      -...

  -folder_2

      -folder_2_image_1.jpg
      -folder_2_image_2.jpg
      -...

  -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 ...)

2

You need to exclude files in subfolders like this:

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

Explanation:

 --wildcards

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

--exclude='*/*'

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

-xvzf

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

images.tar.gz

the archive name, of course

'*.jpg'

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.