将最后 n 行文件添加到 tar/zip [英] Add last n lines of files to tar/zip

查看:39
本文介绍了将最后 n 行文件添加到 tar/zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定期发送可能会变得非常大的日志文件集合,因此我只想发送每个文件的最后 n 行.

I need to regularly send a collection of log files that can grow quite large, so I would like to only send the last n lines of the each of the files.

例如:

/usr/local/data_store1/file.txt (500 lines)
/usr/local/data_store2/file.txt (800 lines)

给定一个包含名为 files.txt 的所需文件列表的文件,我想用每个文件的最后 100 行创建一个存档(tar 或 zip).

Given a file with a list of needed files named files.txt, I would like to create an archive (tar or zip) with the last 100 lines of each of those files.

我可以通过使用尾文件创建一个单独的目录结构来做到这一点,但是当可能有一些管道魔法可以实现它时,这似乎是一种资源浪费.还必须保留完整的目录结构,因为不同目录中的文件可以具有相同的名称.

I can do this by creating a separate directory structure with the tail-ed files, but that seems like a waste of resources when there's probably some piping magic that can happen to accomplish it. Full directory structure also must be preserved since files can have the same names in different directories.

如果可能的话,我希望解决方案是一个 shell 脚本,但是 perl(没有添加模块)也是可以接受的(这适用于没有安装 ruby​​/python/etc.. 的 Solaris 机器.)

I would like the solution to be a shell script if possible, but perl (without added modules) is also acceptable (this is for Solaris machines that don't have ruby/python/etc.. installed on them.)

推荐答案

您专注于特定的实现,而不是着眼于更大的图景.

You are focusing in an specific implementation instead of looking at the bigger picture.

如果最终目标是在目标机器上拥有文件的精确副本,同时最大限度地减少传输的数据量,您应该使用 rsync,它会自动仅发送已更改的文件部分,并且还可以发送时自动压缩,接收时自动解压.

If the final goal is to have an exact copy of the files on the target machine while minimizing the amount of data transfered, what you should use is rsync, which automatically sends only the parts of the files that have changed and also can automatically compress while sending and decompress while receiving.

在目标机器上运行 rsync 不需要标准 sshd 之外的任何守护进程,并且要设置没有密码的自动传输,您只需要使用公钥身份验证.

Running rsync doesn't need any more daemons on the target machine that the standard sshd one, and to setup automatic transfers without passwords you just need to use public key authentication.

这篇关于将最后 n 行文件添加到 tar/zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