Sometimes, I would need to create files of size 1MB, 100MB, 250MB, 1.5GB, 10GB or 100GB. There are different quick ways to create files of varying sizes in Linux. Most of these steps work on macOS too.
How to create a file of a specific size in Linux, and macOS too
You may also have questions like this:
- How do I create a file of size 1GB?
- How do I create a 10GB file?
- What is the fastest way to create a 5GB file?
- Are there one-liner commands to create file of specific sizes?
- Do you have a Python script to create a 100GB file?
Create file of 150MB using dd
Using dd
, this command will create a file bigfile.txt of size 150MB.
dd if=/dev/zero of=bigfile.txt bs=150M count=1
Output:
$ dd if=/dev/zero of=bigfile.txt bs=150M count=1 1+0 records in 1+0 records out 157286400 bytes (157 MB, 150 MiB) copied, 0.158732 s, 991 MB/s
You can also use this command:
dd if=/dev/zero of=bigfile.txt bs=15M count=10
Output:
$ dd if=/dev/zero of=bigfile.txt bs=15M count=10 10+0 records in 10+0 records out 157286400 bytes (157 MB, 150 MiB) copied, 0.147535 s, 1.1 GB/s $ ls -alh bigfile.txt -rw-r--r-- 1 arul arul 150M Aug 4 23:49 bigfile.txt
Create file of 250MB using truncate
The truncate
command can be used to extend or shrink a file to the desired size.
truncate -s 250M bigfile.txt
Output:
$ truncate -s 250M bigfile.txt $ ls -alh bigfile.txt -rw-r--r-- 1 arul arul 250M Aug 4 23:52 bigfile.txt
Create file of 300MB using fallocate
Using fallocate, the disk space of a file is either preallocated or deallocated. This is done by allocating and marking blocks as uninitialized without actually writing to them. This way, the file is generated much faster than if it were created by writing data into it.
fallocate -l 300M bigfile.txt
Output:
$ fallocate -l 300M bigfile.txt $ ls -alh bigfile.txt -rw-r--r-- 1 arul arul 300M Aug 4 23:56 bigfile.txt
Create file of 375MB using head
We can also create a file using the head
command by reading the first few lines of any text given to it as an input, and writing the text to standard output.
head -c 375MB /dev/zero > bigfile.txt
Output:
$ head -c 375MB /dev/zero > bigfile.txt $ ls -alh bigfile.txt -rw-r--r-- 1 arul arul 358M Aug 4 23:58 bigfile.txt $ ls -al bigfile.txt -rw-r--r-- 1 arul arul 375000000 Aug 4 23:58 bigfile.txt
The file generated using head
is substantially smaller because it actually counts the bytes.
- 375000000 bytes = 366210.9375 kilobytes
- 366210.9375 kilobytes = 357.6278 MB, which rounds to 358MB
Using Python to create a 100GB file or of any specific file size
You can create a file using Python as well. Let us create a file of size 100 GB using Python.
This Python program generate_bigfile.py will do the trick.
generate_bigfile.py
import os
with open('bigfile100gb.txt', 'w') as fp:
os.truncate(fp.fileno(), 1024 * 1024 * 1024 * 100)
Output:
$ python generate_bigfile.py $ ls -alh bigfile100gb.txt -rw-r--r-- 1 arul arul 100G Aug 5 00:26 bigfile100gb.txt
Conclusion
If you were able to create a file with random characters this blog post was useful, let me know which command you used. Thanks.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.