# 生成1GB的零填充文件
dd if=/dev/zero of=large_file bs=1G count=1
# 生成10GB的随机数据文件
dd if=/dev/urandom of=random_file bs=1G count=10
# 更快的生成方式(使用更大的块大小)
dd if=/dev/zero of=fast_file bs=64M count=16
# 瞬间创建一个5GB的空文件(不实际占用磁盘空间)
fallocate -l 5G sparse_file
# 预分配实际空间
fallocate -l 5G --dig-holes real_file
# 创建一个指定大小的空文件
truncate -s 2G empty_file
# 按大小分割(每个分割文件100MB)
split -b 100M large_file split_file_
# 按行数分割(每100万行一个文件)
split -l 1000000 large_logfile log_part_
# 合并分割的文件
cat split_file_* > combined_file
# 并行合并(更快)
parallel -j $(nproc) cat {} ::: split_file_* > combined_file
# 查看文件头部
head -n 100 large_file
# 查看文件尾部
tail -n 100 large_file
# 实时监控日志文件增长
tail -f growing_logfile
# 使用less查看大文件(支持搜索和导航)
less large_file
# 基本grep搜索
grep "pattern" large_file
# 更快的grep(忽略大小写)
grep -i "pattern" large_file
# 使用ripgrep(更快的替代品)
rg "pattern" large_file
# 只显示匹配的行号
grep -n "pattern" large_file
# 外部排序大文件
sort --parallel=$(nproc) -S 2G -o sorted_file large_file
# 去重
sort -u large_file > unique_file
# 按内存大小优化排序
sort --buffer-size=4G large_file > sorted_file
# 使用pv监控处理进度
pv large_file | gzip > large_file.gz
# 使用parallel并行处理
cat large_file | parallel --pipe --block 10M 'process_chunk'
# 使用mmap加速处理
mlocate --mmap large_file
# 创建稀疏文件
truncate -s 100G sparse_file
# 检查文件稀疏性
filefrag -v sparse_file
# 转换现有文件为稀疏文件
fallocate --dig-holes existing_large_file
# 使用python进行内存映射处理
python3 -c "
import mmap
with open('large_file', 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
# 处理内存映射内容
mm.close()
"
# 快速导入CSV到PostgreSQL
psql -c "\COPY table_name FROM 'large_file.csv' WITH (FORMAT csv)"
# MySQL导入优化
mysql --local-infile=1 -e "LOAD DATA LOCAL INFILE 'large_file.csv' INTO TABLE table_name"
# 实时监控磁盘I/O
iostat -x 1
# 查看进程I/O使用情况
iotop
# 文件系统缓存统计
free -h
# 增加文件描述符限制
ulimit -n 100000
# 使用更快的临时目录(如RAM磁盘)
export TMPDIR=/dev/shm
希望这些技巧能帮助您高效处理Linux上的大文件。根据具体场景选择合适的方法可以显著提高处理效率。