Creating a file to store and organize data is one of the tasks of Linux users. We usually create an empty file and then import or transfer content to that file, We all do this to organize data. But sometimes the developers need a file of a specific size in special situations such as troubleshooting or testing certain cases. One way is to look for a file that they can use for their needs or create a file of the desired size, which will be the fastest way.
You may be wondering if it is possible to create a file by setting the desired size. The most common method that we all do when creating files, does not meet this requirement, but you can create large files with preset sizes in Linux using Linux commands.
The Linux system has a solution for every problem, and that’s why it always wins in the competition with other operating systems. Linux has become one of the best and most efficient operating systems because it provides a cure for any pain, you just need to get to know it a little, then you will become more interested in the Linux operating system day by day and your preference will always be Linux operating system. Especially, many challenges on the server will be easily solved by running the Linux operating system, which is why Linux VPS is the most popular virtual private server.
In this tutorial, you will easily create files larger than 1GB (from 1GB to 10GB) in Linux by using Linux commands. Stay with us until the end of the article to show you how this is possible and be amazed by the power of Linux.
Linux commands to create large files of a certain size
First, we provide a list of commands that you can use to create files larger than 1GB:
|dd||Copy and convert files (as rewriting, cloning, etc.) and create image files for network or file system testing.|
|fallocate||Allocating a space to a file before creating it.|
|truncate||Change the size of the existing file to the desired size.|
|df||Providing usable disk space.|
|du||Displaying the occupied disk space.|
|ls||Providing a list of file sizes.|
|seq||Expanding the file size by filling the file with the long sequence.|
Method 1: Creating large files through the dd command
By executing the
ddcommand, you can copy and convert files and create image files for tests. The main syntax for using the dd command to create a large file is as follows:
dd if=/path/to/input of=/path/to/output [options] ## OR ## dd if=/dev/zero of=/path/to/output.img [options] ## OR ## dd if=/dev/zero of=YOUR-IMAGE-FILE-NAME-HERE bs=1 count=0 seek=Size-HERE
- /dev/zero: represents a file with the NULL character.
- bs: stands for Block Size, which you determine the size of the file.
- Count: Specify the number of blocks.
Note: The file with NULL characters does not make it possible to count its lines. To create a text file using the dd command, you can type / dev/random.
Creating an image file using the dd command
To create an image file using the dd command, the first step is to check the free disk space to create an image file, use the following command for this purpose:
Then, to create an image file with the desired size, for example, 1024kb (1MB), enter the following command:
dd if=/dev/zero of=examplefile.img bs=1024 count=0 seek=1024
In the output of the above syntax that you executed, you created an empty file with a size of 1 MB. If you want to create an image file with a size of 10MB, you must enter the command as follows:
dd if=/dev/zero of=examplefile.img bs=1024 count=0 seek=$[1024*10]
To create a 100MB image file, the command you will execute will be in the following format:
dd if=/dev/zero of=examplefile.img bs=1024 count=0 seek=$[1024*100] $ ls -lh example file.img
Also, to create a 1GB image file, run the following command:
dd if=/dev/zero of=1g.img bs=1 count=0 seek=1G
According to the bs factor in the main syntax of the dd command, the following commands are used to confirm the file size:
ls -lh 1g.img stat 1g.img du -h 1g.im
Adjusting the size and number of blocks according to your needs and goals can be different.
Creating a large file with the desired size
Another syntax of the dd command, if the Linux system does not support the truncate command, which we will talk about later, is as follows:
dd if=/dev/zero of=/path/to/fiie.img bs=YOUR-FILE-SIZE-HERE count=1
For example, to create a file of size 1G (1g is used in *BSD/OS X) enter the following command:
dd if=/dev/zero of=1g.bin bs=1G count=1
sample output for the above command:
1+0 records in 1+0 records out 1073741824 bytes (1.1 GB) copied, 8.12307 s, 132 MB/s
To check the file you created, run the following command:
Method 2: Creating a large file through the fallocate command
The fastest and easiest way to create large image files is to use the fallocate command, whose main syntax is as follows:
fallocate -l Image_Size_Here /path/to/image.img
For example, to create a 1G image file, enter the following command:
fallocate -l 1G examplefile1.img
lscommand to check and confirm the file created with the desired size:
ls -lh examplefile1.img
-rw-r--r--. 1 root root 1.0G Nov 17 01:22 example file1.img
To check block allocation, you can also run
File: `examplefile1.img' Size: 1073741824 Blocks: 2097160 IO Block: 4096 regular file Device: 805h/2053d Inode: 9043983 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2022-11-17 01:22:27.575841698 -0600 Modify: 2022-11-17 01:22:27.575841698 -0600 Change: 2022-11-17 01:51:06.108962462 -0600
du -h examplefile1.img
Note: In the main syntax of the
fallocatecommand, the -l factor determines the size of the file in bytes. The suffixes k, m, g, t, p, and e are used to allocate the sizes KiB, MiB, and GiB.
It should be noted that you will get more information by referring to the man page of the fallocate command.
Method 3: Create a large file through the truncate command
By executing the truncate command, you will be able to change the size of the file (expand or reduce) to the desired size. If you need a larger file, you can increase the size of the file by executing the truncate command, and additional space will be inserted with 0 bytes. Also, in some cases, you can reduce the extra part of the file.
For example, to create a 3GB file using the truncate command, you must run the following command:
truncate -s 3G examplefile.img
Don’t wait for the output of the above command because you won’t get any output, you may worry whether the desired file is created with the size you specified or not, for this reason, to make sure the desired file is created, you can run the
ls -lh examplefile.img
Method 4: Creating a large file through the command seq
This method is the slowest method for creating large files, but familiarity with it can be helpful. In this method, to expand the size of the files, you fill the file with a long sequence according to your needs. If you use larger sequences, the size of your file will be larger. In the following, we will give an example to better understand how to use the seq command.
For example, to use the sequence 1000000000 to create an example file with an approximate size of 9.3G, you should use the following command:
Seq 1000000000 >> example file du -sh examplefile 9.3G examplefile
Note: The du command is used to check the apparent size of the created file. Therefore, you can run the following command to access the apparent size of the file:
du -h --apparent-size examplefile.img
This method is very slow and you have to wait a long time to create a large file. For more information about the applicable options through the seq command, you can refer to the man page about it.
The maximum file size in 32-bit Linux systems is 2 TB, and for file systems, you can define a maximum size of 273 bytes.
- With the -l - option, it provides a list of files with complete information that displays the size of the files in bytes.
- With the -h - option, it displays the file and directory size in KB, MB, GB, or TB when it is larger than 1024 bytes.
- With the -s - option, it provides a list of files and directories along with their sizes in blocks.
In this tutorial, using different commands, you were able to create large binary image files (larger than 1G or any required size) in Linux. By executing the dd and truncate commands, you will be able to create sparse files for optimal use of the file system space, and you can create files with the desired sizes and allocated blocks.
Among the methods we presented to create large files, using the fallocate command is the most efficient and fastest method, the only drawback is that you cannot create sparse files by executing this command.
If you need guidance on how to use the provided commands to create large files, you can run the
mancommand for the dd, and truncate commands:
man dd man fallocate
We hope that reading this article has satisfied you.
Thank you for your choice.