How can I create a file with a specific size from a command line?

125

39

How can I create a file (e.g. using the touch command or another similar way), but with a specific size (e.g. 14 MB)?

The use case is to test FTP with various file sizes. If we can create a file with a specific size, it will be useful to verify FTP transfer etc.

fasil

Posted 2013-06-18T11:14:22.880

Reputation: 1 301

9

Related SO question: http://stackoverflow.com/questions/257844/quickly-create-a-large-file-on-a-linux-system

– Der Hochstapler – 2013-06-18T13:49:22.733

Answers

160

Use truncate:

truncate -s 14M filename

From man truncate:

DESCRIPTION
   Shrink or extend the size of each FILE to the specified size

[...]

 -s, --size=SIZE
          set or adjust the file size by SIZE

Note: truncate may not be available on your system, e.g. on Mac OS X it's not installed by default (but you can easily install it, using macports for example). In these cases you may need to use dd or head instead.

Ashutosh Vishwa Bandhu

Posted 2013-06-18T11:14:22.880

Reputation: 1 274

truncate -s 16M MyTestFile.txt du -h MyTestFile.txt 4,0K MyTestFile.txt Please why du -h does not work in this case ? Thanks a lot for help :) – researcher – 2015-01-19T18:58:43.963

2wow. very nice, I didn't know this. – n611x007 – 2013-06-18T11:28:07.407

man says it all: truncate - shrink or extend the size of a file to the specified size – ssedano – 2013-06-18T12:48:44.923

12Very nice indeed. The touch is not needed though, you can simply run truncate -s 14M filename it will create filename directly. – terdon – 2013-06-18T13:00:40.577

cool... this is what I exactly looked for... thanks @oliver-salzburg – fasil – 2013-06-18T13:41:47.623

34

This seems to create a sparse file, whereas the below answer (that uses "dd") doesn't. I don't know if the difference is important to you, but I figured I'd point it out

– offby1 – 2013-06-18T17:33:51.747

@offby1 Thanks! I was wondering why this file size change wasn't showing up in the output of df -h. – SaultDon – 2013-06-20T23:14:45.430

100

EDIT: The simplest way is probably the truncate of Ashutosh Vishwa Bandhu's answer, but as pointed out by @offby1 that creates sparse files which may not be what you want. The solutions below create normal, full files.


