问题描述
在Linux中,我有一个很大的文件,文件末尾附加了一些无关的信息.例如,假设我知道一个1.6GB文件的末尾有314个字节的无关数据.
当然,将更多数据添加到文件末尾是非常容易和有效的,但是我可以执行什么操作来删除它,而不必将该文件的第一部分复制到另一个文件中(或覆盖所述文件)?/p>
修改
我在C中看到了一些很好的建议.我希望从命令行编写脚本,但是失败了,我会比C更倾向于在python中实现它.
我看到python在其文件对象上有一个truncate方法,但是无论我如何使用它似乎都在破坏我的文件-我应该能够弄清楚这一点,但是当然答案仍然值得欢迎
使用函数 truncate
http://linux.die.net/man/2/truncate
int truncate(const char *path, off_t length);int ftruncate(int fd,off_t长度);
truncate采用文件名
ftruncate需要一个打开的文件描述符
这两个选项均将文件长度设置为 length
,因此它会被截断或拉长(在后一种情况下,文件的其余部分将填充为NULL/零).
截断(Linux shell命令)也将起作用
** SYNTAX **截断-s整数<文件名>**选项**-s number指定新文件的长度.如果新长度小于当前文件长度,则丢失数据.如果新长度更大,则文件将填充0.您可以指定大小字符以简化大数字:b或B的大小是字节.k大小为1000字节.K大小为1024字节.m的大小是10 ^ 6字节.M大小为1024 ^ 2字节.g 大小为 10^9 字节.G大小为1024 ^ 3字节.**例子**要将文件缩小到10个字节,请执行以下操作:截断-s 10/tmp/foo要将文件放大或缩小到345 MB:截断-s 345M/tmp/foo
[/edit]
In Linux, I have a rather large file with some extraneous information tacked on to the end of it. Let's say for example I know there are 314 bytes of extraneous data at the end of a 1.6GB file.
Of course it is very easy and efficient to add more data to the end of a file, but what can I do to remove it without having to copy the first portion of that file into another (or overwrite said file)?
Edit
I'm seeing some good advice on doing this in C. I was hoping to script it from the commandline, but failing that I would be more inclined to doing it in python than C.
I see that python has a truncate method on its file object but it seems to be demolishing my file no matter how i use it--I should be able to figure this one out, but of course answers are more than welcome still.
use the function truncate
http://linux.die.net/man/2/truncate
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
truncate takes the file name
ftruncate takes an open file descriptor
both of these set the file length to length
so it either truncates or elongates (in the latter case, the rest of the file will be filled with NULL/ZERO)
[edit]
truncate (linux shell command) will work also
**SYNTAX**
truncate -s integer <filename>
**OPTIONS**
-s number specify the new file length. If the new length is smaller than the current filelength data is lost. If the new length is greater the file is padded with 0. You can specify a magnitude character to ease large numbers:
b or B size is bytes.
k size is 1000 bytes.
K size is 1024 bytes.
m size is 10^6 bytes.
M size is 1024^2 bytes.
g size is 10^9 bytes.
G size is 1024^3 bytes.
**EXAMPLES**
To shrink a file to 10 bytes:
truncate -s 10 /tmp/foo
To enlarge or shrink a file to 345 Megabytes:
truncate -s 345M /tmp/foo
[/edit]
这篇关于如何在不读取整个文件的情况下从大文件的末尾删除X字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!