Home > front end >  Use tar to extract files from an archive with an unknown name
Use tar to extract files from an archive with an unknown name

Time:01-20

I would like to use tar (on Windows) in a command line to extract files from an archive, that has a variable name, like *-archive.zip, that I don't know beforehand. I haven't found a way to use a wildcard for the targeted archive name, is there an option to do that ?

I have tried things like tar -xf "*-archive.zip", but it searches for a file with the same exact name, not the pattern.

Thanks.

CodePudding user response:

Use 'tar --wildcards ...' (works at least with tar 1.33). Example:

╰─➤ $ tar --wildcards -tvf /home/johndoe/.cpan/sources/authors/id/T/TO/TODDR/XML-Parser-2.44.tar.gz '*enc'
-rw-r--r-- todd/staff    40706 2014-12-11 07:51 XML-Parser-2.44/Parser/Encodings/big5.enc
-rw-r--r-- todd/staff    45802 2014-12-11 07:51 XML-Parser-2.44/Parser/Encodings/euc-kr.enc
-rw-r--r-- todd/staff     1072 2014-12-11 07:51 XML-Parser-2.44/Parser/Encodings/ibm866.enc
...

CodePudding user response:

What I ended up doing is switching to PowerShell, and ditched tar that cannot handle PowerShell pipes. The final command is :

Get-ChildItem -Name -Filter *-archive.zip | Expand-Archive

It works provided there is only one file matching the pattern in the current working folder.

  • Related