简体   繁体   English

如何尾部位于压缩的 tar 文件中的文件的最后 n 行

[英]How to tail the last nth lines of a file that is located in a compressed tar file

Ubuntu 16.04 Ubuntu 16.04
Bash 4.4.20 Bash 4.4.20

I am wanting to tail the last 30 lines of a SQL file located in a tar.gz file.我想跟踪位于 tar.gz 文件中的 SQL 文件的最后 30 行。 I noticed that the SQL backups were failing due to a locked file.我注意到 SQL 备份由于文件锁定而失败。 Instead of me untarring the entire compressed file, I'd like to simply tail the SQL backup and then grep for certain phrases.我不想解压缩整个压缩文件,而是想简单地跟踪 SQL 备份,然后跟踪某些短语的 grep。

This StackOverflown Thread explains that this is not possible with zipped files.这个StackOverflown 线程解释了压缩文件不可能做到这一点。 Does this also apply to tar files that are created like so:这是否也适用于像这样创建的 tar 文件:

tar czf name.tar.gz foldername  

If this has been explained in another thread, please delete this thread and I'll keep searching.如果这已在另一个线程中进行了解释,请删除此线程,我会继续搜索。

You can use tar's -O option to print content of file then using | (pipe)您可以使用 tar 的-O选项打印文件内容,然后使用| (pipe) | (pipe) , you can apply basic unix operations, like tail: | (pipe) ,您可以应用基本的 unix 操作,例如 tail:

tar zxvf name.tar.gz <file_to_read> -O | tail

Sample command, to read last two lines of a file A1.txt示例命令,读取文件 A1.txt 的最后两行

tar zxvf B.tar.gz B/A1.txt -O | tail -2

Here B is a directory on which I have ran tar command, it contains many files along with file A1.txt这里 B 是我运行 tar 命令的目录,它包含许多文件以及文件 A1.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM