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
You can use below commands to actually extract only the top 10 biggest files from a tar.
files=$(tar -tvf <tar-name> |sort -n -r |egrep -v "^d"|head | awk '{print $9}')
tar -xvf <tar-name> $files
Let me explain what it's doing exactly :
This command will list archive contents to stdout.
tar -tvf <tar-name>
This will sort the content.
tar -tvf <tar-name> |sort -n -r
This will exlcude the directories if any :
tar -tvf <tar-name> |sort -n -r |egrep -v "^d"
This will print the top 10 files (head by default prints 10):
tar -tvf <tar-name> |sort -n -r |egrep -v "^d"|head
This will fetch only the filenames :
tar -tvf <tar-name> |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 <tar-name> $files