The following commands create a 14MB file called foo:

  1. fallocate (thanks to @Breakthrough who suggested it in the comments and vote up Ahmed Masud's answer below which also mentions it.)

    fallocate -l 14000000 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 14MB foo1.txt 
    $ fallocate -l 14000000 foo2.txt 
    $ ls -ls foo?.txt 
        0 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:54 foo1.txt
    13672 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:55 foo2.txt
    
  2. Create a file filled with random data

    dd if=/dev/urandom of=foo bs=14MB count=1
    

                                                                              or

    head -c 14MB /dev/urandom > foo
    
  3. Create a file filled with \0s:

    dd if=/dev/zero of=foo.txt bs=14MB count=1
    

                                                                              or

    head -c 14MB /dev/zero > foo
    
  4. Create a file filled with the first 14MB of data of another file:

    head -c 14MB bar.txt > foo
    
  5. Create a file filled with the last 14MB of data of another file:

    tail -c 14MB bar.txt > foo
    

In all of the above examples, the file will be 14*1000*1000 if you want 14*1024*1024, replace MB with M. For example:

dd if=/dev/urandom of=foo bs=14M count=1
head -c 14M /dev/zero > foo

fallocate only deals in bytes, so you'd have to do (14*1024*1024=14680064)

fallocate -l 14680064 foo

terdon

Posted 2013-06-18T11:14:22.880

Reputation: 45 216

2Thanks... This will also meet my purpose of creating a file of desired size... – fasil – 2013-06-18T13:47:53.983

@fasil which one will you accept? – Dejan – 2013-06-18T15:03:32.167

I've tried head -c 14MB /dev/zero > 14mb.txt and i get

-rw-rw-r-- 1 root root 14000000 giu 18 17:33 14mb.txt

So M stand for 1000 and not 1024? – Fabio F. – 2013-06-18T15:35:05.463

@FabioF. yes, if you want 1024, use M instead of MB. See man head or man tail. – terdon – 2013-06-18T15:36:00.803

2i'd recommend against the use of dd if there is a simpler, less potentially-destructive approach. – acolyte – 2013-06-18T15:50:01.280

3@acolyte so would I. That's why each dd solution has a non-dd alternative. – terdon – 2013-06-18T17:54:01.620

1

The wiki entry says than even dd (with zeros) will create a sparse file, doesn't it? http://en.wikipedia.org/wiki/Sparse_file

– Michael – 2013-06-19T22:01:54.133

@terdon Neither tail nor head says anything about M and MB over here. Just logically, it would make more sense if M meano 10^6 and MB 1024^2. – Michael – 2013-06-19T22:05:53.587

1

@Michael I checked, just to be sure, and none of my commands produces sparse files. The dd command on the wiki page is completely different and does not use /dev/zero. As for whether M or MB is more logical, take it up with the developers. The man pages of head, tail and dd all clearly state that "MB =10001000, M =10241024". This may be different on other *nixes.

– terdon – 2013-06-20T01:16:28.203

1fallocate is also another good alternative, and is probably the quickest for larger files that don't require initialization with random data (from the SO question linked to under a comment in the OP). – Breakthrough – 2013-06-20T12:55:35.580

@Breakthrough yeah, wow, fallocate seems to be the best by far. – terdon – 2013-06-21T02:06:19.110

21

dd bs=1MB count=14 if=/dev/zero of=<yourfilename>

WARNING dd will silently overwrite anything you have the rights to write to, if you specify it either willingly or by accident. Make sure you understand dd, eg. with reading dd --help, and you don't screw up your dd command lines or else you can easily create irreversible data loss - even total.

For start, if= is the input file and of= is the output file. Always double check that you got it right, before running the command. :)

n611x007

Posted 2013-06-18T11:14:22.880

Reputation: 5 291

13

Use magic dd ;) man page

$ dd if=/dev/zero of=output.dat  bs=1024  count=14336

cause 14MB*1024 = 14336KB, or:

$ dd if=/dev/zero of=output.dat  bs=1MB  count=14

And you'll get 14mb output.dat file, filled with zeroes :D

mirkobrankovic

Posted 2013-06-18T11:14:22.880

Reputation: 936

8

This should be the fastest way to do it:

SEEK=$SIZE-1
dd if=/dev/zero of=outfile bs=1 seek=$SEEK count=1 

Otherwise just write a quick and dirty C or perl program that does a seek to the exact position and writes a single byte. This is a LOT faster than actually dumping data onto the blocks.

If you want to avoid sparse files then on Linux (and possibly other systems) you can use fallocate -l <length> from util-linux.

If that's unavailable, and you have a POSIX compliant system then you can write a quick and dirty program using posix_fallocate() library call. posix_fallocate guarantees allocation of the range specified so you will not get a disk-full on subsequent use of the range if it returns successfully

Ahmed Masud

Posted 2013-06-18T11:14:22.880

Reputation: 424

6

While dd is the portable way to do it, the commands mkfile and xfs_mkfile are written specifically for this purpose and are faster:

mkfile size[b|k|m|g] filename
xfs_mkfile size[b|k|m|g] filename

The catch is that neither command is universally available, so they are not good for writing portable scripts. However, if I'm doing it interactively via the command line, those are the commands I use.

By default they create zero-filled files and not sparse (or "holey") files. They both take the -n option to create sparse files instead.

Old Pro

Posted 2013-06-18T11:14:22.880

Reputation: 1 751

1

There is another option with Perl:

perl -e 'print "a" x 1024' > file.txt

This will create 1KB file. Replace 1024 with any number you like.

Majid Azimi

Posted 2013-06-18T11:14:22.880

Reputation: 917