Home > Software engineering >  Create a constantly enlarging file on Mac OS using terminal
Create a constantly enlarging file on Mac OS using terminal

Time:12-10

I want to free up some storage and clear my System Data on my Mac. I did it once effectively by following instructions on Stackoverflow which said to create a file constantly increasing in size and then to delete that file. I can't find this topic anymore and I don't remember Terminal commands. So, how do i create a constantly increasing in size file on my Mac using Terminal? Thank you.

CodePudding user response:

The reason why you want to create the file sounds odd to me, but anyways, this is a simple way to create a fast-growing file:

yes > /path/to/file.txt

CodePudding user response:

You need to explain what limit you are looking for. Creating a "constantly increasing in size" file will eventually eat all your disk space, and the OS will crash, forcing you into recovery. The standard *nix/osx tool for this is dd.

Here's a typical example

dd if=/dev/zero of=FileName bs=1024 count=1000

The parameters here that are important:

  • if=/dev/zero reads from a system file that just has zeros. You will use this exactly as is
  • bs=1024 this is the blocksize of the file in bytes. Use this
  • count=1000 Your file will be bs*count, so in this case, 1000k ie. 1mb.
  • of=Filename Is the name of the output file. If you omit this, the data will be written to standard out, which you can use to your advantage by appending the data to an existing file.

Here is a bash script that will slowly create a large file in the /tmp directory. It adds 2mb to the file each time, then checks free file space, and will run until you hit the threshold, or you can end it in the terminal manually.

The variables involved can be easily modified if you want the iterations to run faster, or make each addition to the file larger.

#!/bin/bash
#make_empty_file.sh
echo "Writing to /tmp/empty_file.bin"
touch /tmp/empty_file.bin

let gb="1024*1000*5"
df=`df -Pk . | sed 1d | grep -v used | awk '{ print $4 "\t" }'`

while [ $gb -lt $df ] 
do
    dd if=/dev/zero bs=2048 count=1000 >> /tmp/empty_file.bin
    sleep 2
    df=`df -Pk . | sed 1d | grep -v used | awk '{ print $4 "\t" }'`
    free=$((df/1024))
    echo "$free Megabytes free..."  
done
  • Related