从Linux开始压缩(截断)文件

问题描述 投票:6回答:3

从一开始就可以在Linux(和/或其他Unix)上使用“缩小”文件吗?我想将其用于持久队列(没有现有的实现方式可以满足我的需求)。从文件的结尾,我猜想可以使用truncate()。

file truncate shrink
3个回答
0
投票

如果使用的是ext4,xfs或其他现代文件系统,则从Linux Kernel 3.15开始,您可以使用:

#include <fcntl.h>

int fallocate(int fd, int mode, off_t offset, off_t len);

带有FALLOC_FL_COLLAPSE_RANGE标志。

http://manpages.ubuntu.com/manpages/disco/en/man2/fallocate.2.html


-2
投票

是,可以使用cuttail删除文件的一部分。

cut -b 17- input_filetail -c +17 input_file

这将从第17个字节开始输出input_file的内容,实际上删除了文件的前16个字节。注意,cut示例还将在输出中添加换行符。


-5
投票

我使用以下Python脚本将截断的文件作为参数提供给64 000 000字节:

#!/usr/bin/env python

import sys
import os

file = sys.argv[1]
f = os.open(file, os.O_RDWR)
os.ftruncate(f, 64000000)
os.close(f)
© www.soinside.com 2019 - 2024. All rights reserved.