Coder Perfect

In Linux, how can I make a file of a specific size?


I need to create a file of a specified size for testing purposes (to test an upload limit).

On Linux, what command do I use to create a file of a specific size?

Asked by Grundlefleck

Solution #1

For small files:

dd if=/dev/zero of=upload_test bs=file_size count=1

Where file size is the number of bytes in your test file.

For big files:

dd if=/dev/zero of=upload_test bs=1M count=size_in_megabytes

Answered by Ilya Kochetov

Solution #2

Please, please, please, please, please, please, please, please, please, please, please, please, please When it comes to Linux (pick one)

truncate -s 10G foo
fallocate -l 5G bar

On a file system that supports sparse files, truncate will create a sparse file, whereas fallocate would not. A sparse file is one in which the file’s allocation units are not allocated until they are needed. The file’s meta-data, on the other hand, will take up a significant amount of space, but not nearly as much as the file itself. You should look at sparse file resources for further information, as this sort of file has both benefits and drawbacks. The blocks (allocation units) in a non-sparse file are allocated ahead of time, indicating that the space is reserved as far as the file system is concerned. Also, neither fallocate nor truncate will change the file’s contents.

Depending on the file system in use and its conformity to any standards or specifications, this behavior could be different. As a result, thorough investigation is recommended to verify that the correct procedure is employed.

Answered by jørgensen

Solution #3

To add to Tom’s post, you can also use dd to produce sparse files:

dd if=/dev/zero of=the_file bs=1 count=0 seek=12345

On most unixes, this will create a file with a “hole” in it; the data will not be written to disk or take up any space until anything other than 0 is written into it.

Answered by Brian

Solution #4

Use this command:


Set $INPUT-FILE=/dev/zero to generate a large (empty) file. The file will be $BLOCK-SIZE * $NUM-BLOCKS in size. $OUTPUT-FILE is the name of the newly created file.

Answered by Grundlefleck

Solution #5

The mkfile command is also accessible on OSX (and Solaris, apparently):

mkfile 10g big_file

This creates the “big file” file, which is 10 GB in size. This method was discovered here.

Answered by steve

Post is based on