Extract top 10 biggest files from tar archive Linux

前端 未结 1 523
逝去的感伤
逝去的感伤 2021-01-28 12:01

I want to extract only the top 10 biggest files from a tar archive using Linux terminal. I can insert the path of the files to be extracted, but I want to know if I can do it us

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-28 12:29

    You can use below commands to actually extract only the top 10 biggest files from a tar.

    files=$(tar -tvf  |sort -n -r |egrep -v "^d"|head | awk '{print $9}')
    tar -xvf  $files
    

    Let me explain what it's doing exactly :

    This command will list archive contents to stdout.

    tar -tvf 
    

    This will sort the content.

    tar -tvf  |sort -n -r
    

    This will exlcude the directories if any :

    tar -tvf  |sort -n -r |egrep -v "^d"
    

    This will print the top 10 files (head by default prints 10):

    tar -tvf  |sort -n -r |egrep -v "^d"|head
    

    This will fetch only the filenames :

    tar -tvf  |sort -n -r |egrep -v "^d"|head | awk '{print $9}'
    

    Once we get the filenames, we save it in files variable and then we can use below command to fetch the exact files from the tarball :

    tar -xvf  $files
    

    0 讨论(0)
提交回复
热议问题