Sometimes you may need to create a large file in Linux, for testing purposes. There are several ways to do this. In this article, we will look at how to create large files in Linux. You can use these steps to create 100mb file in Linux, or create a 1gb File or create large file with random data.
How to Create a Large File in Linux
Here are the steps to create a large file in Linux. Let us say you want to create a 100Mb file.
1. Using dd
dd is the most common way to create large files in Linux. Here is the command to create file data.img with 100Mb space.
$ dd if=/dev/zero of=./data.img bs=1024 count=102400
Here is the command to create 1GB file.
$ dd if=/dev/zero of=./data.img bs=1024 count=1024000
But dd is a copy program that writes block of data and therefore takes a lot of time in I/O operations as the file grows bigger in size. It is one of the slowest method to create large files and not recommended. However, we have included it here since it is the most common and default method used by most system administrators.
2. Using Truncate
You can also use truncate command to create large files. It is one of the fastest ways to create big files. Truncate works by creating a sparse file, that is, a disk section which contains identical data such as zero. It does not store actual data and allocate space but cheats the OS by pretending that all the data is there. The actual space is allocated only when you start writing data to the file. Here is the command to create a 1Gb file using truncate command.
$ truncate -s 1G ./data.img
3. Using fallocate
fallocate is the best method to create large files. It reserves or allocates the specified disk space for your file, without actually writing anything to it. So in this case, you actually get the file of desired space, instead of getting a sparse file, as in truncate. Here is the command to create 1Gb file using fallocate.
$ fallocate -l 1G ./data.img
That’s it. In this article, we have learnt how to create large files in Linux.
Also read:
How to Find JDK Path in Linux
How to Get Length of List in Django Template
Difference Between df and du in Linux
How to Create Startup Disk in Ubuntu
How to Install Fail2ban in Docker
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.