How do I extract files without folder structure using tar

后端 未结 5 1443
无人共我
无人共我 2021-01-30 12:11

I have a tar.gz-file with the following structure:

folder1/img.gif
folder2/img2.gif
folder3/img3.gif

I want to extract the image files without

5条回答
  •  温柔的废话
    2021-01-30 12:49

    You can use the --strip-components option of tar.

     --strip-components count
             (x mode only) Remove the specified number of leading path ele-
             ments.  Pathnames with fewer elements will be silently skipped.
             Note that the pathname is edited after checking inclusion/exclu-
             sion patterns but before security checks.
    

    I create a tar file with a similar structure to yours:

    $tar -tf tarfolder.tar
    tarfolder/
    tarfolder/file.a
    tarfolder/file.b
    
    $ls -la file.*
    ls: file.*: No such file or directory
    

    Then extracted by doing:

    $tar -xf tarfolder.tar --strip-components 1
    $ls -la file.*
    -rw-r--r--  1 ericgorr  wheel  0 Jan 12 12:33 file.a
    -rw-r--r--  1 ericgorr  wheel  0 Jan 12 12:33 file.b
    

提交回复
热议问题