I have a tar file with the following contents:

$ tar tvf ../mein.tar 
drwxr-xr-x user/user         0 2019-01-14 15:31 ./
drwxr-xr-x user/user         0 2019-01-14 15:30 ./dir2/
-rw-r--r-- user/user         0 2019-01-14 15:30 ./dir2/file21.jpg
-rw-r--r-- user/user         0 2019-01-14 15:29 ./file1.jpg
drwxr-xr-x user/user         0 2019-01-14 15:30 ./dir1/
drwxr-xr-x user/user         0 2019-01-14 15:30 ./dir1/dir11/
-rw-r--r-- user/user         0 2019-01-14 15:30 ./dir1/dir11/file111.jpg
-rw-r--r-- user/user         0 2019-01-14 15:30 ./dir1/dir11/file112.jpg
drwxr-xr-x user/user         0 2019-01-14 15:30 ./dir1/dir21/
-rw-r--r-- user/user         0 2019-01-14 15:30 ./dir1/dir21/file122.jpg
-rw-r--r-- user/user         0 2019-01-14 15:30 ./dir1/dir21/file121.jpg
-rw-r--r-- user/user         0 2019-01-14 15:30 ./dir1/file11.jpg

It contains several sub-directories which may or may not contain jpg files. The file names are not always the same, but I know that there is exactly one jpg file in directory 'dir1'. I want to extract this file only, none of the other jpg files, neither in dir2 nor in dir1/dir*.

What I have tried so far is

$ tar -x -f ../mein.tar --wildcards 'dir1/*.jpg'
tar: dir1/*.jpg: Not found in archive
tar: Exiting with failure status due to previous errors

or

$ tar -x -f ../mein.tar --wildcards --wildcards-match-slash 'dir1/*.jpg'
tar: dir1/*.jpg: Not found in archive
tar: Exiting with failure status due to previous errors

If I use:

$ tar -x -f ../mein.tar --wildcards  "*.jpg"

Then I get all jpg files, this is not what I want.

Does anyone have a clue how to extract the jpg file in dir1 given that I don't know the exact name of that file? What I expect is to get exactly one file in sub-directory dir1, the output should be 'dir1/file11.jpg'.

You need to use --no-anchored like this:

$ tar -xf ../mein.tar --wildcards --no-anchored  "dir1/*.jpg"

According to the man page:

File name matching options

<pre><code><b>--anchored</b> Patterns match file name start. <b>--no-anchored</b> Patterns match after any / (default for exclusion). <b>--wildcards</b> Use wildcards (default for exclusion). <b>--no-wildcards</b> Verbatim string matching. </code></pre>

See also untar only a certain number of files from a large tarball