Ask a question »

Shell - Create a file of a specific size

April 2015


When writing a Java code to transfer files via SFTP and FTPS you may need to consider that you will be transferring files of small, medium and large sizes. The way to create files of a certain size in a Linux operating system is to use Shell and the dd command. You can use /dev/random or /dev/zero as a data source or null bytes. The terms used are block size, count - which is for number of blocks, input file and output file. The total size of the file created will be bs multiplied count bytes.

dd input file =/dev/misc output file=filename.dat block size=1024 count
The above command creates file of 1024kb, with random data allocation.

Using dd to create a file of a specific size




The dd command allows you to create an empty file of particular size. Creating these arbitrary heavy files, may be useful for testing purposes.
  • Use the command below:
    • dd if=/dev/zero of=file_to-create bs=1k count=1000
  • This command results in the creation of an empty 1Mb file
    • 1000 represents 1000 blocks of 1Kb


For unlimited offline reading, you can download this article for free in PDF format:
Shell-create-a-file-of-a-specific-size.pdf

See also

In the same category

Shell - Créer un fichier d'une taille précise
By asevere on November 8, 2004
Original article published by asevere. Translated by netty5. - Latest update by Paul Berentzen
This document entitled « Shell - Create a file of a specific size » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.