Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Create a file of certain size in Linux
When using Linux, we often need to create files of specific sizes for testing, development, or system administration tasks. This is particularly useful for testing disk space, creating dummy files for benchmarking, or generating placeholder files of predetermined sizes.
There are several efficient methods to achieve this using built-in Linux commands. Each method has its own advantages depending on the specific use case and performance requirements.
Available Methods
Linux provides multiple utilities for creating files of specific sizes, each with distinct characteristics:
| Command | Speed | File Content | Best Use Case |
|---|---|---|---|
fallocate |
Fastest | Undefined/sparse | Quick empty file creation |
truncate |
Fast | Zero-filled holes | Resizing existing files |
dd |
Slower | Actual data written | Testing I/O performance |
head/tail |
Variable | Zero bytes | Simple zero-filled files |
Using the fallocate Command
The fallocate command uses the fallocate() system call to pre-allocate disk space without actually writing data. This makes it the fastest method for creating large empty files.
fallocate -l 10M file.txt fallocate -l 1G large_file.dat fallocate -l 100MB test_file.bin
The -l option specifies the file length. You can use suffixes like K (kilobytes), M (megabytes), G (gigabytes), or specify exact bytes.
Using the truncate Command
The truncate command creates files or adjusts existing file sizes to a specified length. If the file doesn't exist, it creates a new one; if it exists, it resizes it.
truncate -s 10M file.txt truncate -s 0 empty_file.txt truncate -s +5M existing_file.txt
The -s option sets the size. Use + or - prefixes to extend or shrink existing files relative to their current size.
Using the dd Command
The dd command actually writes data to create files, making it slower but useful when you need files with real content for I/O testing.
dd if=/dev/zero of=file.txt bs=1M count=10 dd if=/dev/urandom of=random_data.bin bs=1024 count=1024
Here, bs sets the block size, count sets the number of blocks, and if/of specify input and output files. Using /dev/zero creates zero-filled files, while /dev/urandom creates files with random data.
Using head and tail Commands
The head and tail commands can read specific amounts of data from /dev/zero to create zero-filled files.
head -c 300K /dev/zero > file.txt tail -c 1G /dev/zero > large_file.txt
The -c option specifies the number of bytes to read. These commands are simple but less efficient than fallocate for large files.
Examples with File Size Verification
$ fallocate -l 5M test1.txt $ truncate -s 5M test2.txt $ dd if=/dev/zero of=test3.txt bs=1M count=5 5+0 records in 5+0 records out 5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.003 seconds, 1.7 GB/s $ ls -lh test*.txt -rw-rw-r-- 1 user user 5.0M Jan 15 10:30 test1.txt -rw-rw-r-- 1 user user 5.0M Jan 15 10:30 test2.txt -rw-rw-r-- 1 user user 5.0M Jan 15 10:30 test3.txt
Conclusion
Linux provides multiple efficient methods for creating files of specific sizes. Use fallocate for fastest empty file creation, truncate for file resizing operations, and dd when you need actual data written for testing purposes. Choose the method that best fits your specific requirements and performance needs.
