简体   繁体   中英

How to extract files using wildcards in specific sub-directory

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

I will offer you the canonical way, used on the times of UNIX:

tar xf ../mein.tar `tar tf ../mein.tar|grep -E dir1/[a-zA-Z0-9]+.jpg$`

This will work independently of Linux or UNIX and also from the version of tar

Here is my test:

[...] ➤ tar tvf ../tar.tar
drwxr-xr-x .......        0 2019-01-15 10:33:31 dir1/
drwxr-xr-x .......        0 2019-01-15 10:21:14 dir1/dir11/
-rw-r--r-- .......        0 2019-01-15 10:21:14 dir1/dir11/file111.jpg
-rw-r--r-- .......        0 2019-01-15 10:21:14 dir1/dir11/file112.jpg
drwxr-xr-x .......        0 2019-01-15 10:21:14 dir1/dir21/
-rw-r--r-- .......        0 2019-01-15 10:21:14 dir1/dir21/file121.jpg
-rw-r--r-- .......        0 2019-01-15 10:21:14 dir1/dir21/file122.jpg
-rw-r--r-- .......        0 2019-01-15 10:21:16 dir1/file11.jpg
drwxr-xr-x .......        0 2019-01-15 10:21:02 dir2/
-rw-r--r-- .......        0 2019-01-15 10:21:14 dir2/file21.jpg
-rw-r--r-- .......        0 2019-01-15 10:21:14 file1.jpg

[....] ➤ tar tf ../tar.tar|grep -E dir1/[a-zA-Z0-9]+.jpg$
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

 Patterns match file name start.  Patterns match after any / (default for exclusion).  Use wildcards (default for exclusion).  Verbatim string matching.

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

这有效

tar -xf mein.tar  dir1/*.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM