6 Methods To Create A File In Specific Size In Linux

If you want to create a specific size of a file in Linux to test your upload or download speed on your file server. Or

If you want to create a certain size of a file in Linux to extend your swap partition then you can use one of the below method to create it.

Navigate to the following URL, if you want to know multiple ways to create a file in Linux.

I already know few commands to perform this but i have included all the possible options in this article.

You can check all the commands which is available in this article and choose the preferred one for you.

I’m going to create a 10M file using all commands to test this.

It can be done using the following 6 methods.

  • fallocate: fallocate is used to preallocate or deallocate space to a file.
  • truncate: truncate is used to shrink or extend the size of a file to the specified size.
  • dd: Copy a file, converting and formatting according to the operands.
  • head: head is used to print the first part of files.
  • xfs_mkfile: xfs_mkfile command allow us to create a specific size of a file in Linux.
  • perl: Perl is a programming language specially designed for text editing.

How To Create A File In Specific Size In Linux Using fallocate Command?

fallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it. For filesystems which support the fallocate system call, preallocation is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks. This is much faster than creating a file by filling it with zeroes.

$ fallocate -l 10M daygeek.txt

Use the ls command to check the given file size.

$ ls -lh daygeek.txt 
-rw-rw-r-- 1 daygeek daygeek 10M Feb  3 13:43 daygeek.txt

How To Create A File In Specific Size In Linux Using truncate Command?

truncate is used to shrink or extend the size of a file to the specified size.

$ truncate -s 10M daygeek1.txt

Use the ls command to check the given file size.

$ ls -lh daygeek1.txt 
-rw-rw-r-- 1 daygeek daygeek 10M Feb  3 13:44 daygeek1.txt

How To Create A File In Specific Size In Linux Using dd Command?

The dd command stands for data duplicator. It is used for copying and converting data (from standard input to standard output, by default).

Also, dd command allow us to create a Bootable USB Disk in Linux.

$ dd if=/dev/zero of=daygeek2.txt  bs=10M  count=1
or
$ dd if=/dev/zero of=daygeek3.txt  bs=1M  count=10

1+0 records in
1+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.03385 s, 310 MB/s

Use the ls command to check the given file size.

$ ls -lh daygeek2.txt 
-rw-rw-r-- 1 daygeek daygeek 10M Feb  3 13:44 daygeek2.txt

How To Create A File In Specific Size In Linux Using head Command?

The head command reads the first few lines of any text given to it as an input and writes them to standard output (which, by default, is the display screen).

$ head -c 10MB /dev/zero > daygeek4.txt

Use the ls command to check the given file size.

$ ls -lh daygeek4.txt 
-rw-rw-r-- 1 daygeek daygeek 9.6M Feb  3 13:45 daygeek4.txt

How To Create A File In Specific Size In Linux Using xfs_mkfile Command?

xfs_mkfile creates one or more files. The file is padded with zeroes by default. The default size is in bytes, but it can be flagged as kilobytes, blocks, megabytes, or gigabytes with the k, b, m or g suffixes, respectively.

$ xfs_mkfile 10M daygeek5.txt

Use the ls command to check the given file size.

$ 1  ls -lh daygeek5.txt 
-rw------- 1 daygeek daygeek 10M Feb  3 13:45 daygeek5.txt

How To Create A File In Specific Size In Linux Using perl Command?

Perl stands in for “Practical Extraction and Reporting Language”. Perl is a programming language specially designed for text editing. It is now widely used for a variety of purposes including Linux system administration, network programming, web development, etc.

$ perl -e 'print "a" x 10485760' > daygeek6.txt

Use the ls command to check the given file size.

$ ls -lh daygeek6.txt 
-rw-rw-r-- 1 daygeek daygeek 10M Feb  3 13:47 daygeek6.txt

All together in the single output.

$ ls -lh daygeek*
-rw-rw-r-- 1 daygeek daygeek  10M Feb  3 13:44 daygeek1.txt
-rw-rw-r-- 1 daygeek daygeek  10M Feb  3 13:44 daygeek2.txt
-rw-rw-r-- 1 daygeek daygeek  10M Feb  3 13:44 daygeek3.txt
-rw-rw-r-- 1 daygeek daygeek 9.6M Feb  3 13:45 daygeek4.txt
-rw------- 1 daygeek daygeek  10M Feb  3 13:45 daygeek5.txt
-rw-rw-r-- 1 daygeek daygeek  10M Feb  3 13:47 daygeek6.txt
-rw-rw-r-- 1 daygeek daygeek  10M Feb  3 13:43 daygeek.txt

Vinoth Kumar

Vinoth Kumar has 3.5+ years of experience in Linux server administration & RHEL certified professional. He is currently working as a Senior L2 Linux Server administrator.

You may also like...