in Uncategorized

How to Create Empty File with Specified Size on Linux

If you require to do some upload/download test, you can create a dummy file which have specific file size. The following commands create a 20MB file called foo .:

  1. fallocate
    fallocate -l 20000000 foo
    

    This command is particularly impressive since it is as fast as truncate (instantaneous) irrespective of the desired file size (unlike the other solutions which will be slow for large files) and yet creates normal files, not sparse ones :

    $ truncate -s 20MB foo1.txt 
    $ fallocate -l 20000000 foo2.txt 
    $ ls -ls foo?.txt 
        0 -rw-r--r-- 1 root root 14000000 Jun 21 03:54 foo1.txt
    13672 -rw-r--r-- 1 root root 14000000 Jun 21 03:55 foo2.txt
    
  2. Create a file filled with random data
    dd if=/dev/urandom of=foo bs=1024k count=20
    
  3. Create a file filled with \0s:
    dd if=/dev/zero of=foo.txt bs=1024k count=20
    
  4. Create a file filled with the first 20MB of data of another file:
    head -c 20MB bar.txt > foo
    
  5. Create a file filled with the last 20MB of data of another file:
    tail -c 20MB bar.txt > foo