How to generate a large file in Linux ?

RHEL 7.3

In Linux, how can I create a large dummy file which is 15GB in size ?

mkfile

?

You don't state the OS version, so I don't know what tools you may already have to do this for you, and it depends what you want the file for.

There is a way in C that you open a new file, skipping to a huge offset then writing a single character before closing it. That might do the trick but it doesn't initially consume disk space and won't worry you for IO load. That may not be good for what you are trying to test. Additionally, if you restore it or copy it, the target file will be the full size, so that might mean that the filesystem does not restore properly if you suddenly fill it during a restore.

A good way can be to take a tar of a good chunk of disk (any files will do) then append it to itself several times, repeating until you get the desired size. Something like:-

cd /path/to/empty/space                                           # An empty directory to build the file
tar -cvf - /path/to/source/data | compress > my_base_file         # Get a chunk of something to start with.  Anything really, but probably best not devices, database files etc. that might be open

cat my_base_file my_base_file my_base_file > my_large_file        # Triple the file size
mv my_large_file  my_base_file                                    # Move it back to save space

Repeat the last two lines as needed to get a big enough file. If you need to trim it down again, you could perhaps:-

split -d -a 1 -b 15G my_large_file part. 2>/dev/null              # Break it into 15Gb chunks with a numeric suffix of length 1
mv part.0 my_large_file                                           # Save the one you actually want
rm part*                                                          # Clean up the rest of the junk

I've made it compress the file (you may prefer gzip) so that it is a true performance test, i.e. it can't be further compressed during transmission etc.

I hope that this helps,
Robin

1 Like

Most if not all Linux distros are lacking mkfile .
The usual workaround is with dd

dd count=15 bs=1G </dev/zero >dummy
1 Like

Hi.

Some repetition, but also:

Generate specific-size file

        0) "dd", and other than Linux at:
           https://stackoverflow.com/questions/257844/quickly-create-a-large-file-on-a-linux-system

        1) mkfile (local), work-alike based on Solaris mkfile, q.v., dd wrapper
           Original from http://steve-parker.org/

        2) xfs_mkfile, part of xfsprogs, resides in /usr/sbin/
           Not restricted to XFS filesystems

        3) fallocate, preallocate or deallocate space to a file

        4) wsf (local), Write, create, sized file

        5) setosi (local), Set to size (length): make files same number of lines

        6) truncate, shrink or extend the size of a file to the specified size

Best wishes ... cheers, drl

3 Likes