如何在 Linux 上创建大文件(需要很长的压缩时间)



我现在做并行作业所以我正在尝试创建虚拟文件并在背景上理解它。 喜欢这个

创建虚拟文件

for in ()
do 
Compress that file &
done
wait

我需要创建虚拟数据所以我尝试了

fallocate -l 1g test.txt

tar cfv test.txt

但是这个压缩工作只完成了 5 秒

如何创建大而需要较长压缩时间(3分钟~5分钟)的虚拟数据

这里有两件事。首先,tar不会压缩任何内容,除非您向它传递一个z标志以及您已经必须触发gzip压缩的内容:

tar cvfz test.txt

对于非常相似的效果,您可以直接调用gzip

gzip test.txt

第二个问题是,对于大多数压缩方案,一个巨大的零字符串(可能是您生成的)非常容易压缩。您可以通过提供随机数据来解决此问题。在类 Unix 系统上,您可以使用伪文件/dev/urandom。这个答案给出了三个选项,按偏好降序排列,具体取决于有效的选项:

  1. head理解后缀,例如 Gibibyte 的后缀G

    head -c 1G < /dev/urandom > test.txt
    
  2. head需要详细说明:

    head -c 1073741824 < /dev/urandom > test.txt
    
  3. 完全没有head,所以使用dd,其中文件大小是块大小 (bs) 乘以count(1073741824 = 1024 * 1048576):

    dd bs=1024 count=1048576 < /dev/urandom > test.txt
    

这样的事情可能会起作用。有一些bash特定的运算符。

#!/bin/bash                                                                                                                  
function createCompressDelete()
{
_rdmfile="$1"
cat /dev/urandom > "$_rdmfile" &  # This writes to file in the background
pidcat=$! #Save the backgrounded pid for later use
echo "createCompressDelete::$_rdmfile::pid[$pidcat]"
sleep 2
while [ -f "$_rdmfile" ]
do
fsize=$(du "$_rdmfile" | awk '{print $1}')                                                                                                 
if (( $fsize < (1024*1024) )); then  # Check the size for 1G
sleep 10
echo -n "...$fsize"
else
kill "$pidcat"  # Kill the pid
tar czvf "${_rdmfile}".tar.gz "$_rdmfile"  # compress
rm -f "${_rdmfile}"  # delete the create file
rm -f "${_rdmfile}".tar.gz  # delete the tarball
fi
done
}
# Run for any number of files
for i in file1 file2 file3 file4
do
createCompressDelete "$i" &> "$i".log & # run it in the background
done

最新更新