+4 votes
315 views
Create large file in Linux quickly

in Guides by (552k points)
reopened | 315 views

1 Answer

+5 votes
Best answer

Tools and commands to create files in Linux.
How to create large files in Linux quickly and easily.
Advantages and disadvantages of dd to create large files.
Using the truncate command to create files.
Using fallacate to create large files.
How to create a large file whose content is all zeros.
Create a large file whose content is random.

In many cases it is necessary to create large files to perform tests or simply to perform some kind of configuration. When we need to create a large file in Linux quickly, the best option is to use the tools available to create one.

image

Our goal will be to create a large file and also do it quickly to avoid unnecessary waiting. This may seem very simple, requires that we pay attention to some considerations. For example, when we create the file, it may contain random data, duplicate data, non-duplicated data or simply zeros.

To cover the basic issues and offer a solid starting point in creating files in Linux, we have created this little explanatory tutorial.

Tools and commands to create files in Linux.

In Linux operating systems we have a wide variety of tools to create files of all kinds, but each has its advantages and disadvantages. In this tutorial we will show the usual tools with which executing a simple command in the console we will have the file created. The tools we will use are:

  • dd
  • truncate
  • fallocate

Advantages and disadvantages of dd to create large files.

First of all we have dd , which is the classic option that has always been used to create files of considerable size. This tool has been basically designed to copy files, so when we use it to create a file, all data blocks are written to the hard disk drive, which leads to a higher time in I / O operations .

But not all are disadvantages in dd , it has a very important advantage, compatibility. With dd we can create large files in any type of file system. We could say that it is the slow but more compatible option .

Using the truncate command to create files.

The second option that we are going to show you is the truncate command, which is the fastest option, but also the one that can give us more than one problem . Personally I advise against its use, since truncate does not locate and reserve the space, but simulates as if there were a file of the specified size .

Using fallacate to create large files.

Finally, we have the fallocate command, which is in my opinion the fastest and most reliable option . With fallocate , the specified space is reserved and located, but it is faster than dd because it does not write the data blocks. Its only limitation is that it only supports file systems: xfs, ext4, Btrfs and tmpfs .

Some examples of the use of fallocate are:

  • Create 512MB file: sudo fallocate -l 512M /archivogrande
  • Create 1GB file: sudo fallocate -l 1G /archivogrande

How to create large files in Linux quickly and easily.

Before continuing, I must clarify that to keep this tutorial as a basic introduction, we will not consider the possibility that the data in the created file can be duplicated or not. We are only going to focus on whether the data we want to be zero or if we want it to be random data.

How to create a large file whose content is all zeros.

Sometimes the content of the file that we are going to create interests us that it is all zeros or if the content doesn't matter to us, choosing to be all zeros is the fastest option. This type of files is widely used when we create a swap partition in Linux .

Depending on the command chosen, we would have the following examples:

  • To create a 512MB file: sudo dd if=/dev/zero of=/archivogrande bs=1024 count=512k
  • To create a 1GB file: sudo dd if=/dev/zero of=/archivogrande bs=1024 count=1024k

We see that we have used /dev/zero , which is a special file that always returns zero or null .

Create a large file whose content is random.

Sometimes we need the content of the file to be random or simply different from zeros, this is when we will use the special system files /dev/random and /dev/urandom .

Basically both files have the same purpose and both return a random value. We are not going to delve into the differences between /dev/random and /dev/urandom , for this tutorial it comes with knowing that /dev/random can be blocked, while /dev/urandom not.

The advantage of /dev/random is that it uses a greater entropy which guarantees us a greater randomness in the generated values, but as it can be blocked, it can sometimes be slow. In general, the use of /dev/urandom is the most recommended when creating files of considerable sizes.


by (3.5m points)

Related questions

+5 votes
1 answer
+5 votes
1 answer
+3 votes
1 answer
+5 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (552k points) | 216 views
+4 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users