从tar存档Linux中提取前10大文件 [英] Extract top 10 biggest files from tar archive Linux
问题描述
我想使用Linux终端从tar归档文件中仅提取前10个最大的文件.我可以插入要提取的文件的路径,但是我想知道是否可以使用命令对文件进行排序,以便仅提取其中10个最大的文件.
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 using a command to sort the files so I can extract only the 10 biggest of them.
tar -xvf myfile.tar | sort -n -r | head -n 2 > otherfile
(通过这种方式,我仅成功保存了文件名,而不是文件.)
tar -xvf myfile.tar | sort -n -r | head -n 2 > otherfile
(in this way I only succeeded to save the names of the files, not the files..)
可以请您提供帮助或建议吗?
Can you please help or advice?
推荐答案
您可以使用以下命令从tar中仅实际提取前10个最大的文件.
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 :
此命令会将存档内容列出到stdout.
This command will list archive contents to stdout.
tar -tvf <tar-name>
这将对内容进行排序.
tar -tvf <tar-name> |sort -n -r
这将排除目录:
tar -tvf <tar-name> |sort -n -r |egrep -v "^d"
这将打印前10个文件(默认情况下打印10个文件):
This will print the top 10 files (head by default prints 10):
tar -tvf <tar-name> |sort -n -r |egrep -v "^d"|head
这只会获取文件名:
tar -tvf <tar-name> |sort -n -r |egrep -v "^d"|head | awk '{print $9}'
获得文件名后,将其保存在files变量中,然后可以使用以下命令从tarball中获取确切的文件:
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
这篇关于从tar存档Linux中提取前10大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!