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

New contributor
fff is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1

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 at 20:27
  • @fff You're welcome :-) – Ned64 Aug 14 at 22:23

Your Answer

fff is a new contributor. Be nice, and check out our Code of Conduct.

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.