Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

What is a quick and easy way to make a file that is, say, 2 GB in size?

share|improve this question

3 Answers 3

ftp://ftp.fsf.hu/testfiles/maketestfiles.sh

or Seek is the size of the file you want in bytes - 1.

dd if=/dev/zero of=filename.big bs=1 count=1 seek=1048575 # 1 MByte

dd if=/dev/zero of=filename.big bs=1 count=1 seek=10485759 # 10 MByte

dd if=/dev/zero of=filename.big bs=1 count=1 seek=104857599 # 100 MByte

dd if=/dev/zero of=filename.big bs=1 count=1 seek=1073741823 # 1024 MByte

dd if=/dev/zero of=filename.big bs=1 count=1 seek=42949672959 # 40960 MByte
share|improve this answer
up vote 25 down vote accepted

The zero-fill method took 38 seconds on an SSD and caused Ubuntu's graphical interface to become unresponsive due to excessive iowait.

$ time dd if=/dev/zero of=large bs=2G count=1
0+1 records in
0+1 records out
2147479552 bytes (2.1 GB) copied, 37.2512 s, 57.6 MB/s

real    0m38.059s
user    0m0.000s
sys 0m16.770s
$ du -B 1 --apparent-size large
2147479552  large
$ du -B 1 large
2147483648  large

fallocate creates large files instantly by directly manipulating the file's allocated disk space:

$ time fallocate -l 2G large

real    0m0.009s
user    0m0.000s
sys 0m0.000s
$ du -B 1 --apparent-size large
2147483648  large
$ du -B 1 large
2147487744  large

truncate also works instantly, and creates sparse files which don't use up actual disk space until data is written later on:

$ time truncate -s 2G large

real    0m0.003s
user    0m0.000s
sys 0m0.000s
$ du -B 1 --apparent-size large
2147483648  large
$ du -B 1 large
0   large
share|improve this answer

An easy way would be to use the dd command to write a file full of zeros.

dd if=/dev/zero of=outputFile bs=2G count=1
  • if = input file
  • of = output file
  • bs = bytes

Use G in the size argument if you want computer (1024*1024*1024) gigabytes, or GB if you want human (1000*1000*1000) gigabytes.

share|improve this answer
1  
i will just add that if you dont want all zeros, you can choose if=/dev/random –  Denwerko Jun 10 '11 at 6:28
    
Though /dev/random will take more time. –  con-f-use Jun 10 '11 at 8:11
9  
Using /dev/random will take an awful lot of time. Use /dev/urandom in that case (it's non-blocking, but not guaranteed to have the same level of randomness). Drawing 2 GB from either one will almost certainly completely exhaust your system's entropy, so don't do anything cryptographic for a while afterwards. –  Michael Kjörling Jun 10 '11 at 8:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.